首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > C++ >

正整数以相反顺序输出,求大神纠错,该如何解决

2013-01-07 
正整数以相反顺序输出,求大神纠错Description将正整数以相反顺序输出的递归算法可以如下描述:ReverseNumbe

正整数以相反顺序输出,求大神纠错
Description

将正整数以相反顺序输出的递归算法可以如下描述:
ReverseNumber (number):
  If the length of the given number equals 1
    out put the number;
  else
    output the lowest digit of the number;
    output ReverseNumber(the remain digits of the number);
按此算法编写程序(包括main()和ReverseNumber(number)函数)
Input

输入包含多组数据,每行一组输入。每组数据只有一个正整数。输入以EOF结束。
Output

对于每组数据,输出一行,输出结果为输入正整数的逆序输出。
Sample Input
Copy sample input to clipboard

12345
120

Sample Output

54321
021
这是我的程序,求纠错
#include <stdio.h>
#include<stdlib.h>
#include <string.h>
int main() {
char x;
scanf("%ch",&x);
    printf("%ch",ReverseNumber(x));
    return 0;
}
int ReverseNumber(char 'x'){
char str[120];
char 'x';
int i, j;
char a;
while ((scanf("%s", str) == 1)!=EOF) {
i = strlen(str);
i--;
for (j = 0; j < i; j++, i--) {
a = str[i];
str[i] =str[j];
str[j] = a;
x=str[j];
}
}
return ReverseNumber(char 'x');
}
[解决办法]

#include <stdio.h>
#include<stdlib.h>
#include <string.h>

char *reserve(char *s)
{
if (s)
{
char *p, *q;
char ch;

p = s;
q = s + strlen(s) - 1;

while (p < q)
{
ch = *p;
*p = *q;
*q = ch;

p++, q--;
}
}
return s;
}

int main(void) {
intx;
charbuf[64];

scanf("%d", &x);
itoa(x, buf, 10);
printf("%s", reserve(buf));

return 0;
}

[解决办法]
这个题目要求用递归吧 ……
定义一个reverse算法
输出最后一位,再输出前n-1为的reverse
当n=1时直接输出、、

你再试试
[解决办法]
/*  HELLO.C -- Hello, world */

#include "stdio.h"
#include "conio.h"

void reserve(int a)
{
    int t;
    if(a>=0&&a<10)
    printf("%d",a);
    else
    {
        t=a%10;
        printf("%d",t);
        a/=10;
        reserve(a);
    }
}

main()
{
    int a=234;
    reserve(a);
    getch();
}

//类似于这个,你自己写吧,加一个读入数据的

热点排行