UVA424 Integer Inquiry
2015-04-23 10:27
381 查看
题目链接:http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=19484
题意:给大数,问他们的和
思路:主要是练一下高精度。发现一个函数调用如果参数是数组,直接就会传地址而不需要&
源码:
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <iostream>
using namespace std;
#define gmax(a,b) a>b?a:b
int const MAXN = 100+10;
char ans[MAXN];
struct D
{
char data[MAXN];
int len;
}d[MAXN];
void change(char ss[],int len)
{
int i,j;
for(i=0; i<len/2; i++){
char temp = ss[i];
ss[i] = ss[len-1-i];
ss[len-1-i] = temp;
}
}
int main()
{
int tot = 0,maxl = 0;;
while(scanf("%s",d[tot].data)!=EOF && d[tot].data[0] != '0'){
d[tot].len = strlen(d[tot].data);
maxl = gmax(d[tot].len,maxl);
change(d[tot].data,d[tot].len);
tot++;
}
int carry = 0;
int i,j,tt;
for(j=0; j<maxl; j++){
tt = 0;
for(i=0; i<tot; i++){
if(j>d[i].len-1)
continue;
tt += d[i].data[j] - '0';
}
tt += carry;
ans[j] = '0' + tt%10;
carry = tt/10;
}
while(carry){
ans[j++] = '0' + carry%10;
carry /= 10;
}
change(ans,j);
printf("%s\n",ans);
return 0;
}
题意:给大数,问他们的和
思路:主要是练一下高精度。发现一个函数调用如果参数是数组,直接就会传地址而不需要&
源码:
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <iostream>
using namespace std;
#define gmax(a,b) a>b?a:b
int const MAXN = 100+10;
char ans[MAXN];
struct D
{
char data[MAXN];
int len;
}d[MAXN];
void change(char ss[],int len)
{
int i,j;
for(i=0; i<len/2; i++){
char temp = ss[i];
ss[i] = ss[len-1-i];
ss[len-1-i] = temp;
}
}
int main()
{
int tot = 0,maxl = 0;;
while(scanf("%s",d[tot].data)!=EOF && d[tot].data[0] != '0'){
d[tot].len = strlen(d[tot].data);
maxl = gmax(d[tot].len,maxl);
change(d[tot].data,d[tot].len);
tot++;
}
int carry = 0;
int i,j,tt;
for(j=0; j<maxl; j++){
tt = 0;
for(i=0; i<tot; i++){
if(j>d[i].len-1)
continue;
tt += d[i].data[j] - '0';
}
tt += carry;
ans[j] = '0' + tt%10;
carry = tt/10;
}
while(carry){
ans[j++] = '0' + carry%10;
carry /= 10;
}
change(ans,j);
printf("%s\n",ans);
return 0;
}
相关文章推荐
- UVa:424 - Integer Inquiry
- UVa 424 - Integer Inquiry
- uva 424 - Integer Inquiry
- UVa 424 - Integer Inquiry
- UVa 424 - Integer Inquiry
- UVA - 424 - Integer Inquiry
- uva-424 - Integer Inquiry
- UVa 424 Integer Inquiry
- UVA bignumber 424 integer inquiry
- UVa 424 - Integer Inquiry
- UVa-424-Integer Inquiry
- UVA - 424 Integer Inquiry
- UVa-424-Integer Inquiry
- UVa 424 Integer Inquiry
- UVA 424 Integer Inquiry
- uva 424 - Integer Inquiry
- uva 424 - Integer Inquiry
- UVa 424 - Integer Inquiry
- UVA - 424 Integer Inquiry
- UVa 424 Integer Inquiry