1049 counting ones

从所有一位数包含的1开始向上递推所有k位数包含的1,递推式:

ak = ak-1 * 10 + pow(10,k-1);

AC代码:

#include <vector>
#include <cstdio>
using namespace std;
int main(){
    int n;
    vector<int> rec;
    scanf("%d",&n);
    int nc(n);
    while(n != 0){
        rec.push_back(n % 10);
        n /= 10;
    }
    vector<int> r(rec.size());
    vector<int> rem;
    int radix(1);
    for(int i = 0;i < r.size();i++){
        rem.push_back(radix);
        if(i == 0)
            r[0] = 1;
        else{
            r[i] = r[i - 1] * 10 + radix;
        }
        radix *= 10;
    }
    int ret(0);
    for(int i = rec.size() - 1;i >= 0;i--){
        if(i != 0 && rec[i] != 0){
            if(rec[i] == 1)
                ret += r[i - 1] + nc % rem[i] + 1;
            else
                ret += r[i - 1] * rec[i] + rem[i];
        }
        else if(i == 0 && rec[i] != 0)
            ret += 1;
    }
    printf("%d\n",ret);
    return 0;
}
时间: 2024-08-03 11:16:48

1049 counting ones的相关文章

PAT 1049. Counting Ones (30)

1049. Counting Ones (30) The task is simple: given any positive integer N, you are supposed to count the total number of 1's in the decimal form of the integers from 1 to N. For example, given N being 12, there are five 1's in 1, 10, 11, and 12. Inpu

PAT 1049 Counting Ones [难]

1049 Counting Ones (30 分) The task is simple: given any positive integer N, you are supposed to count the total number of 1's in the decimal form of the integers from 1 to N. For example, given N being 12, there are five 1's in 1, 10, 11, and 12. Inp

PAT 甲级 1049 Counting Ones (30 分)(找规律,较难,想到了一点但没有深入考虑嫌麻烦)***

1049 Counting Ones (30 分) The task is simple: given any positive integer N, you are supposed to count the total number of 1's in the decimal form of the integers from 1 to N. For example, given N being 12, there are five 1's in 1, 10, 11, and 12. Inp

1049. Counting Ones/整数中1出现的次数(从1到n整数中1出现的次数)

The task is simple: given any positive integer N, you are supposed to count the total number of 1's in the decimal form of the integers from 1 to N. For example, given N being 12, there are five 1's in 1, 10, 11, and 12. Input Specification: Each inp

PAT 1049 Counting Ones

#include <cstdio> #include <cstdlib> using namespace std; #define COL 10 #define ROW 10 int tbl[ROW][COL]; void print(int* tbl) { for (int i=0; i<ROW; i++) { for (int j=0; j<COL; j++) { printf("%11d", tbl[i * COL + j]); } print

1049. Counting Ones (30)

题目如下: The task is simple: given any positive integer N, you are supposed to count the total number of 1's in the decimal form of the integers from 1 to N. For example, given N being 12, there are five 1's in 1, 10, 11, and 12. Input Specification: Ea

PAT甲题题解-1049. Counting Ones-数学问题

n位数,总共有0~10^n-1共计10^n个数那么所有数出现的总次数变为n*(10^n)个数1出现的次数便是十分之一,所以n位数中,1出现的次数为n*10^(n-1)知道这一个后,接下来就方便求了. 举个例子就方便理解了 3125 从头到尾for一遍 3:那么便有三组1000以内的:0~999,1000~1999,2000~29991000以内的1的个数为300,所以共有3*300=900但是又因为1000~1999中千位上的1也要算进去,有1000个所以0~2999中总共有900+1000=1

【PAT甲级】1049 Counting Ones (30 分)(类似数位DP思想的模拟)

题意: 输入一个正整数N(N<=2^30),输出从1到N共有多少个数字包括1. 代码: #define HAVE_STRUCT_TIMESPEC#include<bits/stdc++.h>using namespace std;int main(){ ios::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); int n; cin>>n; int ans=0; int l=0,r=0,low_bit=1,yushu

PAT (Advanced Level) 1049 Counting Ones

题解 now - 当前位的数值 left - 在now左边的所有数字 right - 在now右边的所有数字 mul - right的数量级,如 10,100,100 ① 如果 now == 0 ,当now可以为1时,左边的数值必须为(0 ~ (left-1)),右边的数值可以是 0 ~ 999… ,所以情况有 left*mul ② 如果 now == 1 ,在第①种情况下,再加上right+1,因为当前位已经为1,所以右边的数right在当前范围下是什么无所谓,所以情况为 left*mul+r