【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=0;//当前位左边的数字,当前位右边的数字,当前位,当前位上的数字
while(n/low_bit){
l=n/(10*low_bit);
yushu=n/low_bit%10;
r=n%low_bit;
if(!yushu)
ans+=l*low_bit;//当前位为1时,左边数字可以从0~l-1,故有l*low_bit
else if(yushu==1)
ans+=l*low_bit+r+1;//当前位为1时,有为0时加上右边数字为0~r,故有l*low_bit+r+1
else
ans+=(l+1)*low_bit;//当前位大于1时,左边数字从0~l,当前位只要为1就符合题意,故有(l+1)*low_bit
low_bit*=10;//当前位左移
}
cout<<ans;
return 0;
}

原文地址:https://www.cnblogs.com/ldudxy/p/11616744.html

时间: 2024-08-29 17:42:50

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

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 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 甲级 1016 Phone Bills (25 分) (结构体排序,模拟题,巧妙算时间,坑点太多,debug了好久)

1016 Phone Bills (25 分)   A long-distance telephone company charges its customers by the following rules: Making a long-distance call costs a certain amount per minute, depending on the time of day when the call is made. When a customer starts connec

PAT 甲级 1105 Spiral Matrix (25分)(螺旋矩阵,简单模拟)

1105 Spiral Matrix (25分) This time your job is to fill a sequence of N positive integers into a spiral matrix in non-increasing order. A spiral matrix is filled in from the first element at the upper-left corner, then move in a clockwise spiral. The

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 甲级 1014 Waiting in Line (30 分)(queue的使用,模拟题,有个大坑)

1014 Waiting in Line (30 分) Suppose a bank has N windows open for service. There is a yellow line in front of the windows which devides the waiting area into two parts. The rules for the customers to wait in line are: The space inside the yellow line

【PAT甲级】1070 Mooncake (25 分)(贪心水中水)

题意: 输入两个正整数N和M(存疑M是否为整数,N<=1000,M<=500)表示月饼的种数和市场对于月饼的最大需求,接着输入N个正整数表示某种月饼的库存,再输入N个正数表示某种月饼库存全部出手的利润.输出最大利润. trick: 测试点2可能包含M不为整数的数据.(尽管题面说明M是正整数,可是根据从前PAT甲级题目的经验,有可能不是整数.....) 代码: #define HAVE_STRUCT_TIMESPEC#include<bits/stdc++.h>using names

【PAT甲级】1068 Find More Coins (30 分)(背包/DP)

题意: 输入两个正整数N和M(N<=10000,M<=10000),接着输入N个正整数.输出最小的序列满足序列和为M. 代码: #define HAVE_STRUCT_TIMESPEC#include<bits/stdc++.h>using namespace std;int a[10007];int dp[107];int vis[10007][107];int main(){ ios::sync_with_stdio(false); cin.tie(NULL); cout.ti

PAT甲级——A1155 HeapPaths【30】

In computer science, a heap is a specialized tree-based data structure that satisfies the heap property: if P is a parent node of C, then the key (the value) of P is either greater than or equal to (in a max heap) or less than or equal to (in a min h