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 ...
随机推荐
- Head中的标签
Head中的其它一些用法 1.scheme (方案) 说明:scheme can be used when name is used to specify how the value of conte ...
- Unable to create SVNRepository object
I think you are missing at least the library setup stuff: /* * Initializes the library to w ...
- HtmlHelper的扩展
HtmlHelper的扩展: 注意点:扩展方法必须是静态方法,所在的类必须是静态类,所在的命名空间改成System.Web.MVC则能省略页面中必须添加命名空间的约束. //主要就是输出分页的超级链接 ...
- WebClient与WebRequest差异
WebRequst的使用 WebClient和HttpWebRequst是用来获取数据的2种方式,在我的这篇数据访问(2)中主要是讲的WebClient的使用,一般而言,WebClient更倾向于“按 ...
- Python—进程、线程、协程
一.线程 线程是操作系统能够进行运算调度的最小单位.它被包含在进程之中,是进程中的实际运作单位.一条线程指的是进程中一个单一顺序的控制流,一个进程中可以并发多个线程,每条线程并行执行不同的任务 方法: ...
- Balanced Lineup(树状数组 POJ3264)
Balanced Lineup Time Limit: 5000MS Memory Limit: 65536K Total Submissions: 40493 Accepted: 19035 Cas ...
- laravel框架总结(七) -- 数据库操作
1.使用DB门面进行基本操作 一旦你设置好了数据库连接,就可以使用 DB facade 来进行查找.DB facade 提供每个类型的查找方法:select.update.insert.delet ...
- jquery总结05-常用事件02-表单事件
表单事件 .focus()元素获得焦点时 阻止冒泡 子元素不可以 .blur() 元素失去焦点时 阻止冒泡 子元素不可以 .change() input.select.textarea值发生改变时 i ...
- powerdesinger中建立一个表后,出现Existence of index的警告
可以不检查 Existence of index 这项,也就没有这个警告错误了!意思是说没有给表建立索引,而一个表一般至少要有一个索引,这是一个警告,不用的话对执行没有影响~ 转载:http://bl ...
- WebSocket实战之————GatewayWorker使用笔记例子
参考文档:http://www.workerman.net/gatewaydoc/ 目录结构 ├── Applications // 这里是所有开发者应用项目 │ └── YourApp // 其中一 ...