添加倒数

时间:2018-08-26 12:19:30

标签: c++ numbers

给出2个数字A和B,取反2个数字,相加并打印总和。

#include <stdio.h>
#include <iostream>
#include <string>
using namespace std;

unsigned reverse(unsigned num){
  unsigned reversed = 0;
  while(num){
    reversed = reversed*10+num%10;
    num = num/10;
  }
  return reversed;
}

int main(){
    int n;
    scanf("%d",&n);
    while(n--){
        unsigned num1,num2;
        scanf("%u %u",&num1,&num2);
        printf("%u\n",reverse(reverse(num1)+reverse(num2)));
    }
    return 0;
}

输出:

113 3231 624

预期产量

311 1323 426

请解决.......................

1 个答案:

答案 0 :(得分:1)

问题在这里。

printf("%u\n",reverse(reverse(num1)+reverse(num2)));

应该是

printf("%u\n",reverse(num1)+reverse(num2))

您将再次拨打reverse上的反向电话号码sum