请教一个简单c++问题 立即回复立即得分
给一个不多于5位的正整数,分别打印出每一位数字;按逆序打印出各位数字,例如原数为321,应输出123。
#include <iostream.h>
const int a=5;
void bubblesort(int[]);
void main ()
{
int b[a];
int i;
cout<<"please enter 5 numbers,when you enter one you must press 【enter】 to go on:";
cout<<endl;
for (i=0;i<a;i++)
{
cout<<"第"<<i+1<<"个为:";
cin>>b[i];
}
bubblesort(b);
cout<<"排序后的结果为:>>>";
for (i=0;i<a;i++)
cout<<b[i];
cout<<endl;
}
void bubblesort(int b[])
{
int top=0;
int tmp;
for(int i=0;i<a;i++)
{
for(int j=a-1;j>=top;j--)
if(b[j]>b[j-1]&&(j>0))
{
tmp=b[j-1];
b[j-1]=b[j];
b[j]=tmp;
}
top++;
}
}
问题点数:30、回复次数:23Top
1 楼pcboyxhy(-273.15℃)回复于 2005-04-03 20:35:57 得分 0
char str[10];
cin>>str;
char *p=str;
while(*p) cout<<*p++<<" ";
cout<<endl;
while(p>=str) cout<<*p--<<" ";
Top
2 楼lw1a2(一刀 现在改六点下班了:()回复于 2005-04-03 20:40:33 得分 0
汗,和那个爱情的帖子一样,难道是一个学校的?Top
3 楼llf_hust()回复于 2005-04-03 20:42:22 得分 0
你的冒泡程序有问题Top
4 楼fmzwyfmzwy(fmzwy)回复于 2005-04-03 20:43:58 得分 0
楼上的
我该怎么解决啊
帮帮我
什么爱情
什么的
不懂Top
5 楼kobefly(科比--网络学习中)回复于 2005-04-03 20:44:22 得分 10
#include <iostream.h>
#include <string.h>
const int SIZE =5;
int fun1(char s[])//Ç󳤶Ⱥ¯Êý
{
int i=0;
while(s[i]!='\0')
i++;
return i;
}
char* fun2(char p[],int j)//ÇóÄæÊý
{
int i=0;
int t;
while(i<j)
{
t=p[i];p[i]=p[j];p[j]=t;
i++;j--;
}
return p;
}
void main()
{
int length;
char sh[SIZE];
char sh2[SIZE];
cout<<"ÇëÊäÈëÒ»¸ö²»¶àÓÚ5λµÄÕýÕûÊý:";
cin>>sh;
if(fun1(sh)>5)
{
cout<<"ÊäÈëµÄÊý¾Ý²»ºÏÒªÇó,ÇëÖØÐÂÊäÈë:";
cin>>sh;
}
length=fun1(sh);
cout<<"ÄãÊäÈëµÄÊÇ:"<<length<<"λÊý!"<<endl;
for(int i=0;i<fun1(sh);i++)
cout<<"µÚ"<<i+1<<"¸öÊýÊÇ:"<<sh[i]<<endl;
strcpy(sh2,fun2(sh,length - 1));
cout<<"ÄæÊýΪ:"<<sh2<<endl;
}
Top
6 楼Salam2001(Upgrading : C++ and Data Structure ...)回复于 2005-04-03 20:45:06 得分 0
应该说是同一个老师布置的同一道作业...
绝对零度一箭双雕呀...Top
7 楼kobefly(科比--网络学习中)回复于 2005-04-03 20:45:31 得分 0
http://community.csdn.net/Expert/topic/3905/3905294.xml?temp=.9961664Top
8 楼fmzwyfmzwy(fmzwy)回复于 2005-04-03 20:53:48 得分 0
我的问题怎么变成别人的了
我说的是我的问题怎么解决啊
请你们帮我
解决我的问题
我的程序是那里的问题
我编译成功了
但执行的时候
并没有按我的输出
请帮帮我
谢谢了
Top
9 楼du51(郁郁思扬)回复于 2005-04-03 20:56:02 得分 10
#include <iostream>
using namespace std;
int fun1(int n) //求长度函数
{
int i=0;
if(n<=0)return -1;
else
{
while(n){i++;n/=10;}
return i;
}
}
void fun2(int p) //求逆数
{
cout<<"逆数为:";
while(p)
{
cout<<p%10;
p/=10;
}
cout<<endl;
}
void fun3(int p) //输出正序的数
{
static int count;
int i=10000;
while(p)
{
if(p/i)
{
cout<<"第"<<(count++)+1<<"个数是:"<<p/i<<endl;
p=p%i;
}
else i=i/10;
}
cout<<endl;
}
int main()
{
int n,length;
cout<<"请输入一个不多于5位的正整数:"<<endl;
cin>>n;
length=fun1(n);
if(length>5)
{
cout<<"输入的数据不合要求,请重新输入:";
cin>>n;length=fun1(n);
}
cout<<"你输入的是:"<<length<<"位数!"<<endl;
fun3(n);
fun2(n);
system("PAUSE");
return 0;
}Top
10 楼fmzwyfmzwy(fmzwy)回复于 2005-04-03 20:59:25 得分 0
谢谢你们为什么你们不能解决我的程序问题
你们为什么都拿自己的东西来糊弄我啊
老大门不要在折磨我了
谢谢了
在次谢谢了
真的谢谢了Top
11 楼flying_dancing(小混混-_-)回复于 2005-04-03 21:01:26 得分 0
kobefly(科比---开始学习c++!) 他写的还不错
很清晰!! 你只要把他 那输出的用简单的文字标一下就可以 ;;;
他还把每位的值也输出了 ,那没必要!Top
12 楼fmzwyfmzwy(fmzwy)回复于 2005-04-03 21:03:03 得分 0
我的问题你们可不可以解决撒
我的程序可以执行但为什么执行的时候看到结果就一闪而过Top
13 楼flying_dancing(小混混-_-)回复于 2005-04-03 21:10:39 得分 0
-_-
他们没有糊弄你呀!!!!他们写的肯定比我好!!! 不是可以运行吗Top
14 楼slbllf()回复于 2005-04-03 21:11:57 得分 0
兄弟你这程序是冒泡排序法而且是由大到小排不是把数字倒过来。逆序排列我认为目前你应该会的是用堆栈或首尾交换做。自己试试看。Top
15 楼shine51151(美丽心情)回复于 2005-04-03 21:24:50 得分 10
#include <iostream.h>
const int SIZE =5;
int fun1(int s[])//求长度函数
{
int i=0;
while(s[i]!=0)
i++;
return i;
}
int fun2(int p[],int j)//求逆数
{
int i=0;
int t;
while(i<j)
{
t=p[i];p[i]=p[j];p[j]=t;
i++;j--;
}
return p[SIZE];
}
void main()
{
int sh[SIZE],length;
int sh2[SIZE];
cout<<"请输入一个不多于5位的正整数:";
cin>>sh[SIZE];
if(fun1(sh)>5)
{
cout<<"输入的数据不合要求,请重新输入:";
cin>>sh[SIZE];
}
length=fun1(sh);
cout<<"你输入的是:"<<length<<"位数!"<<endl;
for(int i=0;i<=fun1(sh);i++)
cout<<"第"<<i+1<<"个数是:"<<sh[i]<<endl;
sh2[SIZE]=fun2(sh,fun1(sh));
cout<<"逆数为:"<<sh2<<endl;
}Top
16 楼shine51151(美丽心情)回复于 2005-04-03 21:27:07 得分 0
#include <iostream>
#include <cstdlib>
using namespace std;
#define N 5
void main()
{
int a[N],num,i=0;
cout << "Input a number with no more than 5 digits :\t";
cin >> num;
while(num)
{
a[i++]=num%10;
num/=10;
}
cout << "\nThe number you put has " << i << " digits in all";
cout << "\n\nHere with position from high to low all the digits are :\n\n";
for(int j=i-1;j>=0;j--)
cout << a[j] << "\t";
cout << "\n\nThe number with its digits in a reverse order is :\t";
for(int k=0;k<i;k++)
cout << a[k];
cout << endl << endl;
system("pause");
}Top
17 楼slbllf()回复于 2005-04-03 21:32:13 得分 0
//给一个不多于5位的正整数,分别打印出每一位数字;按逆序打印出各位数字,例如原数为321,应输出123。
#include <iostream.h>
#include <cstdlib>
const int a=5;
void bubblesort(int[]);
void main ()
{
int b[a];
int i;
cout<<"please enter 5 numbers,when you enter one you must press 【enter】 to go on:";
cout<<endl;
for (i=0;i<a;i++)
{
cout<<"第"<<i+1<<"个为:";
cin>>b[i];
}
bubblesort(b);
cout<<"排序后的结果为:>>>";
for (i=0;i<a;i++)
cout<<b[i];
cout<<endl;
}
void bubblesort(int b[])
{
int tmp;
for(int i=0,j=a-1;i<a/2;i++,j--)
{
tmp=b[i];
b[i]=b[j];
b[j]=tmp;
}
system("pause");
}
以上程序符合你的要求!Top
18 楼dearlulu(鹿鹿)回复于 2005-04-03 21:41:43 得分 0
大哥、mm:我也快毕业了,基础可以,较为熟练c,c++,可别人不要我,说没工作经验。没项目。先到小公司发展好还是找个较为轻松的非遍程工作自学好?
谢谢Top
19 楼zhuangmin(happy九拍)回复于 2005-04-03 21:46:37 得分 0
"我的问题你们可不可以解决撒
我的程序可以执行但为什么执行的时候看到结果就一闪而过"
这个你最后加个getchar()就可以了,但你冒泡里的a都没定义真奇怪
Top
20 楼zhuangmin(happy九拍)回复于 2005-04-03 21:51:12 得分 0
a 是全局,没看清
、
for(int i=0;i<a;i++)
{
for(int j=a-1;j>=top;j--)
if(b[j]>b[j-1]&&(j>0))
{
tmp=b[j-1];
b[j-1]=b[j];
b[j]=tmp;
}
top++;
}
嘿嘿,这段代码真想说两个字Top
21 楼inlin()回复于 2005-04-03 22:00:05 得分 0
http://community.csdn.net/Expert/topic/3905/3905294.xml?temp=.9961664
Top
22 楼AtaLoss0202(星空天宇)回复于 2005-04-03 22:28:23 得分 0
#include "iostream"
using namespace std;
void main()
{
char n[5] = "eeeee";
cout << "n = ";
cin >> n;
for(int i = 4; i >=0; i--)
if(n[i] != 'e' && n[i] != '\0')
cout << n[i];
cout << endl;
}
这个满足要求.不过,没有检查输入的数据是否都是数字,以及输入的数据长度是否在5以内.程序只对输入数据的前5位进行逆序输出.Top
23 楼AtaLoss0202(星空天宇)回复于 2005-04-03 22:31:25 得分 0
以下是测试输出:
n = 321
123
n = 13245
54231Top




