題目分析:
使用隊列存放每一位的數字,然後輸出並計算得到反轉數值,從而實現數字反轉。
源代碼:
[cpp]
#include<iostream>
#include<queue>
using namespace std;
int main()
{
int n;
cin>>n;
int m = 1;
while (m <= n) //控制行數循環
{
int a,b;
cin>>a>>b;
int sum = 0,temp = 0;
queue<int> a1,b1,sum1;
sum = a + b;
temp = sum;
while (sum > 0)
{
sum1.push(sum % 10);
sum = sum / 10;
}
while (a > 0) //將a中每個數字入隊列
{
a1.push(a % 10);
a = a / 10;
}
while (b > 0) //將b中每個數字入隊列
{
b1.push(b % 10);
b = b / 10;
}
a = 0, b =0, sum = 0;
for (int k = 0; k < sum1.size(); k++)
{
sum = sum * 10 + sum1.front();
sum1.pop();
}
for (int i = 0; i < a1.size(); i++)
{
a = a * 10 + a1.front();
a1.pop();
}
for (int j = 0; j < b1.size(); j++)
{
b = b * 10 + b1.front();
b1.pop();
}
if (sum == a + b)
{
cout<<temp<<endl;;
}
else
{
cout<<"NO"<<endl;
}
m ++;
}
return 0;
}
#include<iostream>
#include<queue>
using namespace std;
int main()
{
int n;
cin>>n;
int m = 1;
while (m <= n) //控制行數循環
{
int a,b;
cin>>a>>b;
int sum = 0,temp = 0;
queue<int> a1,b1,sum1;
sum = a + b;
temp = sum;
while (sum > 0)
{
sum1.push(sum % 10);
sum = sum / 10;
}
while (a > 0) //將a中每個數字入隊列
{
a1.push(a % 10);
a = a / 10;
}
while (b > 0) //將b中每個數字入隊列
{
b1.push(b % 10);
b = b / 10;
}
a = 0, b =0, sum = 0;
for (int k = 0; k < sum1.size(); k++)
{
sum = sum * 10 + sum1.front();
sum1.pop();
}
for (int i = 0; i < a1.size(); i++)
{
a = a * 10 + a1.front();
a1.pop();
}
for (int j = 0; j < b1.size(); j++)
{
b = b * 10 + b1.front();
b1.pop();
}
if (sum == a + b)
{
cout<<temp<<endl;;
}
else
{
cout<<"NO"<<endl;
}
m ++;
}
return 0;
}