7-3 逆序的三位数 (10分)---队列

程序每次读入一个正3位数,然后输出按位逆序的数字。注意:当输入的数字含有结尾的0时,输出不应带有前导的0。比如输入700,输出应该是7。

输入格式:
每个测试是一个3位的正整数。

输出格式:
输出按位逆序的数。

输入样例:
123
输出样例:
321
鸣谢安阳师范学院软件学院李康康同学补充数据!

不知道大家这题是怎么解决的。我用的是队列。

 #include <iostream>
#include <cmath>
#include <string>
#include <queue>
using namespace std;

int main (){
	string str;
	while(cin>>str){
		queue<char> que;
		for(int i=str.length()-1;i>=0;i--){	
			if(que.empty()==1&&str[i]=='0'){
				continue;
			}else{
				que.push(str[i]);
			}
		}
		while(!que.empty()){
			cout<<que.front();
			que.pop();
		}
		cout<<endl;
	}
	return 0;
}

我先去看看大佬怎么写的。

#include <iostream>
using namespace std;
int main (void)
{
	int num;
	int a, b, c;
	
	scanf("%d", &num);
	a = num / 100;
	b = (num - 100 * a) / 10; 
	c = num % 10;
	
	printf("%d", 100*c + 10*b + a);
	
	return 0;
 } 

他们的好像还更简便一些,直接解决了前导0的问题。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值