题目传送门:4952. Another Rock-Paper-Scissors Problem
思路:
比较简单的一道题,不过也要理清逻辑思路。
姑且用1 2 3 代替R P S比较方便,题目意思是首先会出123 ,每次达到3的次方数之后就开始有人模仿,所以接着会出231 312,然后出
231312123来打败模仿他前9次的人...
这里注意各个分界点是3的次方数,又有3 ^ 25 < 10 ^ 12 < 3 ^ 26 . base[i]记录3 ^ i是多大。count 记录滚动赢的累计次数,例如计算
n = 33时,因为n > 27 ,n - 27 = 6,意思是如果第6次出1,则第33次必须出2.又有6 > 3,6 - 3 = 3.即第3次出3,则第6次必须出1。同理第3次
出的要累计赢第一次出的2次,因此总共算的话第33次出的要赢第一次出的1 + 1 + 2 = 4次,而第一次出1,所以第33次出(1 + 4) % 3 = 2。这里
还要注意如果整除3表示出3,但余数是0.
代码:
1 #include <iostream> 2 using namespace std; 3 4 int main(){ 5 long long n; 6 long long base[26]; 7 base[0] = 1; 8 for(int i = 1;i < 26;i++) 9 base[i] = base[i - 1] * 3; 10 while(cin >> n && n != 0){ 11 int count = 1; 12 for(int i = 25;i >= 0;i--){ 13 while(n > base[i]){ 14 n -= base[i]; 15 count++; 16 } 17 } 18 count = count % 3; 19 if(count == 1) 20 cout << 'P' << endl; 21 else if(count == 2) 22 cout << 'S' << endl; 23 else 24 cout << 'R' << endl; 25 } 26 return 0; 27 }