4.2.1 B - Adding Reversed Numbers(高精度运算)
来源:互联网 发布:数据库工程师考试科目 编辑:程序博客网 时间:2024/06/04 18:46
Description
Reversed number is a number written in arabic numerals but the order of digits is reversed. The first digit becomes last and vice versa. For example, if the main hero had 1245 strawberries in the tragedy, he has 5421 of them now. Note that all the leading zeros are omitted. That means if the number ends with a zero, the zero is lost by reversing (e.g. 1200 gives 21). Also note that the reversed number never has any trailing zeros.
ACM needs to calculate with reversed numbers. Your task is to add two reversed numbers and output their reversed sum. Of course, the result is not unique because any particular number is a reversed form of several numbers (e.g. 21 could be 12, 120 or 1200 before reversing). Thus we must assume that no zeros were lost by reversing (e.g. assume that the original number was 12).
Input
The input consists of N cases. The first line of the input contains only positive integer N. Then follow the cases. Each case consists of exactly one line with two positive integers separated by space. These are the reversed numbers you are to add.
Output
For each case, print exactly one line containing only one integer - the reversed sum of two reversed numbers. Omit any leading zeros in the output.
Sample Input
3
24 1
4358 754
305 794
Sample Output
34
1998
1
思路简单就是简单的将其放在数组中,然后在相加,注意边界为零的情况,但是在测试库里没有001,即第一个数位零的情况
#include <iostream>
#include <cstring>
#include <string> //string类型length
using namespace std;
int a[1000]={0},b[1000]={0},c[1000]={0};
int zhuanhuan(string str,bool boo)
{
int n,i,l=0, w=0;;
n=str.length();
if(boo==0)
for(i=0;i<n;i++)//适用于整数
{
if(!(w==0&&str[i]=='0'))
{
w=1;
a[l]=str[i]-'0';
l++;
}
}
else
for(i=0;i<n;i++)//适用于整数
if(!(w==0&&str[i]=='0'))
{
w=1;
b[l]=str[i]-'0';
l++;
}
return l;
}
int main()
{
int n,i,j;
string str;
cin>>n;
for(i=1;i<=n;i++)
{
memset(a,0,sizeof(int)*1000);
memset(b,0,sizeof(int)*1000);
memset(c,0,sizeof(int)*1000);
cin>>str;
int x;
x=zhuanhuan(str,0);
cin>>str;
int y;
y=zhuanhuan(str,1);
int weishu=x>y?x:y;
for(j=0;j<weishu;j++)
{
c[j]=a[j]+b[j]+c[j];
if(c[j]>9)
{
c[j]=c[j]-10;
c[j+1]++;
}
}
int ww=0;
if(c[weishu]!=0)
weishu++;
for(j=weishu-1;j>0;j--)
if(c[j]==0)
c[j]=-1;
else
break;
for(j=0;j<weishu;j++)
if(!(ww==0&&c[j]==0||c[j]==-1))
{
ww=1;
cout<<c[j];
}
cout<<endl;
}
return 0;
}
超流比代码:
运用倒置字符串函数reverse函数:
#include<iostream>
#include<algorithm>
using namespace std;
int main(void)
{
int t;
scanf("%d",&t);
while(t--)
{
int a,b;
char s1[13],s2[13];
scanf("%d%d",&a,&b);
sprintf(s1,"%d",a);
sprintf(s2,"%d",b);
reverse(s1,s1+strlen(s1));
reverse(s2,s2+strlen(s2));
sscanf(s1,"%d",&a);
sscanf(s2,"%d",&b);
sprintf(s1,"%d",a+b);
reverse(s1,s1+strlen(s1));
sscanf(s1,"%d",&a);
printf("%d\n",a);
}
return 0;
}
- 4.2.1 B - Adding Reversed Numbers(高精度运算)
- B - Adding Reversed Numbers(4.2.1)
- (数组的应用二:高精度运算4.2.1)POJ 1504 Adding Reversed Numbers(将一个数取反)
- (高精度) Adding Reversed Numbers (P1504)
- B - Adding Reversed Numbers
- B - Adding Reversed Numbers
- 《数据结构编程实验》 4.2.1Adding Reversed Numbers
- POJ 1504 Adding Reversed Numbers(水~)
- SPOJ42 - Adding Reversed Numbers (简单题)
- 1504 Adding Reversed Numbers
- poj1504 Adding Reversed Numbers
- Adding Reversed Numbers
- Adding Reversed Numbers
- Adding Reversed Numbers
- Adding Reversed Numbers
- 2210. Adding Reversed Numbers
- zoj_2001 Adding Reversed Numbers
- Adding Reversed Numbers(0058)
- 用WPF控件MediaElement创建简易播放器(视频区域、播放控制区域、播放列表、循环播放)
- 八个常用的并行设计模式
- 防止ViewPager中的Fragment被销毁的方法
- 【openstack】学习笔记一
- 如何做好需求收集
- 4.2.1 B - Adding Reversed Numbers(高精度运算)
- 网络棋牌游戏创造快乐心情
- Struts2 标签
- Leetcode刷题日记 -- Longest Substring Without Repeating Characters
- 锐化空间滤波
- SAS9.3 64位版Win7安装指引
- android手机开发常用开源组件
- 图片自适应大小@百度编辑器
- Nginx主主负载均衡架构