close

Give you a number then you should to test whether it is a multiple of 9.

e.g.

999999999999999999999          //  sum of digits = 189    degree=1

189                                             //  18 degree=2

18                                            // 9 degree=3, answer

 

----

#include <stdio.h>

#define N 1000

#define true 1
#define false 0

typedef int bool;

int Recursive_nine(int dig_total);

int main(int argc, char *argv[]) 
{
    char num[N+1];
    int i, j;
    int dig_total; /* digits' total */
    bool is_mul; /* multiple */
    
    while(~scanf("%s", &num))
    {
        /* initialize */
        is_mul = false;
                    
        if(num[0] == '0')
        {
            for(i = 0, j = 0;  i < N;  i++) /* To check whether all of the digits in this array is 0. */
                if(num[i] == '0')
                    j++;
                else if(num[i] =='\0')
                    break;
            if(j == i) break; /* All digits is '0'. */
        }
        
        for(dig_total = 0, i = 0;  num[i];  i++) /* Find digits total */
            dig_total += num[i] - '0'; /* char to int */
            
        if(!(dig_total % 9)) /* Whether it is a multiple of nine. */
            is_mul = true;
        
        if(is_mul)
        {
            for(i = 0; num[i]; i++)
                printf("%c", num[i]);
            printf(" is a multiple of 9 and has 9-degree %d.\n", Recursive_nine(dig_total));
        }
        else
        {
            for(i = 0; num[i]; i++)
                printf("%c", num[i]);
            printf(" is not a multiple of 9.\n");
        }
    }
    return 0;
}

/* Compute how deep it is. */
int Recursive_nine(int dig_total)
{
    int i, sum;
    int depth = 0;
    if(dig_total == 9) /* Terminal condition */
        return depth+1;
    
    for(i = 0, sum = 0;  dig_total;  i++, dig_total /= 10) /* Fine the sum of this layer. */
        sum += dig_total % 10;
        
    depth += Recursive_nine(sum);
    return depth+1;
}
/* When "gets" function reads strings successfully, it returns the memory address of this array. */
    /* When "gets" function reads "EOF", it returns the integer "0". */
    /*while(gets(num) && num[0] != '0')
    {
        
    }*/
 

 

----

package uva.pkg10922;

import java.util.Scanner;

/**
 *
 * @author awesq
 */
public class UVa10922 {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        try (Scanner sc = new Scanner(System.in)) {
            while (sc.hasNextLine()) {
                String numberStr = sc.nextLine();
                if (numberStr.charAt(0) == '0') {
                    break;
                }

                int degree = recursiveNine(numberStr);
                if (degree < 0) {
                    System.out.println(numberStr + " is not a multiple of 9.");
                } else {
                    System.out.println(numberStr + " is a multiple of 9 and has 9-degree " + degree + ".");
                }
            }
        } catch (Exception e) {
        }
    }

    public static int recursiveNine(String number) {
        int sum = 0;
        for (int i = 0; i < number.length(); i++) {
            sum += number.charAt(i) - '0';
        }
        if (sum > 9) {
            return 1 + recursiveNine("" + sum);
        } else if (sum == 9) {
            return 1;
        } else {
            return Integer.MIN_VALUE;
        }
    }
}
 

arrow
arrow
    全站熱搜

    awesq123 發表在 痞客邦 留言(0) 人氣()