首页 > 后端开发 > C++ > 正文

使用堆栈在C++中反转一个数字

王林
发布: 2023-09-14 12:45:02
转载
1492人浏览过

使用堆栈在c++中反转一个数字

We are given an integer number Num as input. The goal is to find the reverse of the number using stack.

Stack:- A stack is a data structure in C++ which stores data in LIFO ( Last in First Out) manner. Major operations of stack are-:

Declaration-: stack stck; //stck is now a stack variable.

  • Finding Top using top(). Function stck.top() returns reference of top element in the stck

    立即学习C++免费学习笔记(深入)”;

  • Removing Top using pop(). Function removes topmost element from the stck

  • Adding element to top using push(). Function stck.push( value ) adds item value in stack. Value should be of type stck.

  • Check if staxk is empty using empty(). Function stck.empty() returns true if stack is empty.

Examples

Input − Num = 33267

Output − Reverse of number is: 76233

Explanation

First we will push all elements to stack

7 - 6 - 2 - 3 - 3 ← top

7 * 10000 + 6 * 1000 + 2*100 + 3*10 + 3*1 ←

= 70000 + 6000 + 200 + 30 + 3 ←

= 76233

Input − Num = 111000

Output − Reverse of number is: 111

Explanation

First we will push all elements to stack

0 - 0 - 0 - 1 - 1 - 1 ← top

0 * 100000 + 0 * 10000 + 0*1000 + 1*100 + 1*10 + 1*1 ←

= 0 + 0 + 0 + 100 + 10 + 1 ←

= 111

Approach used in the below program is as follows

In this approach we will first take remainders of input number and push to stack and reduce number by 10 until number becomes 0. In this way stack will be filled with top as first digit.

  • Take the input number Num.

  • Take empty stack for integers using stack stck.

  • Function pushDigts(int num1) takes num1 and adds it to stack with first digit on top.

  • Take rem as variable.

  • Using a while loop check if num1 is non-zero, if true then set rem=num1%10.

  • Push rem to stack.

  • Reduce num1 by 10 for 2nd digit and so on.

  • Now reverse the number using elements of stack with function revrseNum().

  • Take variables revrs, topp, temp, i.

  • While the stack is not empty

  • Take the topmost element as topp=stck.top().

  • Reduce stack using stck.pop().

  • Set temp=topp*i.

  • Add temp to revrs.

  • Increase i by i*10 in multiples of 100.

  • At the end return the reverse of the input num as revrs.

  • Print result obtained inside main.

Example

#include <bits/stdc++.h>
using namespace std;
stack <int> stck;
void pushDigts(int num1){
   int rem;
   while (num1 > 0){
      rem=num1 % 10;
      stck.push(rem);
      num1 = num1 / 10;
   }
}
int revrseNum(){
   int revrs = 0;
   int i = 1;
   int temp;
   int topp;
   while (!stck.empty()){
      topp=stck.top();
      stck.pop();
      temp=topp*i;
      revrs = revrs + temp;
      i *= 10;
   }
   return revrs;
}
int main(){
   int Num = 43556;
   pushDigts(Num);
   cout<<"Reverse of number is: "<<revrseNum();
   return 0;
}
登录后复制

输出

如果我们运行上面的代码,它将生成以下输出

Reverse of number is: 65534
登录后复制

以上就是使用堆栈在C++中反转一个数字的详细内容,更多请关注php中文网其它相关文章!

c++速学教程(入门到精通)
c++速学教程(入门到精通)

c++怎么学习?c++怎么入门?c++在哪学?c++怎么学才快?不用担心,这里为大家提供了c++速学教程(入门到精通),有需要的小伙伴保存下载就能学习啦!

下载
来源:tutorialspoint网
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
最新问题
开源免费商场系统广告
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习
PHP中文网抖音号
发现有趣的

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号