12翻一下是21,34翻一下是43,12+34是46,46翻一下是64,现在又任意两个正整数,问他们两个数反转的和是否等于两个数的和的反转。
#include<cstdio> int reverse(int n) { int tmp = n; int res = 0; while (tmp) { res = 10 * res + tmp % 10; tmp /= 10; } return res; } int main() { int a = 0, b = 0; while (~scanf("%d%d", &a, &b)) { if (reverse(a + b) == reverse(a) + reverse(b)) { printf("%d\n", a + b); } else { printf("NO\n"); } } return 0; }
#include<bits/stdc++.h> using namespace std; int main(){ string num1, num2, ans1; while(cin >> num1 >> num2){ string num1Reverse = num1; string num2Reverse = num2; reverse(num1Reverse.begin(), num1Reverse.end()); reverse(num2Reverse.begin(), num2Reverse.end()); int ans1 = stoi(num1Reverse) + stoi(num2Reverse); int ans2 = stoi(num1) + stoi(num2); string temp = to_string(ans2); reverse(temp.begin(), temp.end()); ans2 = stoi(temp); if(ans1 == ans2){ cout << stoi(num1) + stoi(num2) << endl; }else{ cout << "NO" << endl; } } return 0; }
#include<stdio.h> #include<math.h> int fanzhuan(int n) { int i,sum[6],key=0,num=0;//num数组下标,也是数组个数,key代表翻转之后的数字 while(n!=0) { sum[num]=n%10;//反序存放在数组中 n=n/10; num++; } for(i=0;i<num;i++) key+=sum[i]*pow(10,num-1-i); return key; } int main() { int a,b,p,q;//p代表两个数的和,q代表两个数和的翻转 scanf("%d%d",&a,&b); p=fanzhuan(a+b); q=fanzhuan(a)+fanzhuan(b); if(p==q) printf("%d\n",a+b); else printf("NO\n"); }
import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); while (scanner.hasNext()){ // 12翻一下是21,34翻一下是43,12+34是46,46翻一下是64,现在又任意两个正整数,问他们两个数反转的和是否等于两个数的和的反转。 int i = scanner.nextInt(); //12 int j = scanner.nextInt(); ///34 int i1 = getReverse(i); //21 int i2 = getReverse(j); //43 int sum1 = i+j; //46 int sum2 = i1+i2; //64 System.out.println(getReverse(sum1)==sum2?sum1:"NO"); } } static int getReverse(int i){ return Integer.parseInt(new StringBuilder(String.valueOf(i)).reverse().toString()); } }
#include<iostream> using namespace std; int reverse(int n){ char a[5]; int temp=n,count=0; while(temp>0){ a[count++]=temp%10; temp/=10; } for(int i=0;i<count/2;i++){ temp=a[i]; a[count-1-i]=a[i]; a[i]=a[count-1-i]; } int total=0; for(int i=0;i<count;i++) total=total*10+a[i]; return total; } int main(){ int a,b; while(cin>>a>>b){ int r_a=reverse(a); int r_b=reverse(b); int sum_r=reverse(a+b); int r_sum=r_a+r_b; if(r_sum==sum_r) cout<<a+b<<endl; else cout<<"NO"<<endl; } }
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <math.h>
int turn(int x)
{
int a[5];
//万位
a[4]=x/10000 ;
//千位
a[3]=(x%10000)/1000;
//百位
a[2]=(x%1000)/100;
//十位
a[1]=(x%100)/10;
//个位
a[0]=(x%10);
if(a[4]!=0) //五位数,反转之后依旧是四位数
{
return a[0]*10000+a[1]*1000+a[2]*100+a[3]*10+a[4];
}
else if(a[3]!=0) //四位数,反转之后依旧是四位数
{
return a[0]*1000+a[1]*100+a[2]*10+a[3];
}
else if(a[2]!=0)
{
return a[0]*100+a[1]*10+a[2];
}
else if(a[1]!=0)
{
return a[0]*10+a[1];
}
else
{
return a[0];
}
}
int main()
{
int a,b;
while(scanf("%d %d",&a,&b)!=EOF)
{
if(turn(a)+turn(b)==turn(a+b))
{
printf("%d\n",a+b);
}
else
{
printf("NO\n");
}
}
return 0;
}
#include<stdio.h> int reverse(int num){ int sum=0; while(num){ sum= sum*10+num%10; num/=10; } return sum; } int main() { int num1,num2,i; scanf("%d",&i); for(int j=0;j<i;j++){ scanf("%d%d",&num1,&num2); if(reverse(num1+num2)==reverse(num1)+reverse(num2))printf("%d\n",num1+num2); else printf("NO\n"); } return 0; }
#include<stdio.h> int main() { int x,y; while(scanf("%d %d",&x,&y)!=EOF) { int flag=1; int a=x,b=y; while(a!=0 && b!=0) { if(a%10+b%10>=10) { flag=0;break; } a=a/10,b=b/10; } if(a!=0 || b!=0) flag=0; if(flag) printf("%d\n",x+y); else printf("NO\n"); } }不用反转的算法:位数不同必为NO,位数相同如果每位相加有进位就为NO,其他输出
def fun1(a): arr = [] while a>0: t = a % 10 a = int(a / 10) arr.append(t) return arr def fun2(a): n = len(a) - 1 s = 0 for i in a: s += i*(10**n) n -= 1 return s def resverse(a ,b): arr, brr = [], [] arrv = fun1(a) brrv = fun1(b) s1 = fun2(arrv) s2 = fun2(brrv) s = s1 + s2 srrv = fun1(a+b) sv = fun2(srrv) if sv == s: print(a+b) else: print('NO') while True: try: a, b = map(int, input().split()) resverse(a, b) except: break
#include<stdio.h> #include<string> #include<algorithm> using namespace std; int main(){ int a,b; scanf("%d%d",&a,&b); int res = a+b; int ra=0,rb=0; while(a!=0){ ra = ra*10+a%10; a=a/10; } while(b!=0){ rb = rb*10+b%10; b = b/10; } int rres = ra+rb; string s1 = to_string(res); string s2 = to_string(rres); reverse(s2.begin(),s2.end()); if(s1==s2){ printf("%s\n",s1.c_str()); }else{ printf("NO\n"); } }
#include <iostream> using namespace std; int reverse(int n) { //求数n的反转 int rev = 0; while (n) { rev = rev * 10 + n % 10; n /= 10; } return rev; } int main() { int a, b; while (cin >> a >> b) { reverse(a) + reverse(b) == reverse(a + b) ? cout << a + b << endl : cout << "NO" << endl; } return 0; }
#include <iostream> #include <cstring> #include <algorithm> #include <string> using namespace std; int rev(int n){//反转int型数字 int sum=0; while(n>0){ sum=10*sum+n%10; n/=10; } return sum; } int main(){ int a,b; while(cin>>a>>b){ int oldstr,newstr; oldstr=a+b; a=rev(a); b=rev(b); newstr=a+b; newstr=rev(newstr); if(newstr==oldstr){ cout<<oldstr<<endl; } else{ cout<<"NO"<<endl; } } return 0; }