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

原文地址:https://www.cnblogs.com/lvhang/p/12238388.html

时间: 2024-08-30 12:20:20

FZU ICPC 2020 寒假训练 1的相关文章

FZU ICPC 2020 寒假训练 4 —— 模拟(一)

P1042 乒乓球 题目背景 国际乒联现在主席沙拉拉自从上任以来就立志于推行一系列改革,以推动乒乓球运动在全球的普及.其中11分制改革引起了很大的争议,有一部分球员因为无法适应新规则只能选择退役.华华就是其中一位,他退役之后走上了乒乓球研究工作,意图弄明白11分制和21分制对选手的不同影响.在开展他的研究之前,他首先需要对他多年比赛的统计数据进行一些分析,所以需要你的帮忙. 题目描述 华华通过以下方式进行分析,首先将比赛每个球的胜负列成一张表,然后分别计算在11分制和21分制下,双方的比赛结果(

FZU ICPC 2020 寒假训练 4 —— 模拟(二)

P1056 排座椅 题目描述 上课的时候总会有一些同学和前后左右的人交头接耳,这是令小学班主任十分头疼的一件事情.不过,班主任小雪发现了一些有趣的现象,当同学们的座次确定下来之后,只有有限的 D 对同学上课时会交头接耳.同学们在教室中坐成了 M 行 N 列,坐在第 i 行第 j 列的同学的位置是 (i,j),为了方便同学们进出,在教室中设置了 K 条横向的通道,L 条纵向的通道. 于是,聪明的小雪想到了一个办法,或许可以减少上课时学生交头接耳的问题:她打算重新摆放桌椅,改变同学们桌椅间通道的位置

FZU ICPC 2020 寒假训练 6 —— 字符串处理

P1603 斯诺登的密码 题目描述 2013年X月X日,俄罗斯办理了斯诺登的护照,于是他混迹于一架开往委内瑞拉的飞机.但是,这件事情太不周密了,因为FBI的间谍早已获悉他的具体位置--但这不是最重要的--最重要的是如果要去委内瑞拉,那么就要经过古巴,而经过古巴的路在美国的掌控之中. 丧心病狂的奥巴马迫降斯诺登的飞机,搜查时却发现,斯诺登杳无踪迹.但是,在据说是斯诺登的座位上,发现了一张纸条.纸条由纯英文构成:Obama is a two five zero.(以"."结束输出,只有6个

2020寒假训练记录

目标: 信息学奥赛之数学一本通重学 第一章 数论 第三章 组合数学 第四章 概率 第六章 矩阵 网络流 DP 多项式 生成函数 CFdiv1 30题 整理模板 2020-01-10 添加模板: 辛普森积分 中国剩余定理 矩阵乘法 提交:POJ1006 原文地址:https://www.cnblogs.com/FYH-SSGSS/p/12177888.html

NUC-ACM/ICPC 寒假训练 简单DP A - G题

第一题:数塔 HDU - 2084 做法: 从第 i , j 个 节点往下走的最优解可以由从第 i+1,j 个节点往下走的最优解和第i+1,j+1个节点往下走的最优解得出,二者取其优即可. 代码: 记忆化搜素 1 #include<iostream> 2 using namespace std; 3 #include<cstdio> 4 using namespace std; 5 int n; 6 int f[100][100]; 7 int v[100][100]; 8 int

2020 寒假记录(一)

2020 冬 寒假记录(一) 费解的开关 https://vijos.org/p/1197 当第 \(i\) 行的状态确定了之后,只有第 \(i+1\) 行可以影响它,也就是翻完第一行后,后面每一行的操作也就是确定的了,所以枚举第一行的32种状态即可. 深搜 #include <iostream> #include <algorithm> #include <cstdio> using namespace std; int dir[4][2] = { {0,-1},{0

窦小凤2020寒假学习心得

2020年1-2月寒假学习心得—窦小凤 一.学习成果 赛事准备上,每天坚持做题,完成代码量6000行(其中还包括了很多知识点学习上的代码). 学习python基础,对python有了基本认识,完成代码1500行(但其中基本都是python中的基础语法) 选择性学习了一些用Java语言讲的数据结构与算法. 前期主要是将Java的基础视频重看了一遍(主要是在学校的时候没认真),然后从最基础入门开始做起,到寒假末来看,自身在Java方面跟在大一上学期还是有较大进步,但仍还有很长的路要走. Python

常州大学新生寒假训练会试 题解

[题目链接] A - 添加逗号 注意是从后往前三个三个加逗号,最前面不允许有逗号 #include <bits/stdc++.h> using namespace std; const int maxn = 1e5 + 10; char s[maxn]; char ans[maxn]; int sz; int main() { scanf("%s", s); int len = strlen(s); sz = 0; int t = 0; for(int i = len -

2020寒假训练计划

今天是2019年的最后一天了,按照计划.应当对Python有了初步了解,Linux基础操作比较熟悉. 是时候分一下方向了,首先看看各个方向该学些什么. Web PHP 在Web通常是以代码审计.PHP各种协议.特性等等情况出现.基础要求是看懂题中的PHP代码,然后要对题目中常见绕过方法有了解.因此,PHP语言上,要学习到面对对象,能了解PHP序列化和反序列化及各种魔法方法.还要多刷题,了解题目的一些常见的考点,针对考点学习. SQL 要学习SQL注入首先要会SQL的基础语法,SQL语法比较简单,