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 寒假训练 1的更多相关文章

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

    P1042 乒乓球 题目背景 国际乒联现在主席沙拉拉自从上任以来就立志于推行一系列改革,以推动乒乓球运动在全球的普及.其中11分制改革引起了很大的争议,有一部分球员因为无法适应新规则只能选择退役.华华 ...

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

    P1603 斯诺登的密码 题目描述 2013年X月X日,俄罗斯办理了斯诺登的护照,于是他混迹于一架开往委内瑞拉的飞机.但是,这件事情太不周密了,因为FBI的间谍早已获悉他的具体位置--但这不是最重要的 ...

  3. FZU ICPC 2020 寒假训练 5 —— 排序

    P1177 [模板]快速排序 题目描述 利用快速排序算法将读入的 N 个数从小到大排序后输出.快速排序是信息学竞赛的必备算法之一.对于快速排序不是很了解的同学可以自行上网查询相关资料,掌握后独立完成. ...

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

    P1056 排座椅 题目描述 上课的时候总会有一些同学和前后左右的人交头接耳,这是令小学班主任十分头疼的一件事情.不过,班主任小雪发现了一些有趣的现象,当同学们的座次确定下来之后,只有有限的 D 对同 ...

  5. FZU ICPC 2020 寒假训练 3

    P1308 统计单词数 题目描述 一般的文本编辑器都有查找单词的功能,该功能可以快速定位特定单词在文章中的位置,有的还能统计出特定单词在文章中出现的次数.现在,请你编程实现这一功能,具体要求是:给定一 ...

  6. FZU ICPC 2020 寒假训练 2

    A - 排序 输入一行数字,如果我们把这行数字中的'5'都看成空格,那么就得到一行用空格分割的若 干非负整数(可能有些整数以'0'开头,这些头部的'0'应该被忽略掉,除非这个整数就是由 若干个'0'组 ...

  7. FZU ICPC 2020 寒假阶段测试 2

    P1464 Function 题目描述 对于一个递归函数w(a,b,c)如果a≤0 or b≤0 or c≤0就返回值1.如果a>20 or b>20 or c>20就返回w(20, ...

  8. 寒假训练第九场 Brocard Point of a Triangle

    题意:求布洛卡点坐标 思路:直接利用布洛卡点的性质.http://pan.baidu.com/s/1eQiP76E #include<cstdio> #include<cstring ...

  9. FJUT2017寒假训练二题解

    A题 题意:让你找出唯一的一个四位数,满足对话时的要求. 思路:因为是4位数,可以直接从1000-9999遍历一遍,判断是否有唯一的数能满足所有条件,如果不是唯一的或者没有满足条件的数就输出Not s ...

随机推荐

  1. pkusc2021游记

    @ 目录 前言 Day 0 Day 1 Day 2 Day 3 前言 到时候APIO的大概也会写在这篇里吧. Day 0 车,公交,飞机,公交,车 坐了半天的交通终于到了,整个人都坐的晕乎乎的,然后看 ...

  2. 如何一次性add library to classpath

    前言:导入项目时,时常需要手动导包,提示"add library to classpath",需要一个个找报红的类 点击添加本地项目包

  3. mybatis多种查询方法

    1. 查询单行,对象,并封装成一个对象 Employee getEmpById(Integer id); <select id="getEmpById" resultType ...

  4. oracle基础安全配置

    1.oracle中用户密码复杂度配置 1)查看参数 select limit from dba_profiles where resource_name='PASSWORD_VERIFY_FUNCTI ...

  5. 「JOISC 2020 Day2」变态龙之色 题解

    题目传送门 注意 同性必定不同色 必有一个同色异性,且不相互不喜欢 Solution 我们发现,我们问题比较大的就是如何确定性别问题.我们可以一个一个加进去,在原来已经确定了的二分图上增加新的性别关系 ...

  6. uoj22 外星人(dp)

    题目大意: 给定一个\(n\)个数的序列\(a\),给定一个\(x\),其中\(a\)数组可以进行顺序的调换,每一个\(a_i\)都能使$x=x \mod a_i \(, 求最后经过一系列计算后的\) ...

  7. SudokuSolver 1.0:用C++实现的数独解题程序 【一】

    SudokuSolver 1.0 用法与实现效果 SudokuSolver 是一个提供命令交互的命令行程序,提供的命令清单有: H:\Read\num\Release>sudoku.exe Or ...

  8. FastAPI 学习之路(十五)响应状态码

    系列文章: FastAPI 学习之路(一)fastapi--高性能web开发框架 FastAPI 学习之路(二) FastAPI 学习之路(三) FastAPI 学习之路(四) FastAPI 学习之 ...

  9. 2.3 Core Building Blocks 核心构件

    Core Building Blocks 核心构件 DDD mostly focuses on the Domain & Application Layers and ignores the ...

  10. String直接赋字符串和new String的区别

    String A="ABC"; String B=new String("ABC"); String A = "ABC";内存会去查找常量池 ...