数数

数数

问题描述

ACM俱乐部里的那些无聊家伙经常举行数数比赛- -。
比赛规则就是对于一个给出的正整数n,把1到n的正整数写在纸上,然后数里面数字1被写出来的次数。

输入

输入有多组数据。 每组数据一行,包含一个正整数n(小于等于2^26)。

输出

对应每组数据,输出所求的1的出现次数。

输入例子 1

11
20

输出例子 1

4
12

import java.util.Scanner;
public class Main {
    public static int f(int n)
    {
        int factor = 1;
        int res = 0;
        int low_bit, cursor, high_bit;
        while (n / factor!=0) {
            low_bit = n % factor;
            cursor = n / factor % 10;
            high_bit = n / factor / 10;
            if (cursor == 0)
                res += high_bit * factor;
            else if (cursor == 1)
                res += high_bit * factor + low_bit + 1;
            else
                res += (high_bit + 1) * factor;
            factor *= 10;
        }
        return res;
    }
    public static void main(String[] args) {
        int n;
        Scanner scanner = new Scanner(System.in);
        while (scanner.hasNext()) {
            n=scanner.nextInt();
            System.out.println(f(n));
        }
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值