zoukankan      html  css  js  c++  java
  • 正序输出整数

    1、

     1 /*
     2     正序输出整数的每一位数,数字之间用空格隔开
     3     123456    1 2 3 4 5 6
     4 */
     5 
     6 #include <stdio.h>
     7 int pow(int a, int b);
     8 
     9 int main()
    10 {
    11     int n;
    12 
    13     scanf_s("%d", &n);
    14 
    15     //判断一个数有几位
    16     int count = 0;
    17     int temp = n;
    18     do
    19     {
    20         temp = temp / 10;
    21         count++;
    22     } while (temp!=0);
    23 
    24     //分离出每一位数
    25     int digit;
    26     temp = n;
    27     do
    28     {
    29         digit = temp / pow(10, count - 1);
    30         printf("%d", digit);
    31         if (count > 1)
    32         {
    33             printf(" ");
    34         }
    35         temp = temp % pow(10, count - 1);
    36         count--;
    37     } while (count!=0);
    38     
    39     printf("
    ");
    40     return 0;
    41 }
    42 int pow(int a, int b)
    43 {
    44     int result = 1;
    45     for (int i = 0; i < b; i++)
    46     {
    47         result = result * a;
    48     }
    49 
    50     return result;
    51 }

    2、参考老师讲解视频修改版

     1 /*
     2     正序输出整数的每一位数,数字之间用空格隔开
     3     123456    1 2 3 4 5 6
     4 */
     5 
     6 #include <stdio.h>
     7 
     8 int main()
     9 {
    10     int n;
    11 
    12     scanf_s("%d", &n);
    13 
    14     int count = 0;
    15     int temp = n;
    16     int mask = 1;
    17     do
    18     {
    19         temp = temp / 10;
    20         count++;
    21         mask = mask * 10;
    22     } while (temp!=0);
    23 
    24     mask = mask / 10;
    25     //分离出每一位数
    26     int digit;
    27     temp = n;
    28     do
    29     {
    30         digit = temp / mask;
    31         printf("%d", digit);
    32         if (count > 1)
    33         {
    34             printf(" ");
    35         }
    36         temp = temp % mask;
    37         count--;
    38         mask = mask / 10;
    39     } while (count!=0);
    40     
    41     printf("
    ");
    42     return 0;
    43 }
  • 相关阅读:
    python转换emoji字符串
    python位运算符详细介绍
    python制作动态排序图
    docker安装mysql
    yum安装centos-7版nginx
    pysimplegui模块实现倒计时UI框
    pysimplegui模块实现进度条
    python枚举的应用enum
    第0-0课
    SV -- Array 数组
  • 原文地址:https://www.cnblogs.com/2018jason/p/10949279.html
Copyright © 2011-2022 走看看