hdu 1047 Integer Inquiry
来源:互联网 发布:拍照赚钱软件 编辑:程序博客网 时间:2024/06/06 03:23
Integer Inquiry View CodeView Code
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 13978 Accepted Submission(s): 3524
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.)
``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.
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.
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
11234567890123456789012345678901234567890123456789012345678901234567890123456789012345678900
Sample Output
370370367037037036703703703670
普通:
1 #include<iostream> 2 #include<stdio.h> 3 #include<string.h> 4 #include<math.h> 5 #include<stdlib.h> 6 #include<queue> 7 using namespace std; 8 int main() 9 {10 //freopen("in.txt","r",stdin);11 int n,sum[110];12 char s1[110];13 cin >> n;14 int i,j;15 while(n--)16 {17 getchar();18 memset(sum,0,sizeof(sum));19 while(scanf("%s",s1))20 {21 if(strcmp(s1,"0")==0)22 break;23 for(i=j=strlen(s1)-1;i>=0;i--)24 {25 sum[j-i]+=s1[i]-'0';26 }27 }28 int c=0,x;29 for(i=0;i<110;i++)30 {31 x=sum[i]+c;32 sum[i]=x%10;33 c=x/10;34 }35 i=100;36 while(sum[i]==0)37 i--;38 if(i<0)39 cout << "0";40 for(;i>=0;i--)41 printf("%d",sum[i]);42 printf("\n");43 if(n)44 printf("\n");45 }46 return 0;47 }
模板:
1 #include <stdio.h> 2 #include <string.h> 3 #include <stdlib.h> 4 #include <math.h> 5 #include <assert.h> 6 #include <ctype.h> 7 #include <map> 8 #include <string> 9 #include <set> 10 #include <bitset> 11 #include <utility> 12 #include <algorithm> 13 #include <vector> 14 #include <stack> 15 #include <queue> 16 #include <iostream> 17 #include <fstream> 18 #include <list> 19 using namespace std; 20 21 const int MAXL=120; 22 struct BigNum 23 { 24 int num[MAXL]; 25 int len; 26 BigNum() 27 { 28 memset(num,0,sizeof(num)); 29 } 30 }; 31 //高精度加法 32 BigNum Add(BigNum &a, BigNum &b) 33 { 34 BigNum c; 35 int i, len; 36 len = (a.len > b.len) ? a.len : b.len; 37 memset(c.num, 0, sizeof(c.num)); 38 for(i = 0; i < len; i++) 39 { 40 c.num[i] += (a.num[i]+b.num[i]); 41 if(c.num[i] >= 10) 42 { 43 c.num[i+1]++; 44 c.num[i] -= 10; 45 } 46 } 47 if(c.num[len]) 48 len++; 49 c.len = len; 50 return c; 51 } 52 void print(BigNum &a) //输出大数 53 { 54 int i; 55 for(i = a.len-1; i >= 0; i--) 56 printf("%d", a.num[i]); 57 puts(""); 58 } 59 60 void Init(BigNum &a, char *s, int &tag) //将字符串转化为大数 61 { 62 int i = 0, j = strlen(s); 63 if(s[0] == '-') 64 { 65 j--; 66 i++; 67 tag *= -1; 68 } 69 a.len = j; 70 for(; s[i] != '\0'; i++, j--) 71 a.num[j-1] = s[i]-'0'; 72 } 73 74 int main(void) 75 { 76 //freopen("in.txt","r",stdin); 77 int n; 78 scanf("%d",&n); 79 char s[MAXL]; 80 while(n--) 81 { 82 getchar(); 83 BigNum a; 84 int tag=1; 85 Init(a,"0",tag); 86 while(scanf("%s",s)) 87 { 88 BigNum b; 89 if(strcmp(s,"0")==0) 90 break; 91 int tag=1; 92 Init(b,s,tag); 93 a=Add(a,b); 94 } 95 print(a); 96 if(n) 97 printf("\n"); 98 } 99 return 0;100 }
阅读全文
0 0
- HDU 1047 Integer Inquiry
- HDU 1047 Integer Inquiry
- hdu 1047 Integer Inquiry
- hdu 1047 Integer Inquiry
- hdu 1047 Integer Inquiry
- hdu 1047 Integer Inquiry
- hdu-1047Integer Inquiry
- hdu 1047 Integer Inquiry
- HDU 1047 Integer Inquiry
- hdu 1047Integer Inquiry
- hdu 1047 Integer Inquiry
- HDU 1047 Integer Inquiry
- HDU 1047 Integer Inquiry
- HDU 1047 Integer Inquiry
- HDU 1047 Integer Inquiry
- HDU 1047 Integer Inquiry
- HDU 1047 Integer Inquiry
- HDU-1047 Integer Inquiry
- Codeforces Round #295 (Div. 2) B. Two Buttons
- 实验三 栈和队列的基本操作实现及其应用 (1)
- N! HDU 1042
- 数字图像实验一:图像合成
- 大数模板(高精度)
- hdu 1047 Integer Inquiry
- 多线程互斥锁访问算法(下)------Lamport算法(面包店算法)
- HUSTOJ(转发)
- hdu 1063 Exponentiation
- hdu 1002 A+B
- c++设计模式之桥接模式
- Go 编译sqlite3 遇到的问题 + 加快go IDE编译速度
- hdu 1753 大明A+B
- 其他类型转化成DataTable