HDoj Integer Inquiry(大数)
2014-05-05 20:22
337 查看
真心要哭了。。这几天在搞大数 高精度计算 昨晚在机房敲 很快敲完了 就是过不了啊过不了 劳资都想骂脏话啊 NMB 一开始不输出前面的0啊 过不了 看discuss 百度 找了个AC的代码 找了几组测试数据 那个代码输出前面的0啊 我的妈 今天有找了个代码 不输出0啊 我的天。。。真心要被逼疯了 幸好还是AC了。。。算是有进步吧 之前的心态肯定坚持不下来啊
题意很简单 就是输入一连串数字作为一个大的整数 遇到单个0结束输出
每个CASE输出之间 输出一个空行
我的测试数据:
5
123456789012345678901234567890
123456789012345678901234567890
123456789012345678901234567890
0
000
00
0
02
088
0
9999
99
0
00
00
0
代码如下:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cstdlib>
using namespace std;
const int maxn=110;
char s[maxn];
int res[maxn];
int num[maxn];
int main()
{
freopen("test.txt","r",stdin);
int n;
cin>>n;
getchar();
while(n--)
{
memset(res,0,sizeof(res));
int m=0;
while(1)
{
gets(s);
if(strlen(s)==1&&s[0]=='0')
break;
int k=0;
for(int i=strlen(s)-1;i>=0;i--)
{
num[k++]=s[i]-'0';
//cout<<num[k-1]<<" ";
}
//cout<<endl;
m=max(m,k);
//cout<<"m1="<<m<<endl;
if(m>k)
{
for(int i=k;i<m;i++)
num[i]=0;
}
for(int i=0;i<m;i++)
{
res[i]+=num[i];
res[i+1]+=res[i]/10;
res[i]=res[i]%10;
}
//cout<<"res[m]="<<res[m]<<"res[m-1]"<<res[m-1]<<endl;
if(res[m]>0)
{
res[m]+=res[m-1]/10;
res[m-1]=res[m-1]%10;
m++;
}
}
//cout<<"m2="<<m<<endl;
// for(int i=m-1;i>=0;i--)
// cout<<res[i];
int flag=0;
int x=m-1;
for(;x>=0;x--)
{
if(res[x]>0)
flag=1;
if(flag)
cout<<res[x];
}
if(flag==0)
cout<<0;//如果输入的全是0 输出0
cout<<endl;
if(n)
cout<<endl;
}
return 0;
}
终究还是AC了 好开心。。。。
杭电ACM 2014暑期集训队——选拔安排~ |
Integer InquiryTime Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 11714 Accepted Submission(s): 2942 Problem Description One of the first users of BIT's new supercomputer was Chip Diller. He extended his exploration of powers of 3 to go from 0 to 333 and he explored taking various sums of those numbers. ``This supercomputer is great,'' remarked Chip. ``I only wish Timothy were here to see these results.'' (Chip moved to a new apartment, once one became available on the third floor of the Lemon Sky apartments on Third Street.) Input The input will consist of at most 100 lines of text, each of which contains a single VeryLongInteger. Each VeryLongInteger will be 100 or fewer characters in length, and will only contain digits (no VeryLongInteger will be negative). The final input line will contain a single zero on a line by itself. Output Your program should output the sum of the VeryLongIntegers given in the input. This problem contains multiple test cases! The first line of a multiple input is an integer N, then a blank line followed by N input blocks. Each input block is in the format indicated in the problem description. There is a blank line between input blocks. The output format consists of N output blocks. There is a blank line between output blocks. Sample Input 1 123456789012345678901234567890 123456789012345678901234567890 123456789012345678901234567890 0 Sample Output 370370367037037036703703703670 Source East Central North America 1996 Recommend |
每个CASE输出之间 输出一个空行
我的测试数据:
5
123456789012345678901234567890
123456789012345678901234567890
123456789012345678901234567890
0
000
00
0
02
088
0
9999
99
0
00
00
0
代码如下:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cstdlib>
using namespace std;
const int maxn=110;
char s[maxn];
int res[maxn];
int num[maxn];
int main()
{
freopen("test.txt","r",stdin);
int n;
cin>>n;
getchar();
while(n--)
{
memset(res,0,sizeof(res));
int m=0;
while(1)
{
gets(s);
if(strlen(s)==1&&s[0]=='0')
break;
int k=0;
for(int i=strlen(s)-1;i>=0;i--)
{
num[k++]=s[i]-'0';
//cout<<num[k-1]<<" ";
}
//cout<<endl;
m=max(m,k);
//cout<<"m1="<<m<<endl;
if(m>k)
{
for(int i=k;i<m;i++)
num[i]=0;
}
for(int i=0;i<m;i++)
{
res[i]+=num[i];
res[i+1]+=res[i]/10;
res[i]=res[i]%10;
}
//cout<<"res[m]="<<res[m]<<"res[m-1]"<<res[m-1]<<endl;
if(res[m]>0)
{
res[m]+=res[m-1]/10;
res[m-1]=res[m-1]%10;
m++;
}
}
//cout<<"m2="<<m<<endl;
// for(int i=m-1;i>=0;i--)
// cout<<res[i];
int flag=0;
int x=m-1;
for(;x>=0;x--)
{
if(res[x]>0)
flag=1;
if(flag)
cout<<res[x];
}
if(flag==0)
cout<<0;//如果输入的全是0 输出0
cout<<endl;
if(n)
cout<<endl;
}
return 0;
}
终究还是AC了 好开心。。。。
相关文章推荐
- UVA Product(大数乘法)
- 用字符串实现大数相乘
- hdu1002 大数运算
- 补码表示的大数加减乘除运算程序设计
- 杭电OJ(HDOJ)1063题:Exponentiation(大数操作——幂运算)
- 黑马程序员——java中对大数的处理
- hdu1141 (Factstone Benchmark(利用对数进行大数比较))
- WV.24-大数阶乘算法4-近似计算之二
- HDU-A Fibonacci sequence斐波那契数列-大数求和
- HDU 1047(大数相加)
- HDU Buy the Ticket(大数除法乘法要注意)
- hdu 1134 Game of Connections 【卡特兰数+大数】
- hdu 1314 只图个1314(Java大数)
- hdu5050(Java大数 + 二进制 + 最大公约数)
- hdoj1002大数a+b!!!
- LeetCode-Multiply Strings实现大数的乘法
- 大数溢出数组求最大子数组
- hdu 1023 Train Problem II 这题运用到大数相乘+大数相除+卡特兰数
- zoj 3175 Number of Containers (大数灵活题,除2改成平方根)
- [acm 1001] c++ 大数加法 乘法 幂