1 32 2
4
这道题就是模拟乘法,需要用到大数相乘和结构体的知识。最多不超过1e4;
代码:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <queue>
using namespace std;
const int M =1e4;
struct node{
int s[15];
};
int main(){
int n;
cin >> n;
while(n --){
int m, k;
cin >> m >> k;
/* if(k == 1){
printf()
}*/
node a;
memset(a.s, 0, sizeof(a.s));
int i = 1;
string st;
while(m){
a.s[i++] = m%10;
//st += (a.s[i-1]+'0');
m /= 10;
}
for(int i = 1; i <= k; ++ i)
st += (a.s[i]+'0');
// cout << st;
int res = 0;
node b = a, c;
//memset(c.s, 0, sizeof(c.s));
string str;
while(res < M){
memset(c.s, 0, sizeof(c.s));
for(int i = 1; i <= k; ++ i){
for(int j = 1; j <= k; ++ j){
c.s[i+j-1] += a.s[i]*b.s[j];
c.s[i+j] += c.s[i+j-1]/10;
c.s[i+j-1] %= 10;
}
/*str += (c.s[i]+'0');
cout << str;*/
}
for(int i = 1; i <= k; ++ i) str += (c.s[i]+'0');
++res;
if(st == str) break;
str.clear();
b = c;
}
if(res == M) cout << -1 <<endl;
else cout <<res << endl;
}
return 0;
} 原文:http://blog.csdn.net/shengweisong/article/details/44187889