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 ...
随机推荐
- Velocity(3)——字面值和转义
货币符号: $是美元的符号,在文本中出现的"$2.5"这样的字符串,是不会被Velocity解释为一个变量或者一个属性的,因为2.5或者2或者5都不是一个VTL标识符. 转义: 假 ...
- mysql 查看是否存在某一张表
判断表是否存在 SELECT table_name FROM information_schema.TABLES WHERE table_name ='yourname'; 或者 SHOW TABLE ...
- C++Primer 第十七章
//1.当我们希望将一些数据组合成单一对象,但又不想麻烦地定义一个新的数据结构来表示这些数据的时候,tuple非常有用.其和其伴随类型和函数都定义在头文件tuple中,声明在命名空间std中. tup ...
- hdu 2528 Area
2014-07-30 http://acm.hdu.edu.cn/showproblem.php?pid=2528解题思路: 求多边形被一条直线分成两部分的面积分别是多少.因为题目给的直线一定能把多边 ...
- C++之路进阶——codevs1204(寻找子串位置)
1204 寻找子串位置 时间限制: 1 s 空间限制: 128000 KB 题目等级 : 青铜 Bronze 题目描述 Description 给出字符串a和字符串b,保证b是a的一个子 ...
- C++之路进阶——codevs1036(商务旅行)
1036 商务旅行 题目描述 Description 某首都城市的商人要经常到各城镇去做生意,他们按自己的路线去做,目的是为了更好的节约时间. 假设有N个城镇,首都编号为1,商人从首都出发,其他各城镇 ...
- ligerui_实际项目_001:利用ligerLayout、ligerAccordion实现可折叠的菜单效果
效果:利用ligerLayout.ligerAccordion实现可折叠的菜单效果 可能用到的js.css.images等,可到官网下载: 第01步:引入相应的文件 <head><l ...
- 异常:Struts:org.springframework.beans.factory.CannotLoadBeanClassException: Cannot find BasicDataSource
org.springframework.beans.factory.CannotLoadBeanClassException: Cannot find class [org.apache.common ...
- js获取url的参数值
var match = new RegExp('[?&]voucherSn=([^&]*)').exec("http://m.v3beta.tootoo.cn/index.p ...
- Hibernate开始上手总结
1,导入hibernate 的jar包,c3p0jar包 2,创建和表关联的实体类,创建关联实体类的配置文件 package com.entity; public class News { priva ...