红、绿、蓝分别表示0、1、2,每次操作就相当于+1,原问题就转化为求n的三进制
表示的最低的m位,即求 n mod 3^m3的三进制表示。
复杂度 O(m)O(m)
1 #include <iostream> 2 #include <cstdio> 3 #include <cstring> 4 using namespace std; 5 typedef long long LL; 6 int main() 7 { 8 int m ,t; 9 LL n; 10 scanf("%d", &t); 11 int color[50]; 12 while (t--) 13 { 14 memset(color, 0, sizeof(color)); 15 scanf("%d%I64d", &m, &n); 16 int cnt = m; 17 while (n > 0 && m > 0) 18 { 19 color[m--] = n % 3; 20 n = n / 3; 21 } 22 for (int i = 1; i <= cnt; i++) 23 { 24 if (color[i] == 0) 25 printf("R"); 26 else if(color[i] == 1) 27 printf("G"); 28 else if (color[i] == 2) 29 printf("B"); 30 } 31 printf("\n"); 32 } 33 return 0; 34 } View Code
转载于:https://www.cnblogs.com/zhaopAC/p/5428058.html
相关资源:JAVA上百实例源码以及开源项目