0

0

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

王林

王林

发布时间:2023-09-14 12:45:02

|

1565人浏览过

|

来源于tutorialspoint

转载

使用堆栈在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 ←

知识画家
知识画家

AI交互知识生成引擎,一句话生成知识视频、动画和应用

下载

= 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 
using namespace std;
stack  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: "<

输出

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

Reverse of number is: 65534

热门AI工具

更多
DeepSeek
DeepSeek

幻方量化公司旗下的开源大模型平台

豆包大模型
豆包大模型

字节跳动自主研发的一系列大型语言模型

通义千问
通义千问

阿里巴巴推出的全能AI助手

腾讯元宝
腾讯元宝

腾讯混元平台推出的AI助手

文心一言
文心一言

文心一言是百度开发的AI聊天机器人,通过对话可以生成各种形式的内容。

讯飞写作
讯飞写作

基于讯飞星火大模型的AI写作工具,可以快速生成新闻稿件、品宣文案、工作总结、心得体会等各种文文稿

即梦AI
即梦AI

一站式AI创作平台,免费AI图片和视频生成。

ChatGPT
ChatGPT

最最强大的AI聊天机器人程序,ChatGPT不单是聊天机器人,还能进行撰写邮件、视频脚本、文案、翻译、代码等任务。

相关专题

更多
python中print函数的用法
python中print函数的用法

python中print函数的语法是“print(value1, value2, ..., sep=' ', end=' ', file=sys.stdout, flush=False)”。本专题为大家提供print相关的文章、下载、课程内容,供大家免费下载体验。

186

2023.09.27

if什么意思
if什么意思

if的意思是“如果”的条件。它是一个用于引导条件语句的关键词,用于根据特定条件的真假情况来执行不同的代码块。本专题提供if什么意思的相关文章,供大家免费阅读。

779

2023.08.22

while的用法
while的用法

while的用法是“while 条件: 代码块”,条件是一个表达式,当条件为真时,执行代码块,然后再次判断条件是否为真,如果为真则继续执行代码块,直到条件为假为止。本专题为大家提供while相关的文章、下载、课程内容,供大家免费下载体验。

97

2023.09.25

string转int
string转int

在编程中,我们经常会遇到需要将字符串(str)转换为整数(int)的情况。这可能是因为我们需要对字符串进行数值计算,或者需要将用户输入的字符串转换为整数进行处理。php中文网给大家带来了相关的教程以及文章,欢迎大家前来学习阅读。

463

2023.08.02

int占多少字节
int占多少字节

int占4个字节,意味着一个int变量可以存储范围在-2,147,483,648到2,147,483,647之间的整数值,在某些情况下也可能是2个字节或8个字节,int是一种常用的数据类型,用于表示整数,需要根据具体情况选择合适的数据类型,以确保程序的正确性和性能。本专题为大家提供相关的文章、下载、课程内容,供大家免费下载体验。

544

2024.08.29

c++怎么把double转成int
c++怎么把double转成int

本专题整合了 c++ double相关教程,阅读专题下面的文章了解更多详细内容。

113

2025.08.29

C++中int的含义
C++中int的含义

本专题整合了C++中int相关内容,阅读专题下面的文章了解更多详细内容。

200

2025.08.29

堆和栈的区别
堆和栈的区别

堆和栈的区别:1、内存分配方式不同;2、大小不同;3、数据访问方式不同;4、数据的生命周期。本专题为大家提供堆和栈的区别的相关的文章、下载、课程内容,供大家免费下载体验。

397

2023.07.18

C++ 设计模式与软件架构
C++ 设计模式与软件架构

本专题深入讲解 C++ 中的常见设计模式与架构优化,包括单例模式、工厂模式、观察者模式、策略模式、命令模式等,结合实际案例展示如何在 C++ 项目中应用这些模式提升代码可维护性与扩展性。通过案例分析,帮助开发者掌握 如何运用设计模式构建高质量的软件架构,提升系统的灵活性与可扩展性。

8

2026.01.30

热门下载

更多
网站特效
/
网站源码
/
网站素材
/
前端模板

精品课程

更多
相关推荐
/
热门推荐
/
最新课程
CSS3 教程
CSS3 教程

共18课时 | 5万人学习

PostgreSQL 教程
PostgreSQL 教程

共48课时 | 8.1万人学习

Django 教程
Django 教程

共28课时 | 3.7万人学习

关于我们 免责申明 举报中心 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送

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