FZU ICPC 2020 寒假训练 1

B - Sum Problem

In this problem, your task is to calculate SUM(n) = 1 + 2 + 3 + ... + n. 

Input

The input will consist of a series of integers n, one integer per line. 

Output

For each case, output SUM(n) in one line, followed by a blank line. 
You may assume the result will be in the range of 32-bit signed integer. 

Sample Input

1
100

Sample Output

1

5050

WrongAnswer

#include <stdio.h>
int main() {
    int n,sum;
    while(scanf("%d",&n)!=EOF){ 
       *sum=((1+n)*n)/2;*   //因为(1+n)*n时,当数据过大时会造成数据溢出,从而出现WA。
       printf("%d\n\n",sum);
    }
    return 0;
}

修改后:

#include<stdio.h>
int main(){
   int n,sum1;
   while(scanf("%d",&n)!=EOF){
      *sum1=n/2*(n+1);*
      printf("%d\n\n",sum1);
   }
   return 0;
}

C - A + B Problem II

I have a very simple problem for you. 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. 

Sample Input

2
1 2
112233445566778899 998877665544332211

Sample Output

Case 1:
1 + 2 = 3

Case 2:
112233445566778899 + 998877665544332211 = 1111111111111111110

WrongAnswer

这题我首先就注意到了第二组数据过大,所以我写了一个数据类型为long long的代码,如下:

#include<stdio.h>
int main()
{
    int n,i;
    long long a=0,b =0;
    scanf("%d",&n);
    for(i=1;i<=n;i++){
        scanf("%lld%lld",&a,&b);
        printf("Case %d:\n%lld + %lld = %lld",i,a,b,a+b);
        if(i!=n){printf("\n\n");}//这里要注意格式!!
    }
return 0;
}

但是很快发现时WA,后面我意识到需要用字符串进行解题,也就是大数问题(使2字符串的中的字符数字减去'0',逐个相加大于等于10的可以使本位减10,下一位自增1,后面的处理就非常简单了;)

#include<stdio.h>
#include<string.h>
int num(int sum[],int len1,int len2,int j,char str1[],char str2[]);
int main()
{
    int n,i,j;
    char str1[1010],str2[1010];
    long t;
    long len1,len2;
    scanf("%d",&n);
    for(i=1;i<=n;i++){
        int flag=0;
        int  sum[10000]={0};
        scanf("%s %s",str1,str2);
        printf("Case %d:\n%s + %s = ",i,str1,str2);
        len1=strlen(str1)-1;
        len2=strlen(str2)-1;
        j=0;
        while(len1>=0&&len2>=0){
            if(sum[j]+(str1[len1]-'0')+(str2[len2]-'0')>=10){//逢十进一
                sum[j]=sum[j]+(str1[len1]-'0')+(str2[len2]-'0')-10;
                sum[j+1]++;
            }
            else{
               sum[j]=sum[j]+(str1[len1]-'0')+(str2[len2]-'0');
            }
            j++;len1--;len2--;}
        if(len1>=0){
            for(t=len1;t>=0;t--){
                sum[j]=sum[j]+(str1[t]-'0');
                j++;
            }
        }
        else if(len2>=0){
            for(t=len2;t>=0;t--){
                sum[j]=sum[j]+(str2[t]-'0');
                j++;
            }
        }
        else if(sum[j]!=0) j++;//两个位数相同的数,这步不能丢!
            for(t=j-1;t>=0;t--){
                if(sum[t]==0&&flag==0) continue;
                else{
                    flag=1;
                    printf("%d",sum[t]);
                }
            }
        if(i!=n){printf("\n\n");}//注意输出格式
        else printf("\n");
    }
return 0;
}

[参考]--问题 C: A+B Problem II

上一篇:FZU ICPC 2020 寒假训练 3


下一篇:The Preliminary Contest for ICPC Asia Nanjing 2019