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 ...
随机推荐
- win8win10以管理员身份运行cmd方法
win8win10以管理员身份运行cmd方法 Win7/8下提示OpenSCManager failed 拒绝访问Maven nexus 安装nexus : wrapper | OpenSCManag ...
- Events in ASP.NET Master and Content Pages
Content page PreInit event. Master page controls Init event. Content controls Init event. Master pag ...
- PLSQL Developer 不能连接 oracle 12c 64位 的解决办法 for win 64
1.安装Oracle 12c 64位 2.安装32位的Oracle客户端( instantclient-basic-nt-12.1.0.1.0) 1) 下载instantclient-basic-nt ...
- 【转】Struts1.x系列教程(2):简单的数据验证
转载地址:http://www.blogjava.net/nokiaguy/archive/2009/01/archive/2009/01/13/251197.html 简单验证从本质上说就是在服务端 ...
- Locality Sensitive Hash 局部敏感哈希
Locality Sensitive Hash是一种常见的用于处理高维向量的索引办法.与其它基于Tree的数据结构,诸如KD-Tree.SR-Tree相比,它较好地克服了Curse of Dimens ...
- java list倒序输出及复制list集合
如原来有一个集合list,list里面是有数据的,现在如果把list中的集合倒序过来,加这代码 Collections.reverse(list);此代码中传入原来的list数据 有这代码后list在 ...
- EF中使用linq进行关联查询
EF使用linq进行多表查询是完全可以的,最后ToList()调用的时候回产生一条分页的sql语句,所以并不是全部查询再分页的.所以不会影响查询的性能 public void TestLinq() { ...
- linux passwd文件解析
#cat/etc/passwd root:x:::Superuser:/: daemon:x:::Systemdaemons:/etc: bin:x:::Ownerofsystemcommands:/ ...
- vs2012编译Qwt
主题:vs2012编译Qwt ------------------------------------------------------------------------- 参照网络地址: htt ...
- max-width实现图片的自适应
在自适应布局中,有时候会让图片随着宽度的变化相应的放大或者缩小,或者说让图片等比缩放,但是每个图片的大小又不一样,这个时候我们就要用到max-width这个属性了. img{ max-width:10 ...