当前位置: 代码迷 >> C语言 >> [求助]SOS!请问下怎样将数字反向输出?
  详细解决方案

[求助]SOS!请问下怎样将数字反向输出?

热度:257   发布时间:2007-04-18 13:56:02.0
[求助]SOS!请问下怎样将数字反向输出?

题目是 输入一个低于5位的正整数
1,输出它是几位数字.
2,反向输出,即 比如原来是123 现在要求输出321,原来输出1234 现在要求输出4321.
请高手指点下,我刚学C,先谢谢了.
请给中文注释,我用的是VC.再次感谢.

搜索更多相关的解决方案: SOS  数字  输出  

----------------解决方案--------------------------------------------------------

你看看这个行不?
#include <stdio.h>
#include <string.h>

int main(void)
{
char ch[200],*p,*q,temp;
gets(ch);
p=ch;
q=p+strlen(ch)-1;
while(p<q)
{
temp=*p;
*p=*q;
*q=temp;
p++;
q--;
}
printf("the strlen of the num is:%d\n",strlen(ch));
printf("print the operate string:%s",ch);
printf("\n");
return 0;
}


----------------解决方案--------------------------------------------------------
如果不用数组能做吗?我想知道不用数组怎么做....
----------------解决方案--------------------------------------------------------
那可以用链式栈。应该很简单的吧。。。如果还有问题,我待会给写一个!
----------------解决方案--------------------------------------------------------
这个吧:
# include<stdio.h>
# include<malloc.h>
struct list
{
int data;
struct list *next;
};
struct list *creat();
void reverse(struct list *head);
void print(struct list *head);
void main()
{
struct list *head;
head=creat();
reverse(head);
return;
}
struct list *creat()
{
struct list *head,*p,*rear;
int x;
head=(struct list*)malloc(sizeof(struct list));
rear=head;
printf("please input the date:\n");
scanf("%d",&x);
puts("input the list end with '0':\n");
while(x)
{
p=(struct list*)malloc(sizeof(struct list));
p->data=x;
rear->next=p;
rear=p;
scanf("%d",&x);
}
rear->next=NULL;
puts("the list you input is: ");
print(head->next);
return head->next;
}
void reverse(struct list *head)
{
struct list *p,*q,*s;
p=head;
s=head;
q=p->next;
s->next=NULL;
while(q)
{
p=q;
q=q->next;
p->next=s;
s=p;
}
head=p;
puts("\nthe reversed list is: ");
print(head);
}
void print(struct list *head)
{
struct list *p;
p=head;
while(p)
{
printf("%3d",p->data);
p=p->next;
}
printf("\n");
}
这个没用到链式栈!
----------------解决方案--------------------------------------------------------

太麻烦了吧。可以把n除于1000得余数i,如果不是0的话打印出余数i,然后再n=n-i*1000;再除于100。依次类推


----------------解决方案--------------------------------------------------------
晕,打错了
----------------解决方案--------------------------------------------------------

设那个数字为n,i=n%10,打印i,然后i=(n%100)/10;打印i.i=(n%1000)/100,打印i,然后i=n/1000,打印i


----------------解决方案--------------------------------------------------------
.....谢谢各位
----------------解决方案--------------------------------------------------------
  相关解决方案