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;
}
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】博客园社区专享云产品让利特惠,阿里云新客6.5折上折
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 用 C# 插值字符串处理器写一个 sscanf
· Java 中堆内存和栈内存上的数据分布和特点
· 开发中对象命名的一点思考
· .NET Core内存结构体系(Windows环境)底层原理浅谈
· C# 深度学习:对抗生成网络(GAN)训练头像生成模型
· 趁着过年的时候手搓了一个低代码框架
· 本地部署DeepSeek后,没有好看的交互界面怎么行!
· 为什么说在企业级应用开发中,后端往往是效率杀手?
· 用 C# 插值字符串处理器写一个 sscanf
· 乌龟冬眠箱湿度监控系统和AI辅助建议功能的实现