Day 0x1B UVa10922 2 the 9s

题意

  • 输入一正整数 N,判断是否为 9 的倍数及输出 nine-degree
  • 需要注意的有:
    1. 重复输入一正整数 N 直到 0 (最大到 1000 位)
    2. 什麽是 nine-degree
      • 根据 9 的倍数判别法 (跟 3 一样),检查位元总和是不是 9 的倍数,并重复计算有几次总和是 9 的倍数
      • e.g. 837 = 8 + 3 + 7 = 18 = 9 x 2 → 一次
        18 = 1 + 8 = 9 = 9 x 1 → 两次
        nine-degree = 2
    3. 输出格式
      • N is a multiple of 9 and has 9-degree D.
      • N is not a multiple of 9.

解法

  • 起手式 while 回圈重复输入 N,直到读进 0;因为数字可能很大,用字串方式读入存到字元阵列中
    int main(){
    
        char N[1001] = {0};
    
        while(scanf("%s", N)){
            if(N[0] == '0' && strlen(N) == 1){
                break;
            }
            else{
                ...
            }
        }
    
        return 0;
    }
    
  • main() 处理好输出格式
    printf("%s ", N);
    
    int D = degree(N);
    
    if(D){
        printf("is a multiple of 9 and has 9-degree %d.", D);
    }
    else{
        printf("is not a multiple of 9.");
    }
    
    printf("\n");
    
  • 自订 function 计算有多少次总和为 9 的倍数;先过滤掉只有 '9' 的时候,只要长度 ≧ 2 就代表是 10 以上,所以还有机会计算总和;每次都 for 回圈遍历字元计算总和,只要 % 9 == 0 代表即为 9 的倍数,并更新次数;再透过 sprintf() 这个神奇的函式把整数转回字元阵列继续计算
    int degree(char *str){
    
        int i;
        int multiple, D = 0;
    
        if(strlen(str) == 1 && str[0] == '9'){
            return 1;
        }
    
        while(strlen(str) >= 2){
    
            multiple = 0;
    
            for(i = 0; i < strlen(str); i++){
                multiple = multiple + str[i] - '0';
            }
    
            if(multiple % 9 == 0){
                D++;
            }
    
            sprintf(str, "%d", multiple);
        }
    
        return D;
    }
    
  • C code
    #include<stdio.h>
    #include<string.h>
    
    int degree(char *str){
    
        int i;
        int multiple, D = 0;
    
        if(strlen(str) == 1 && str[0] == '9'){
            return 1;
        }
    
        while(strlen(str) >= 2){
    
            multiple = 0;
    
            for(i = 0; i < strlen(str); i++){
                multiple = multiple + str[i] - '0';
            }
    
            if(multiple % 9 == 0){
                D++;
            }
    
            sprintf(str, "%d", multiple);
        }
    
        return D;
    }
    
    int main(){
    
        char N[1001] = {0};
    
        while(scanf("%s", N)){
            if(N[0] == '0' && strlen(N) == 1){
                break;
            }
            else{
    
                printf("%s ", N);
    
                int D = degree(N);
    
                if(D){
                    printf("is a multiple of 9 and has 9-degree %d.", D);
                }
                else{
                    printf("is not a multiple of 9.");
                }
    
                printf("\n");
            }
    
            memset(N, 0, sizeof(N));
        }
    
        return 0;
    }
    

<<:  sed - 5 Replace command

>>:  Day 28 : 用於生产的机械学习 TensorFlow Extended (TFX) 介绍

[Day4] Vite 出小蜜蜂~ Input Control 操作系统!

Day4 接下来卡比要是着操作 LaserCannon,让他可以左右移动。 Input 在上个章节,...

DAY11-JAVA的类别(5)

在建构元中也有所谓公有(public)和私有(private)之分。截至目前为止,所提到的都是公有(...

Day 06: Python基础必备小知识(上)

那麽在前篇,我们已经安装建置好整个Python运行环境,接下来就可以学习如何编写简单的程序了! 以下...

Day 13. 是时候来规划个专案了,先来个小调查

我的小夥伴推荐我做贪吃蛇但吃的是我,於是就有了以下两种: 不是很完整的想法,希望大家可以从我的文字叙...

Day5 跟着官方文件学习Laravel-把Request丢给Controller去处理

回顾一下昨天,我们已经能透过route+view让登入页面显示在画面上了,今天的任务呢,就是要把登入...