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.

Input Specification:

Each input file contains one test case which gives the positive N (<=230).

Output Specification:

For each test case, print the number of 1‘s in one line.

Sample Input:

12

Sample Output:

5
 1 #include <iostream>
 2 #include <cmath>
 3 #include <string>
 4
 5 using namespace std;
 6
 7 int ToInt(string num)
 8 {
 9     int res = 0;
10     for (int i = 0; i < num.size(); i++)
11         res = res * 10 + num[i] - ‘0‘;
12     return res;
13 }
14
15 int CountingOnes(string num)
16 {
17     if (num.size() == 1)
18     {
19         if (num == "0")
20             return 0;
21         else
22             return 1;
23     }
24     else
25     {
26         int total = 0;
27         int first = num[0] - ‘0‘;
28         string sub = string(num.begin() + 1, num.end());
29         if (first == 1)
30             total += (ToInt(sub) + 1);
31         else if (first > 1)
32             total += pow(10, sub.size());
33         total += CountingOnes(sub) + first * CountingOnes(string(sub.size(), ‘9‘));
34         return total;
35     }
36 }
37
38 int main()
39 {
40     string num;
41     cin >> num;
42
43     cout << CountingOnes(num);
44 }
时间: 2024-12-28 20:45:38

PAT 1049. Counting Ones (30)的相关文章

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

PAT 1004. Counting Leaves (30)

A family hierarchy is usually presented by a pedigree tree.  Your job is to count those family members who have no child. Input Each input file contains one test case. Each case starts with a line containing 0 < N < 100, the number of nodes in a tre

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 分)(类似数位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 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: Each

PAT 1004. Counting Leaves (30) C#实现

A family hierarchy is usually presented by a pedigree tree. Your job is to count those family members who have no child. Input Each input file contains one test case. Each case starts with a line containing 0 < N < 100, the number of nodes in a tree

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

pat1049. Counting Ones (30)

1049. Counting Ones (30) 时间限制 10 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue 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 examp