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 ...
随机推荐
- iPad和iPhone开发区别
原文:http://mobile.51cto.com/iphone-273895.htm iPad与iPhone 开发区别详解是本文要介绍的内容,先来看看他们的区别. 1.首先我们先从官方发布的SDK ...
- Unity随机随学
1.什么是渲染管道? 是指在显示器上为了显示出图像而经过的一系列必要操作.渲染管道中的步骤很多,都要将几何物体从一个坐标系中变换到另一个坐标系中去. 主要步骤有: 本地坐标->视图坐标-> ...
- svn使用方法介绍(1)
TotoiseSVN的基本使用方法 在 项目管理实践教程一.工欲善其事,必先利其器[Basic Tools]中,我已经讲解了怎样安装TortoiseSVN.在上面的讲解中已经讲了怎么使用VisualS ...
- linux下cmake编译安装、配置和卸载mysql
WIN10下虚拟机:VMware workstation 12 PRO 安装 # 1.查看系统版本 [root@vm-xiluhua][/home/xiluhua]$ cat /etc/redhat- ...
- php实现回复图文,图片,文字
[代码]php代码: <?php /** 微信公众平台 开发者模式 默认用户输入任何文字,均返回同一个图文信息,链接地址为手机站; 可以根据变量$keyword,即用户输入的信息,进行判断, ...
- Asp.net 之Application
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.We ...
- JAVA基础知识之JDBC——编程步骤及执行SQL
JDBC编程步骤 下面以mysql数据库为例, 1.加载驱动 首先需要下载数据库的驱动jar文件,并且在eclipse包中加入到class path中去, 例如mysql的驱动文件 mysql-con ...
- [SDN] What is SDN?
本篇学习笔记写于 Sun Nov 13 15:08:02 2016, 可能有认识不全面的地方. 参考资料为ONF的官方网站中: Software-Defined Networking (SDN) De ...
- 什么是 HTML?
前言 在 W3C(万维网联盟)官网里,有一套针对于初学者的 HTML 培训教程,为期四周.为了提升自己的翻译水平,同时帮助大家入门,我给大家翻译出来,以供参考. 1. 什么是 HTML HTML 是创 ...
- CentOS的网络配置(终端环境)
在虚拟机中安装了Server版的CentOS,用于做Hadoop相关的实验.Server版的CentOS默认是没有桌面环境的,eth0的网络默认也没有开启,需要我们手动启动网络. 关键配置说明 网络和 ...