1259.Problem G. play the guitar
Description
lmh like playing the guitar, but he don’t satisfy others’ GTP. He want to write guitar score by himself. And he don’t want to use 53231323, it’s boring! He want to find a new rhythm. So he come up with a idea: get a GTP by computer and math. So he choose “n^k” this expression. But we know, the guitar just have only six chords. So we have to change all 7,8,9 to 1,2,3(7 to 1, 8 to 2, 9 to 3, 0 means open string).
Now give you n and k, can you give lmh a prefect GTP?
Input
Input contains multiple test cases.
Each test case starts with two numbers n and k (0 < N <= 300, 0 < k <= 300).
Output
Calculate n^k and change all 7,8,9 to 1,2,3(7 to 1, 8 to 2, 9 to 3).
Sample Input
2 2
2 4
2 7
Sample Output
4
16
122
思路:将2的2次方, 2的4次方, 2 的7次方中的7变成1, 8变成2, 9变成3
一般人会用java处理 但在这里我选用了c
#include<iostream>
#include<cstring>
#include<cstdio>
#include<algorithm>
#include<vector>
#include<set>
#include<map>
#include<queue>
#include<cmath>
using namespace std;
int a[100005];
int b[100005];
int h[100005];
int main()
{
char s[100000];
while( scanf("%s", s)!= EOF )
{int n , k , i, j ;
scanf("%d", &k);
int h1 = strlen ( s );
int cnt1 = 1;
int cnt2 = 1;
for( i = h1-1; i>=0; i--)
{
a[cnt1++] = s[i] -'0';
b[cnt2++] = s[i] - '0';///从个位开始存储
}
int time = 0;
while( time < k-1 )
{
time++;
for( i = 1; i<cnt1; i++)
{
for( j =1; j<cnt2; j++)
{
h[i+j-1] = h[i+j-1] + a[i]*b[j];
h[i+j] =h[i+j]+ h[i+j-1]/10;
h[i+j-1] = h[i+j-1]%10;
}
}
if( h[i+j-2] != 0)
cnt2 = cnt2+cnt1 - 1;
else
cnt2 = cnt2+cnt1-2;
for( i = 1 ; i< cnt2; i++)
{
b[i] = h[i];
//printf("%d", h[i]);///不断更新b, a不变
}
memset( h, 0 ,sizeof( h ));
}
//for( i = cnt2-1; i>=1; i--)
//printf("%d", b[i]);
// printf("\n");
for( i = cnt2- 1; i>=1; i--)
{
if( b[i] == 7 )
b[i] = 1;
else if( b[i] == 8)
b[i] = 2;
else if( b[i] == 9)
b[i] = 3;
printf("%d", b[i]);
}
printf("\n");
memset( s, 0 ,sizeof( s));
}
return 0;
}