Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1598235
  • 博文数量: 354
  • 博客积分: 8137
  • 博客等级: 中将
  • 技术积分: 5137
  • 用 户 组: 普通用户
  • 注册时间: 2009-11-26 15:40
文章分类

全部博文(354)

文章存档

2010年(300)

2009年(54)

分类: C/C++

2010-06-19 23:16:26


/***********
给出一个不多于5位的正整数,要求,1 求它是几位数,2 分别打印出每一位数字;3逆向打印出个数字
*************/

#include <stdio.h>

int main(int argc, char *argv[])
{
    int num;
    int indiv, ten, hundred, thousand;
    int ten_thousand, hundred_thousand,place;

    printf("请输入一个整数(0~999999):");
    scanf("%d",&num);
    if(num > 99999)
        place = 6;
    else if(num > 9999)
        place = 5;
    else if(num > 999)
        place = 4;
    else if (num > 99)
        place = 3;
    else if(num > 9)
        place = 2;
    else place = 1;

    printf("place = %d", place);

    printf("每位数字:");

    hundred_thousand = num/100000;
    ten_thousand = (num - hundred_thousand*100000)/10000;
    thousand = (num - hundred_thousand*100000-ten_thousand*10000)/1000;
    hundred = (num - hundred_thousand*100000-ten_thousand*10000-thousand*1000)/100;
    ten = (num - hundred_thousand*100000-ten_thousand*10000-thousand*1000-hundred*100)/10;
    indiv = num - hundred_thousand*100000-ten_thousand*10000-thousand*1000-hundred*100-ten*10;

    switch(place)
    {    case 1:printf("%d", indiv);
             printf("\n反序数字为:");
             printf("%d\n",indiv);
             break;

        case 2:printf("%d,%d", ten, indiv);
             printf("\n反序数字为:");
             printf("%d%d\n",indiv, ten);
             break;
        case 3:printf("%d,%d,%d",hundred, ten, indiv);
             printf("\n反序数字为:");
             printf("%d%d%d\n",indiv, ten, hundred);
             break;
        case 4:printf("%d,%d,%d,%d", thousand, hundred,ten,indiv);
             printf("\n反序数字为:");
             printf("%d%d%d%d\n",indiv,ten,hundred,thousand);
             break;
        case 5:printf("%d,%d,%d,%d,%d", ten_thousand,thousand,hundred,ten,indiv);
             printf("\n反序数字为:");
             printf("%d%d%d%d%d\n",indiv,ten,hundred,thousand,ten_thousand);
             break;
        case 6:printf("%d,%d,%d,%d,%d,%d",hundred_thousand,ten_thousand,thousand,hundred,ten,indiv);
             printf("\n反序数字为:");
             printf("%d%d%d%d%d%d\n",indiv,ten,hundred,thousand,ten_thousand,hundred_thousand);
             break;

        default:printf("NOT FIND.\n");
        break;
    }

        while(1);
    return 0;
}


阅读(2308) | 评论(0) | 转发(1) |
给主人留下些什么吧!~~