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 寒假训练 1的更多相关文章
- FZU ICPC 2020 寒假训练 4 —— 模拟(一)
P1042 乒乓球 题目背景 国际乒联现在主席沙拉拉自从上任以来就立志于推行一系列改革,以推动乒乓球运动在全球的普及.其中11分制改革引起了很大的争议,有一部分球员因为无法适应新规则只能选择退役.华华 ...
- FZU ICPC 2020 寒假训练 6 —— 字符串处理
P1603 斯诺登的密码 题目描述 2013年X月X日,俄罗斯办理了斯诺登的护照,于是他混迹于一架开往委内瑞拉的飞机.但是,这件事情太不周密了,因为FBI的间谍早已获悉他的具体位置--但这不是最重要的 ...
- FZU ICPC 2020 寒假训练 5 —— 排序
P1177 [模板]快速排序 题目描述 利用快速排序算法将读入的 N 个数从小到大排序后输出.快速排序是信息学竞赛的必备算法之一.对于快速排序不是很了解的同学可以自行上网查询相关资料,掌握后独立完成. ...
- FZU ICPC 2020 寒假训练 4 —— 模拟(二)
P1056 排座椅 题目描述 上课的时候总会有一些同学和前后左右的人交头接耳,这是令小学班主任十分头疼的一件事情.不过,班主任小雪发现了一些有趣的现象,当同学们的座次确定下来之后,只有有限的 D 对同 ...
- FZU ICPC 2020 寒假训练 3
P1308 统计单词数 题目描述 一般的文本编辑器都有查找单词的功能,该功能可以快速定位特定单词在文章中的位置,有的还能统计出特定单词在文章中出现的次数.现在,请你编程实现这一功能,具体要求是:给定一 ...
- FZU ICPC 2020 寒假训练 2
A - 排序 输入一行数字,如果我们把这行数字中的'5'都看成空格,那么就得到一行用空格分割的若 干非负整数(可能有些整数以'0'开头,这些头部的'0'应该被忽略掉,除非这个整数就是由 若干个'0'组 ...
- 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, ...
- 寒假训练第九场 Brocard Point of a Triangle
题意:求布洛卡点坐标 思路:直接利用布洛卡点的性质.http://pan.baidu.com/s/1eQiP76E #include<cstdio> #include<cstring ...
- FJUT2017寒假训练二题解
A题 题意:让你找出唯一的一个四位数,满足对话时的要求. 思路:因为是4位数,可以直接从1000-9999遍历一遍,判断是否有唯一的数能满足所有条件,如果不是唯一的或者没有满足条件的数就输出Not s ...
随机推荐
- P5048-[Ynoi2019 模拟赛]Yuno loves sqrt technology III【分块】
正题 题目链接:https://www.luogu.com.cn/problem/P5048 题目大意 就是这个 [QA]区间众数,但空间很小 长度为\(n\)的序列,要求支持查找区间众数出现次数. ...
- 实时渲染基础(4)纹理(Texture)
目录 纹理映射(Texture Mapping) 球形贴图(Spherical Map) 立方体贴图(Cube Map) 纹理走样问题 Mipmap 各向异性过滤(Ripmap) 纹理应用技术(Tex ...
- 关于 Spring Boot 中创建对象的疑虑 → @Bean 与 @Component 同时作用同一个类,会怎么样?
开心一刻 今天放学回家,气愤愤地找到我妈 我:妈,我们班同学都说我五官长得特别平 妈:你小时候爱趴着睡觉 我:你怎么不把我翻过来呢 妈:那你不是凌晨2点时候出生的吗 我:嗯,凌晨2点出生就爱趴着睡觉呗 ...
- 我惊了!CompletableFuture居然有性能问题!
你好呀,我是歪歪. 国庆的时候闲来无事,就随手写了一点之前说的比赛的代码,目标就是保住前 100 混个大赛的文化衫就行了. 现在还混在前 50 的队伍里面,稳的一比. 其实我觉得大家做柔性负载均衡那题 ...
- SPA测试
1.生产端:环境准备为了进行SPA测试,在生产数据库中创建了SPA测试专用用户,避免与其他用户相互混淆与可能产生的误操作. CREATE USER SPA IDENTIFIED BY SPA DEFA ...
- 【原创】xenomai 在X86平台下中断响应时间测试
1.中断响应时间 实时操作系统的意义就在于能够在确定的时间内处理各种突发的事件,而中断这些事件.系统抢占调度的触发点,因而衡量嵌入式实时操作系统的最主要.最具有代表性的性能指标参数无疑是中断响应时间. ...
- 题解 [HNOI2007]分裂游戏
题目传送门 题目大意 有趣的取石子游戏即将开始. 有 \(n\) 堆石头,编号为 \(0,1,2,...,n-1\).两个人轮流挑石头. 在每个回合中,每个人选择三堆编号为 \(i,j,k\) 的石头 ...
- 题解 Beautiful Pair
题目传送门 题目大意 给出一个 \(n\) 个点的序列 \(a_{1,2,...,n}\) ,问有多少对点对 \((i,j)\) 满足 \(a_i\times a_j\le a_k(i\le k\le ...
- 洛谷3163 CQOI2014危桥 (最大流)
一开始想了一发费用流做法然后直接出负环了 首先,比较显然的思路就是对于原图中没有限制的边,对应的流量就是\(inf\),如果是危桥,那么流量就应该是\(2\). 由于存在两个起始点,我们考虑直接\(s ...
- hadoop学习笔记:运行wordcount对文件字符串进行统计案例
文/朱季谦 我最近使用四台Centos虚拟机搭建了一套分布式hadoop环境,简单模拟了线上上的hadoop真实分布式集群,主要用于业余学习大数据相关体系. 其中,一台服务器作为NameNode,一台 ...