注册 登录
编程论坛 C图形专区

c语言编写任意输入一个数并将他分离出来

lp617 发布于 2015-05-19 23:33, 1198 次点击
c语言编写任意输入一个数并将他分离出来
7 回复
#2
醉祁今朝2015-05-24 09:35
是说如果输入358,出来的是3 5 8 么。。
#3
lp6172015-05-31 10:00
回复 2楼 醉祁今朝
对但不能出现0
#4
yx19982015-07-26 12:12
有没有数据类型要求呢?如果可以用字符串保存不是很容易吗?
#5
yx19982015-07-26 12:28
我写了一个,是处理数字的。
程序代码:

#include<stdio.h>
#include<math.h>
int main (void)
{
    int n;//要求的十进制数
    int size=1;//这个十进制数的位数
    printf("Enter a number.");
    scanf("%d",&n);
    while((n/pow(10,size))>=1)//如果size就是十进制的位数,n/10size次会为0
    size++;
    int num[size];//用来保存拆开来的数字,以便倒序输出
    for(int count=1;count<=size;count++)
    {
        num[count-1]=n%10;
        n=(n-n%10)/10;
    }
    for(int count=size-1;count>=0;count--)
    {
        printf("%d ",num[count]);
    }
    return 0;
}
#6
天使梦魔2015-07-27 07:46
不明白,用string不就行了,输出的时候访问下标
#7
冷曦。2015-08-01 21:28
程序代码:
#include<stdio.h>
#include<math.h>
void main ()
{
    int n,t=0,a,b[100],i,j,p;
    scanf("%d",&n);
    p=n;
    for(a=1;n/a!=0;a=a*10)
        t++;
    a=pow(10,t);
    b[0]=n/a;
    for(i=1;i<t;i++)
    {
        for(j=0;--i>0;)
            p-=b[j]*a/(j*10);
        b[i]=p/(a/(pow(10,i)));
    }
    for(i=0;i<strlen(b);i++)
        printf("%d\t");
}

、、、突然发现我自己看着都烦、、、
#8
冷曦。2015-08-01 21:29
程序代码:
#include<stdio.h>
#include<math.h>
#include<string.h>
void main ()
{
    int n,t=0,a,b[100],i,j,p;
    scanf("%d",&n);
    p=n;
    for(a=1;n/a!=0;a=a*10)
        t++;
    a=pow(10,t);
    b[0]=n/a;
    for(i=1;i<t;i++)
    {
        for(j=0;--i>0;)
            p-=b[j]*a/(j*10);
        b[i]=p/(a/(pow(10,i)));
    }
    for(i=0;i<strlen(b);i++)
        printf("%d\t");
}

漏打了、、
1