HDOJ——简单题(1002,1004)

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:

For each case, print the color of balloon for the most popular problem on a single line. It is guaranteed that there is a unique solution for each test case.
输出最受欢迎的问题的气球的颜色。
思路:看到N<=1000,就说明用for遍历的话应该不会超时。就是把每个气球与后面的气球的比较一下。然后记录一下每个气球的数量。
#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]);    
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值