标题:顺序输出各位数字
只看楼主
灵昀
Rank: 1
等 级:新手上路
帖 子:13
专家分:0
注 册:2012-12-1
结帖率:80%
已结贴  问题点数:20 回复次数:5 
顺序输出各位数字
Description

输入一个整数,从高位开始逐位分割并输出各位数字。



Input

输入一个正整数n,n是int型数据



Output

依次输出各位上的数字,每一个数字后面有一个空格,输出占一行。例如,输入 12345 ,输出 1 2 3 4 5



Sample Input


12345

Sample Output


1 2 3 4 5

搜索更多相关主题的帖子: 分割 正整数 
2012-12-01 16:30
一个孩子
Rank: 8Rank: 8
等 级:蝙蝠侠
威 望:5
帖 子:356
专家分:954
注 册:2012-10-1
得分:7 
程序代码:
#include<stdio.h>
int main()
{
    long a;
    long i;
    long j=0;
    long b[20];
    printf("please input the number:\n");
    scanf("%ld",&a);
    printf("从低位到高位分别是:\n");
    while(a)
    {
        i=a%10;
        b[j]=i;
        printf("%ld ",i);
        a/=10;
        if(a==0)
            break;
        else
            j++;
       
    }
    printf("\n");
    printf("从高位到低位分别是:\n");
    for(;j>=0;j--)
    {
        printf("%d ",b[j]);
    }
    printf("\n");
    return 0;
}

重要的不是结果,是求一个结果的过程,哪怕千难万难,当你有想要的结果时,你已走的很远
2012-12-01 16:33
灵昀
Rank: 1
等 级:新手上路
帖 子:13
专家分:0
注 册:2012-12-1
得分:0 
谢谢了
2012-12-01 20:53
灵昀
Rank: 1
等 级:新手上路
帖 子:13
专家分:0
注 册:2012-12-1
得分:0 
回复 2楼 一个孩子
谢谢了
2012-12-01 20:54
wp231957
Rank: 16Rank: 16Rank: 16Rank: 16
来 自:神界
等 级:版主
威 望:422
帖 子:13681
专家分:53296
注 册:2012-10-18
得分:7 
程序代码:
#include <stdio.h>

int i=0; 
void int2char(char s[],long int source) 
{

 if (source==0)

 {
  return ;

 }

 int2char(s,source/10); 

 s[i]=source%10+48; 

 i++;

}

int main(void)
{
    int source=12345;
    char dest[6]={'\0'};
    int2char(dest,source);
    printf("%s",dest);
    return 0;
}
用递归也可以

DO IT YOURSELF !
2012-12-01 21:53
神龙赖了
Rank: 10Rank: 10Rank: 10
来 自:萨塔星
等 级:青峰侠
威 望:2
帖 子:711
专家分:1788
注 册:2012-10-13
得分:7 
程序代码:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int main(void)
{
    int number = 0;
    char *temp = NULL;
    int number2 = 0;
    int count = 1;
    int i = 0;

    printf("Please enter a number:");
    scanf("%d",&number);

    number2 = number;
    printf("\n\nOutput:\n");
    printf("逆向\n");
    do
    {
        count++;
        printf("%2d",number%10);
    }while((number /= 10) >0);
    printf("\n");

    temp = (char*)malloc(count);
    temp[--count] = '\0';
    do
    {
        temp[--count] = number2%10+'0';
    }while((number2 /= 10) >0);

    printf("顺向:\n");
    for(i=0;i<strlen(temp);i++)
        printf("%2c",temp[i]);
    printf("\n");
    return 0;
}

也可以用char指针去储存
相对来说更省空间。

I have not failed completely
2012-12-02 13:31



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-389213-1-1.html




关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.957319 second(s), 7 queries.
Copyright©2004-2025, BCCN.NET, All Rights Reserved