POJ 1503 Integer Inquiry 解题报告
2007-06-06 02:28
453 查看
类型:高精度加法
总结几个较舒服的做法:
1。输入字符串,在转换成整数的同时把串反转, 这样便于计算。
如输入:1234
存储:a[0]=4, a[1]=3, .....
2。把字符串长度存在首个元素里,如a[0]。
3。从MAX递减寻找非0数,开始输出,不用记录结果长度。
这题wa了n次,只因为数组开小了,真不知道数据不符合题意也往上搬,害得人浪费时间。所以以后实在不行,可以试试是不是MAX设小了
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
#include<stdio.h>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
#include<string.h>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
#define MAX 104
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
char a[MAX][MAX];
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
char s[MAX];
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
int main()
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
int i, j, num, maxLen, carry;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
for(i=0; scanf("%s", s) && !(s[0]=='0' && s[1]==0); i++)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
a[i][0] = strlen(s);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
for(j=1; j<=a[i][0]; j++)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
a[i][j] = s[a[i][0]-j] - '0';
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
num = i;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
for(i=1; i<num; i++)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
carry = 0;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
for(j=1; j<=MAX-2; j++)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
a[0][j] += carry + a[i][j];
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
carry = a[0][j] / 10;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
a[0][j] %= 10;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
i = MAX-1;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
while(a[0][i] == 0)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
i--;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
for(; i>=1; i--)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
printf("%d", a[0][i]);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
printf("/n");
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
return 0;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
}
总结几个较舒服的做法:
1。输入字符串,在转换成整数的同时把串反转, 这样便于计算。
如输入:1234
存储:a[0]=4, a[1]=3, .....
2。把字符串长度存在首个元素里,如a[0]。
3。从MAX递减寻找非0数,开始输出,不用记录结果长度。
这题wa了n次,只因为数组开小了,真不知道数据不符合题意也往上搬,害得人浪费时间。所以以后实在不行,可以试试是不是MAX设小了
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
#include<stdio.h>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
#include<string.h>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
#define MAX 104
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
char a[MAX][MAX];
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
char s[MAX];
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
int main()
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
int i, j, num, maxLen, carry;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
for(i=0; scanf("%s", s) && !(s[0]=='0' && s[1]==0); i++)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
a[i][0] = strlen(s);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
for(j=1; j<=a[i][0]; j++)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
a[i][j] = s[a[i][0]-j] - '0';
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
num = i;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
for(i=1; i<num; i++)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
carry = 0;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
for(j=1; j<=MAX-2; j++)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
a[0][j] += carry + a[i][j];
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
carry = a[0][j] / 10;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
a[0][j] %= 10;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
i = MAX-1;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
while(a[0][i] == 0)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
i--;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
for(; i>=1; i--)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
printf("%d", a[0][i]);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
printf("/n");
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
return 0;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
}
相关文章推荐
- 【原】 POJ 1503 Integer Inquiry 大整数加法 解题报告
- POJ-1503-Integer Inquiry-大数加法
- 杭电OJ题 1047 Integer Inquiry 解题报告
- poj 1503 Integer Inquiry
- Integer Inquiry--POJ 1503
- POJ 1503 Integer Inquiry
- POJ1503_Integer Inquiry
- POJ1503,Integer Inquiry,与hdu1047一样。下面加上另外几种方法
- POJ 1503 Integer Inquiry 简单大数相加
- POJ 1503 Integer Inquiry 【大数加法】
- POJ 1503 Integer Inquiry
- POJ 1503 Integer Inquiry
- POJ 1503 Integer Inquiry(高精度运算)
- POJ 1503 Integer Inquiry(高精度)
- POJ1503 UVA424 UVALive5399 Integer Inquiry【大数】
- POJ-1503 Integer Inquiry
- POJ 1503 Integer Inquiry 简单大数相加
- UVa 424 - Integer Inquiry解题报告
- POJ 1503 Integer Inquiry 大数 难度:0
- POJ 1503 Integer Inquiry