1. 程式人生 > >《程式設計師程式碼面試指南》給定一個整數n,返回從1到n的數字中1出現的次數

《程式設計師程式碼面試指南》給定一個整數n,返回從1到n的數字中1出現的次數

題目:

給定一個整數n,返回從1到n的數字中1出現的次數。

例如:

n=5,1~n為1,2,3,4,5。那麼1出現1次所以返回1。

n=11,1~n為1,2,3,4,5,6,7,8,9,10,11。那麼1出現的次數為1(1,10,11)返回4。

import java.util.Scanner;

public class Solution1 {
    public static int solution1(int num){
        if(num < 1){
            return 0;
        }
        int count = 0;
        for(int i = 1; i != num + 1; i++){
            count += get1Nums(i);
        }
        return count;
    }
    public static int get1Nums(int i){
        int res = 0;
        while(i != 0){
            if(i % 10 == 1){
                res++;
            }
            i /= 10;
        }
        return res;
    }
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        System.out.println(solution1(n));
    }
}

參考資料:《程式設計師面試程式碼指南》左程雲 著