Problem 1017 - Duan's problem
/*
Time Limit: 1000MS Memory Limit: 65536KB Difficulty:
Total Submit:
179 Accepted: 42 Special Judge: No
Duan was a naulty boy. One day, he
made a mistake again. His teacher was very angry, and want to punish him. The
teacher wanted him to solve a problem. If he successfully solved it, he could go
home.
Here is the problem. Duan is given two number a and b. As
everybody knows, a number is made of 0-9. The teacher wanted him to count how
many 0-9 in a*b, and calculate which one appears more than others.
For
example, if a is 100, b is 4, then a*b is 400.
Number 0 1 2 3 4 5 6 7 8 9
Frequency 2 0 0 0 1 0 0 0 0 0
so duan should answer 0.
If a is 110,b
is 10,then a*b is 1100
Number 0 1 2 3 4 5 6 7 8 9
Frequency 2 2 0 0 0 0
0 0 0 0
so duan should answer 0 and 1.
Can you help
him?
Input
There are multiple test cases.Each case contains two
integers a and b (0 <= a < 10^10000, 0<=b<10^9). Input file ends of
EOF.
Output
On a single line, print the numbers of largest
frequency in incremental order separated by one space.
Sample
Input
100 4
110 10
Sample Output
0
0 1
*/
也算是大数乘法,这里a的范围是10^10000,所以用数组存,b可用int存下,这里模拟手算乘法。下面是AC代码。
1 #include<stdio.h> 2 #include<string.h> 3 #include<stdlib.h> 4 char s[10020]; 5 int b,len,ans[10]; 6 int main() 7 { 8 int i,f,j,max; 9 while(scanf("%s%d",s,&b)!=EOF) 10 { 11 memset(ans,0,sizeof(ans)); 12 f=0; 13 max=0; 14 len=strlen(s); 15 while(len--) 16 { 17 f+=(long long)b*(s[len]-'0'); 18 ans[f%10]++; 19 f/=10; 20 } 21 while(f) 22 { 23 ans[f%10]++; 24 f/=10; 25 } 26 for(i=0;i<10;i++) 27 { 28 if(max<ans[i]) {max=ans[i];j=i;} 29 } 30 printf("%d",j); 31 for(i=j+1;i<10;i++) 32 { 33 if(ans[i]==max) printf(" %d",i); 34 } 35 printf("\n"); 36 } 37 return 0; 38 }