POJ 1504 Adding Reversed Numbers

/*Sample Input

 

 3

 24 1

 4358 754

 305 

 

 Sample Output

 

 34

 1998

 */

值得总结的几点就是:

1、atoi函数将字符串转化为整型数字(类似于强制转换)

2、sprintf函数的用法,比如sprintf(res,"%d",i+j);

3、这种将字符串逆置的方法应该是最简洁的

 

 

 

#include<iostream>

 

#include <stdio.h>

 

#include <stdlib.h>

 

#include <string.h>

 

using namespace std;

 

void toreverse(char ch[])

 

{

 

    int i;

 

    int n=(int)strlen(ch);

 

    char temp;

 

    for(i=0;i<n/2;i++)

 

    {

 

        temp=ch[i];

 

        ch[i]=ch[n-1-i];

 

        ch[n-1-i]=temp;

 

    }

 

}

 

 

 

int main()

 

{

 

    int N,i,j;

 

    char res[10],a[10],b[10];

 

    cin>>N;

 

    while(N--)

 

    {

 

        cin>>a>>b;

 

        toreverse(a);

 

        toreverse(b);

 

        i = atoi(a);//atoi函数将字符串转化为数字(int

 

         j = atoi(b);

 

        sprintf(res,"%d",i+j);

 

        toreverse(res);

 

        printf("%d\n",atoi((char *)res));

 

    }

 

    return 0;

 

}

 

      

 

posted @ 2017-01-20 20:08  女王公园的八神  阅读(135)  评论(0编辑  收藏  举报