关于如何把数字倒序输出

satertt 2008-09-27 12:17:30
要求写一个公式:
输入1-9999之间任意数字,然后倒序输出,比如你输入12345,输出54321
这要怎么写啊?
...全文
1096 15 打赏 收藏 转发到动态 举报
写回复
用AI写文章
15 条回复
切换为时间正序
请发表友善的回复…
发表回复
yeatol 2008-10-04
  • 打赏
  • 举报
回复
指针
rivulettornado 2008-09-28
  • 打赏
  • 举报
回复
我觉得这里是不是有一点问题,就是提问中说的“公式”的概念,我的理解是只使用简单的操作,不可以使用循环等。
uurabit 2008-09-28
  • 打赏
  • 举报
回复
思路
先把数字格式化到字符数组中去,在递归输出
大写的池 2008-09-27
  • 打赏
  • 举报
回复
哈哈,用栈


#include <iostream>
#include <string>
#include <stack>
using namespace std;

int main(int argc, char* argv[])
{
string str;
cout<<"input your number:";
cin>>str;
Stack<char> _stack;
for(int i=0;i<str.length();++i)
{
_stack.push(str[i]);
}
for(int i=0;i<str.length();++i)
{
cout<<_stack.pop();
}

return 0;
}
wxb_nudt 2008-09-27
  • 打赏
  • 举报
回复
现在是C++和STL的时代了,各位看我的,我自豪的说我是最优解:

#include "stdafx.h"
#include <iostream>
#include <string>
#include <algorithm>
#include <list>
using namespace std;

int main(int argc, char* argv[])
{
string str;
cout<<"input your number:";
cin>>str;
list<char> lstResult;
copy(str.begin(), str.end(), front_inserter(lstResult));
copy(lstResult.begin(), lstResult.end(), ostream_iterator<char>(cout));
return 0;
}
lbh2001 2008-09-27
  • 打赏
  • 举报
回复
直接用字符吧


#include <iostream>

int main(void)
{
const int N = 10; //最多N-1位数字
char str[N] = { '\0' };

std::cin >> str;

int len = strlen(str);
for (int i = len - 1; i >= 0; --i)
std::cout << str[i];

std::cout << '\n';

return 0;
}
kkndciapp 2008-09-27
  • 打赏
  • 举报
回复
方法二
#include<iostream>
using namespace std;

void main()
{
int num;
char p[20];
cin>>num;
int i=0;
while(num)
{
p[i++]=num%10+'0';
num/=10;
}
p[i]='\0';
cout<<p;
}
kkndciapp 2008-09-27
  • 打赏
  • 举报
回复
帮你写了一个,方法一
#include<iostream>
using namespace std;

void main()
{
int num;
char p[20];
cin>>num;
itoa(num,p,10);
int len=strlen(p);
for(int i=0;i<len/2;i++)
{
char temp=p[i];
p[i]=p[len-1-i];
p[len-1-i]=temp;
}
cout<<p;'
}
whuyotc 2008-09-27
  • 打赏
  • 举报
回复
mark
yslwpl 2008-09-27
  • 打赏
  • 举报
回复
学习了。
ysunwpi 2008-09-27
  • 打赏
  • 举报
回复
See the code below, very easy ya.......

#include <cstdlib>
#include <iostream>
using namespace std;

int main(int argc, char *argv[])
{
char str[8];
cin >> str;

int s_value = atoi(str);
int s_digit;
do {
s_digit = s_value % 10;
s_value /= 10;
cout << s_digit;
} while (s_value > 0);
cout << endl;

system("PAUSE");
return EXIT_SUCCESS;
}

Output:
12345
54321
Press any key to continue . . .
rivulettornado 2008-09-27
  • 打赏
  • 举报
回复
你应该输出b才行啊。
satertt 2008-09-27
  • 打赏
  • 举报
回复
这个运行不出来啊,之前还要设什么参数吗?
#include <iostream>
#include <cmath>

using namespace std;

int main()
{
int x,b;
cout << "Please enter a number :"<< endl;
cin >> x;
b =(((x%10)*pow(10.0,(int)(bool)((x/10)%10))+(x/10)%10)*pow(10.0,(int)(bool)((x/100)%10))+(x/100)%10)*pow(10.0,(int)(bool)((x/1000)%10))+(x/1000)%10;
return 0;
}
rivulettornado 2008-09-27
  • 打赏
  • 举报
回复
如果是这样的数字从1到9999,设为x,b为其反序的数字,这样的公式可以
b=(((x%10)*pow(10.0,(int)(bool)((x/10)%10))+(x/10)%10)*pow(10.0,(int)(bool)((x/100)%10))
+(x/100)%10)*pow(10.0,(int)(bool)((x/1000)%10))+(x/1000)%10;

对于更高的位数,可以进行扩展。

ps:看起来有点汗啊...
kingteng 2008-09-27
  • 打赏
  • 举报
回复
#include<iostream>
#include<string>
#include<cstdlib>
using namespace std;

int main()
{
long int num;
cout<<"input a num";

cin>>num;

char *pchar = new char[20];
itoa(num,pchar,10);
string str1(pchar);
string str2(str1.begin(),str1.end());

for(string::iterator itr1 = str2.begin(),itr2 = str2.begin()+str2.size()-1;
itr1 != itr2;itr1++,itr2-- )
swap(*itr1,*itr2);
cout<<str2;
delete [] pchar;
return 0;
}

64,682

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧