題目大意:ios
http://codevs.cn/problem/1045/spa
代碼:code
#include <iostream> #include <cstring> using namespace std; int n,m; char ch[500]; int arr[500]; void print() { for(int i = 0 ; i < 500; i++) { if(arr[i] != -1) cout << arr[i] << " " ; else break; } cout << endl; } void add() { int tmp[500]; int arr1[500]; int count = 0; int l = 0; for(int i = 0; i < 500; i++) { if(arr[i] != -1) l++; else break; } l--; for(int i = l, j = 0; i >= 0 ;i--, j++) { arr1[j] = arr[i]; } int flag = 0; while(l >= 0) { int t = (arr[l] + arr1[l] + flag) % n; flag = (arr[l] + arr1[l] + flag) / n; tmp[count++] = t; l--; } if(flag == 1) tmp[count++] = 1; for(int i = count - 1, j = 0; i >= 0; i--, j++) { arr[j] = tmp[i]; } } bool judge() { int l = 0; for(int i = 0; i < 500; i++) { if(arr[i] != -1) l++; else break; } l--; int s = 0; while(s <= l) { if(arr[s] == arr[l]) { s++; l--; } else return false; } return true; } int main() { cin >> n >> ch; for(int i = 0; i < 500; i++) arr[i] = -1; for(int i = 0; i < strlen(ch); i++) { int tmp = ch[i] - '0'; if(tmp <= 9) arr[i] = tmp; else arr[i] = tmp - 7; } for(int i = 0; i <= 30; i++) { if(!judge()) { add(); } else { cout <<"STEP=" << i << endl; return 0; } } cout << "Impossible!" << endl; return 0; }