hdu 1047 (big integer sum, fgets or scanf, make you func return useful infos) 分类: hdoj 2015-06-18 08:21 39人阅读 评论(0) 收藏
errors made,
boundary conditions, <= vs < ,
decreasing vs increasing , ++, –,
‘0’/’1’ vs 0/1
prototype of sum, return the starting position of c-style string containing the sum, just like sprintf return number of characters successfully read.
p1=sum(p1,p2);
and
p=sprintf(p,"%09d",x);
have some similarities.
#include <cstdio>
#include <cstring>
char* sum(char *to, char *from) {
static const int charzero='0',oublecharzero='0'+'0';
int sz1=strlen(to), sz2=strlen(from), nszmax,nszmin, i,j, tmp,carry;
if(sz1>=sz2) { nszmax=-sz2, nszmin=-sz1; }
else { nszmax=-sz1, nszmin=-sz2; }
to+=sz1, from+=sz2;
for(carry=0,i=-1;i>=nszmax;--i) {
tmp=carry+to[i]+from[i]-doublecharzero;
to[i]=charzero+tmp%10; carry=tmp/10;
}
if(sz2>sz1) {
for(j=i;j>=nszmin;--j) to[j]=from[j];
sz1=sz2;
}
if(carry) {
for(j=i;j>=nszmin && to[j]=='9';--j) to[j]='0';
if(j<nszmin) { to[j]='1'; ++sz1; }
else { ++to[j]; }
}
return to-sz1;
}
int main() {
//freopen("input.txt","r",stdin);
const int MAXSIZE=101;
int ncase,i,j;
char buf1[MAXSIZE+2]={0}, buf2[MAXSIZE+2]={0}, *p1,*p2;
scanf("%d",&ncase);
for(i=1;i<=ncase;++i) {
p1=buf1+MAXSIZE, p2=buf2; p1[0]='0', p1[1]=0;
while(scanf("%100s",p2)==1 && strcmp(p2,"0")!=0) {
p1=sum(p1,p2);
}
puts(p1); if(i!=ncase) putchar('\n');
}
return 0;
}
版权声明:本文为博主原创文章,未经博主允许不得转载。// p.s. If in any way improment can be achieved, better performance or whatever, it will be well-appreciated to let me know, thanks in advance.
hdu 1047 (big integer sum, fgets or scanf, make you func return useful infos) 分类: hdoj 2015-06-18 08:21 39人阅读 评论(0) 收藏的更多相关文章
- hdu 1030 Delta-wave (C++, 0ms, explanatory comments.) 分类: hdoj 2015-06-15 12:21 45人阅读 评论(0) 收藏
problem description http://acm.hdu.edu.cn/showproblem.php?pid=1030 #include <cstdio> #include ...
- Hdu 1506 Largest Rectangle in a Histogram 分类: Brush Mode 2014-10-28 19:16 93人阅读 评论(0) 收藏
Largest Rectangle in a Histogram Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 ...
- HDU 1532 Drainage Ditches 分类: Brush Mode 2014-07-31 10:38 82人阅读 评论(0) 收藏
Drainage Ditches Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) ...
- Hdu 1507 Uncle Tom's Inherited Land* 分类: Brush Mode 2014-07-30 09:28 112人阅读 评论(0) 收藏
Uncle Tom's Inherited Land* Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (J ...
- hdu 1034 (preprocess optimization, property of division to avoid if, decreasing order process) 分类: hdoj 2015-06-16 13:32 39人阅读 评论(0) 收藏
IMO, version 1 better than version 2, version 2 better than version 3. make some preprocess to make ...
- Hdu 1429 胜利大逃亡(续) 分类: Brush Mode 2014-08-07 17:01 92人阅读 评论(0) 收藏
胜利大逃亡(续) Time Limit : 4000/2000ms (Java/Other) Memory Limit : 65536/32768K (Java/Other) Total Subm ...
- Hdu 1010 Tempter of the Bone 分类: Translation Mode 2014-08-04 16:11 82人阅读 评论(0) 收藏
Tempter of the Bone Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Othe ...
- Hdu 1009 FatMouse' Trade 分类: Translation Mode 2014-08-04 14:07 74人阅读 评论(0) 收藏
FatMouse' Trade Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) ...
- POJ2739 Sum of Consecutive Prime Numbers 2017-05-31 09:33 47人阅读 评论(0) 收藏
Sum of Consecutive Prime Numbers Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 25225 ...
随机推荐
- C++嵌套多个命名空间举例
首先在结构上是能经得起推敲的,举个例子: test.h #pragma region 嵌套多个命名空间举例 namespace Group { namespace C ...
- jquery判断id是否存在
1.判断标签是否存在 ){ 存在 } 2.判断(id="id名"的标签)是否存在,下面的不可以!!!因为 $("#id") 不管对象是否存在都会返回 objec ...
- js 多少天以后的时间
/** * 多少天以后的时间 * @param date 时间 * @param num 多少天 * @param type 类型 年 月 天(默认天) */ exports.afterDate = ...
- 【java报错】Could not instantiate listener
这个错误以前出现过好几次,莫名其妙的出现,莫名其妙的解决掉...昨天好好的,今天又出现了,记下来 2015-03-03 09:38:45.105:INFO:oejs.Server:jetty-8.1. ...
- print函数
python中print既可以写成print a,也可以写成print(a) >>> a=1 >>> print a 1 >>> print(a) ...
- JAVA线程锁lock下Condition高级使用-多个Condition的整合使用
import java.util.concurrent.locks.Condition; import java.util.concurrent.locks.Lock; import java.uti ...
- Visual Studio的Web Performance Test提取规则详解(2)
总结 Visual Studio的Web Performance Test是基于HTTP协议层的,它不依赖于浏览器,通过直接接收,发送HTTP包来和Web服务器交互.Web Performance T ...
- 前端工程师的PS默认工作区
右侧依次是信息.图层.历史记录,如下图:
- 《BI项目笔记》挑选产出分析Cube
数据源设置: 数据处理逻辑: --I_GBGradeID SELECT * FROM T_NPick_PkgMov WHERE I_GBGradeID NOT IN ( SELECT I_GBGrad ...
- Scrum Meeting 8-20151210
任务安排 姓名 今日任务 明日任务 困难 董元财 服务器入口更新 服务器发布记录接口 无 胡亚坤 请假(编译攻坚) 聊天界面优化 无 刘猛 请假(编译攻坚) 开新的分支存放服务器端或者,创建的仓库记录 ...