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. xLua中Lua调用C#

    xLua中Lua调用C# 1.前提 这里使用的是XLua框架,需要提前配置xlua,设置加载器路径: 可以参考之前的Blog:<xlua入门基础>: //调用段,所有的lua代码都写在Lu ...

  2. Dapr + .NET Core实战(十二)服务调用之GRPC

    什么是GRPC gRPC 是一种与语言无关的高性能远程过程调用 (RPC) 框架. gRPC 的主要优点是: 高性能轻量级 RPC 框架. 协定优先 API 开发,默认使用协议缓冲区,允许与语言无关的 ...

  3. 从零入门 Serverless | 课时5 函数的调试与部署

    作者 | 江昱 阿里巴巴高级产品经理 本文整理自<Serverless 技术公开课>,关注"Serverless"公众号,回复"入门",即可获取 S ...

  4. Python | JSON 数据解析(Json & JsonPath)

    一.什么是JSON? JSON(JavaScript Object Notation, JS 对象简谱) 是一种轻量级的数据交换格式.它基于 ECMAScript (欧洲计算机协会制定的js规范)的一 ...

  5. Django序列化页和过滤页规范

    序列化类:serializers.py from rest_framework import serializers from goods.models import Goods, GoodsCate ...

  6. golang []byte和string的高性能转换

    golang []byte和string的高性能转换 在fasthttp的最佳实践中有这么一句话: Avoid conversion between []byte and string, since ...

  7. 2020.11.6-vj补题

    A - A CodeForces - 136A 题解:按输入的顺序输出对应的下标即可,定义一个数组,将输入的作为下标,下标为值,最后依次输出即可: #include<bits/stdc++.h& ...

  8. FastAPI 学习之路(十三)Cookie 参数,Header参数

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

  9. Java(3)基本数据类型及其类型转换

    作者:季沐测试笔记 原文地址:https://www.cnblogs.com/testero/p/15201501.html 博客主页:https://www.cnblogs.com/testero ...

  10. 款阿里开源的 Java 诊断工具Arthas

    Arthas是什么鬼? Arthas是一款阿里巴巴开源的 Java 线上诊断工具,功能非常强大,可以解决很多线上不方便解决的问题. Arthas诊断使用的是命令行交互模式,支持JDK6+,Linux. ...