题解 | 记数问题

记数问题

https://www.nowcoder.com/practice/28b2d9f2bf2c48de94a1297ed90e1732

import java.util.Scanner;

// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        // 注意 hasNext 和 hasNextLine 的区别
        int n = in.nextInt();
        int x = in.nextInt();

        // 1-100
        // 9出现的次数

        int count = 0;
        for(int i = 1;i<=n;i++){
            // 依次读取每一位
            int temp = i;
            // System.out.println("-----------i:"+temp);
            int k = Integer.MAX_VALUE;
            while(temp!=0){
                k = temp % 10;
                // System.out.println("k:"+k);
                if(k==x){
                    count++;
                }
                temp /= 10;
            }
        }
        System.out.println(count);
    }
}

全部评论

相关推荐

02-04 17:01
南昌大学 Java
牛客96763241...:拿插件直接投就完了,这玩意看运气的
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务