当前位置:编程学习 > C/C++ >>

九度OJ 1089 数字反转

 


题目分析:

使用队列存放每一位的数字,然后输出并计算得到反转数值,从而实现数字反转。

 


源代码:


[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;
}

 

补充:软件开发 , C++ ,
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,