题目1089:数字反转 (分别用C++/Java实现)

来源:互联网 发布:淘宝如何寻找货源 编辑:程序博客网 时间:2024/06/14 10:29
题目描述:

    12翻一下是21,34翻一下是43,12+34是46,46翻一下是64,现在又任意两个正整数,问他们两个数反转的和是否等于两个数的和的反转。

输入:

    第一行一个正整数表示测试数据的个数n。
    只有n行,每行两个正整数a和b(0<a,b<=10000)。

输出:

    如果满足题目的要求输出a+b的值,否则输出NO。

样例输入:
212 3499 1
样例输出:
46NO
来源:
#include <iostream>using namespace std;int r(int a){    int temp=0;    int x;    while (a)    {        temp=temp*10+a%10;        a/=10;    }    return temp;}int main(){    int n;    int a[10000],b[100000];    int i,j;    while (cin>>n)    {        for (i=0;i<n;i++)        {            cin>>a[i]>>b[i];        }        for(j=0;j<i;j++)        {            if (a[j]+b[j]==r(r(a[j])+r(b[j])))            {                cout<<a[j]+b[j]<<endl;            }            else                cout<<"NO"<<endl;        }    }    return 1;}/**************************************************************    Problem: 1089    User: Carvin    Language: C++    Result: Accepted    Time:0 ms    Memory:1876 kb****************************************************************/
Java代码:
<pre name="code" class="java">import java.util.Scanner; public class Main{    public static void main(String args[])    {        Scanner cin =new Scanner(System.in);        int []a=new int[1000];        int []b=new int[1000];        int []sum=new int[1000];        int i=0,N;        while(cin.hasNext())        {            N=cin.nextInt();            for(i=0;i<N;i++)            {                a[i]=cin.nextInt();                b[i]=cin.nextInt();                sum[i]=a[i]+b[i];            }            for(i=0;i<N;i++)            {                if(sum[i]==a(a(a[i])+a(b[i])))                {                    System.out.println(sum[i]);                }                else {                    System.out.println("NO");                }            }                     }    }  public static int a(int x){    int temp=0;    while(x!=0)    {        temp=temp*10+x%10;        x/=10;    }    return temp;}}/**************************************************************    Problem: 1089    User: Carvin    Language: Java    Result: Accepted    Time:80 ms    Memory:15472 kb****************************************************************/




0 0
原创粉丝点击