| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 1304 人关注过本帖
标题:顺序输出各位数字
只看楼主 加入收藏
灵昀
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: 20Rank: 20Rank: 20Rank: 20Rank: 20
来 自:神界
等 级:贵宾
威 望:423
帖 子:13688
专家分:53332
注 册: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
快速回复:顺序输出各位数字
数据加载中...
 
   



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

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