ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

FZU ICPC 2020 寒假训练 1

2020-01-28 17:03:41  阅读:273  来源: 互联网

标签:int sum FZU ICPC len2 len1 2020 long line


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

标签:int,sum,FZU,ICPC,len2,len1,2020,long,line
来源: https://www.cnblogs.com/lvhang/p/12238388.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有