1002 :A + B Problem II
Given two integers A and B, your job is to calculate the Sum of A + B.
Input:
The first line of the input contains an integer T(1<=T<=20) which means the number of test cases. Then T lines follow, each line consists of two positive integers, A and B. Notice that the integers are very large, that means you should not process them by using 32-bit integer. You may assume the length of each integer will not exceed 1000。
Output:
For each test case, you should output two lines. The first line is "Case #:", # means the number of the test case. The second line is the an equation "A + B = Sum", Sum means the result of A + B. Note there are some spaces int the equation. Output a blank line between two test cases.
由输入可见:这里是不能用__int64的,所以显而易见用字符串来储存。
#include<stdio.h>
#include<string.h>
int main()
{
int t,i,j,k,l1,l2,l;
char a[1001],b[1001];
int num[1001];
scanf("%d",&t);
for(i=1;i<=t;i++)
{
getchar();
memset(a,0,sizeof(a)); memset(b,0,sizeof(b)); memset(num,0,sizeof(num));
scanf("%s%s",a,b);
l1=strlen(a); l2=strlen(b);
k=l1>l2?l1:l2;
l1--; l2--;
for(j=k;l1>=0&&l2>=0;j--)
{
num[j]+=a[l1]-'0'+b[l2]-'0'; // 这里不要少加num+=...;因为num后面会累加的,要小心
if(num[j]>9) {num[j]-=10; j--; num[j]++; j++; }
l1--; l2--;
}
while(l1>=0){
num[j]+=a[l1]-'0';
if(num[j]>9){
num[j]-=10; num[j--]++; j++;
}
j--; l1--;
}
while(l2>=0){
num[j]+=b[l2]-'0';
if(num[j]>9){
num[j]-=10; num[j--]++; j++;
}
j--; l2--;
}
printf("Case %d:\n",i);
printf("%s + %s = ",a,b);
for(l=1;l<=k;l++)
printf("%d",num[l]);
printf("\n");
if(i!=t) printf("\n");
}
}
*注意最后一个输出格式,当i==t时,那时候就不用再进行换行操作了。
1004:Let the Balloon Rise
the judges' favorite time is guessing the most popular problem. When the contest is over, they will count the balloons of each color and find the result.
Input:
Input contains multiple test cases. Each test case starts with a number N (0 < N <= 1000) -- the total number of balloons distributed. The next N lines contain one color each. The color of a balloon is a string of up to 15 lower-case letters.
A test case with N = 0 terminates the input and this test case is not to be processed.
Output:
输出最受欢迎的问题的气球的颜色。
#include<stdio.h>
#include<string.h>
int main()
{
int n,i,j,sum[1001],t;
char name[1001][20];
while(scanf("%d",&n)!=EOF)
{
getchar();
if(n==0) break;
for(i=1;i<=n;i++)
{ scanf("%s",name[i]);
sum[i]=1; }
for(i=1;i<n;i++)
for(j=i+1;j<=n;j++)
{
if(strcmp(name[i],name[j])==0) sum[i]++;
}
t=1;
for(i=1;i<=n-1;i++)
if(sum[i]>sum[t]) t=i;
puts(name[t]);
}
}