HDU:Integer Inquiry
#include"stdio.h"
#include"stdlib.h"
#include"string.h"
#define N 105
int a[N];
int main()
{
int i,j,n,n1,flag;
char ch[N];
scanf("%d",&n);
while(n--)
{
memset(a,,sizeof(a)); //数组初始化为零
while(scanf("%s",ch),ch[]!='') //输入零结束
{
n1=strlen(ch);
for(i=n1-,j=;i>=;i--)
{
a[j++]+=ch[i]-'';
a[j]+=a[j-]/; //进位
a[j-]%=; //取余
}
}
flag=;
for(i=N-;i>;i--)
{
if(flag)
printf("%d",a[i]);
else if(a[i])
{
printf("%d",a[i]);
flag=;
}
}
printf("%d\n",a[]); //输入0时应输出0,错了n多次
if(n) //输出块之间有空行
printf("\n");
}
return ;
}
改版 为什么一直WA
#include"stdio.h"
#include"stdlib.h"
#include"string.h"
#define N 105
int a[N],b[N];
int main()
{
int i,j,n,n1,flag;
char ch[N];
scanf("%d",&n);
while(n--)
{
memset(a,,sizeof(a)); //数组初始化为零
while(scanf("%s",ch),ch[]!='') //输入零结束
{
n1=strlen(ch);
memset(b,,sizeof(b));
for(i=,j=n1-;j>=;j--,i++)
b[i]=ch[j]-'';
int h=;
int m;
for(i=;i<n1;i++)
{
m=a[i]+b[i]+h;
a[i]=m%;
h=m/;
}
while(h)
{
a[i]=h%;
n1++;
h=h/;
}
}
flag=;
for(i=N-;i>;i--)
{
if(flag)
printf("%d",a[i]);
else if(a[i])
{
printf("%d",a[i]);
flag=;
}
}
printf("%d\n",a[]); //输入0时应输出0,错了n多次
if(n) //输出块之间有空行
printf("\n");
}
return ;
}
C++版本
#include<iostream>
#include<cstring>
using namespace std;
#define N 110
int main()
{
int t;
cin>>t;
while(t--)
{
int sum[N]={0}; char str[N];
while(cin>>str&&str[0]!='0')
{
int num[N]={0};
int len=strlen(str);
for(int i=0;i<len;i++)
num[i]=str[len-1-i]-'0';
for(int i=0;i<N;i++)
{
sum[i]+=num[i];
if(sum[i]>9)
{
sum[i]-=10;
sum[i+1]+=1;
}
//sum[i+1]+=(sum[i]+num[i])/10;
//sum[i]=(sum[i]+num[i])%10;
}
}
int k=N-1;
while(sum[k]==0) k--;
if(k<0)
cout<<0;
else
for(;k>=0;k--)
cout<<sum[k];
cout<<endl;
if(t)
cout<<endl;
} return 0;
}
#include <iostream>
#include <cstring>
#include <string>
#include <cmath>
using namespace std;
const int N=;
int main()
{
int a[N],b[N],c,d,ou,kl;
string s1,s2;
getline(cin,s1);
c=s1.length();
ou=;
for (int i=;i<N;i++)//把s1h装到a[100]里
{
a[N--i]=s1[c--i]-'';
if(c--i<)
{
a[N--i]=;
}
}
while(getline(cin,s2)&&s2!="")
{
d=s2.length();
for (int k=;k<N;k++)//把s2装到b[100]里
{
b[N--k]=s2[d--k]-'';
if(d--k<)
{
b[N--k]=;
}
}
for (int l=N-;l>;l--)//做加法
{
a[l]+=b[l];
if(a[l]>=)
{
a[l]-=;
a[l-]++;
}
}
}
for (int g=;g<N;g++)//从不为零的位置开始输出到最后一位
{
ou+=a[g];
if(ou!=)
cout<<a[g];
}
cout<<endl; return ;
}
HDU:Integer Inquiry的更多相关文章
- hdu 1047 Integer Inquiry
题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=1047 Integer Inquiry Description One of the first use ...
- hdu acm-1047 Integer Inquiry(大数相加)
Integer Inquiry Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)T ...
- hdu 1047 Integer Inquiry(高精度数)
Problem Description Oneof the first users of BIT's new supercomputer was Chip Diller. He extended hi ...
- 九度OJ 1119:Integer Inquiry(整数相加) (大数运算)
时间限制:1 秒 内存限制:32 兆 特殊判题:否 提交:679 解决:357 题目描述: One of the first users of BIT's new supercomputer was ...
- hdu 1047 Integer Inquiry(大数)
题意:整数大数加法 思路:大数模板 #include<iostream> #include<stdio.h> #include<stdlib.h> #include ...
- HDU 1047 Integer Inquiry 大数相加 string解法
本题就是大数相加,题目都不用看了. 只是注意的就是HDU的肯爹输出,好几次presentation error了. 还有个特殊情况,就是会有空数据的输入case. #include <stdio ...
- HDU 1047 Integer Inquiry( 高精度加法水 )
链接:传送门 思路:高精度水题 /************************************************************************* > File ...
- Poj 1503 Integer Inquiry
1.链接地址: http://poj.org/problem?id=1503 2.题目: Integer Inquiry Time Limit: 1000MS Memory Limit: 1000 ...
- Integer Inquiry【大数的加法举例】
Integer Inquiry Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 27730 Accepted: 10764 ...
随机推荐
- cocos2dx 3.x(一张背景图利用定时器实现循环轮播)
// // MainScene.hpp // helloworld // // Created by apple on 16/9/19. // // #ifndef MainScene_hpp #de ...
- SQL exist
EXISTS = IN,意思相同不过语法上有点点区别,好像使用IN效率要差点,应该是不会执行索引的原因SELECT ID,NAME FROM A WHERE ID IN (SELECT AID FRO ...
- myeclipse里装svn插件
找到myeclipse的安装目录下的dropins文件夹,在该文件夹中建立一个新的文件夹SVN,将下载的site-1.8.16.zip解压下的东西放在该文件夹中.再打开myeclipse,import ...
- ajax异步提交文件
首先 下载jquery和jquery.form.js http://malsup.com/jquery/form/ <script type="text/javascript&qu ...
- MongoDB(一):安装
安装 从度娘上搜索MongoDB,找到官网地址:https://www.mongodb.com 找到下载中心地址:https://www.mongodb.com/download-center 我下载 ...
- iOS6:在你的企业系统中支持Passbook
前言 这是一篇翻译,感谢Jonathan Tang. 原文地址:iOS 6 Tutorial: Supporting Passbook within Your Enterprise Systems 正 ...
- Velocity(3)——字面值和转义
货币符号: $是美元的符号,在文本中出现的"$2.5"这样的字符串,是不会被Velocity解释为一个变量或者一个属性的,因为2.5或者2或者5都不是一个VTL标识符. 转义: 假 ...
- C++Primer 第五章
//1.表达式语句的作用:执行表达式并丢弃求值结果 ; value + ; //执行,并丢弃结果 //2.复合语句是指用花括号括起来的语句和声明的序列,复合语句称为块.一个块就是一个作用域.块不以分号 ...
- curl命令使用(转)
转自:http://www.cnblogs.com/sunada2005/p/3829772.html curl命令可以用来构造http请求.参数有很多,常用的参数如下: 通用语法:curl [opt ...
- [原创]java WEB学习笔记59:Struts2学习之路---OGNL,值栈,读取对象栈中的对象的属性,读取 Context Map 里的对象的属性,调用字段和方法,数组,list,map
本博客的目的:①总结自己的学习过程,相当于学习笔记 ②将自己的经验分享给大家,相互学习,互相交流,不可商用 内容难免出现问题,欢迎指正,交流,探讨,可以留言,也可以通过以下方式联系. 本人互联网技术爱 ...