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+right+1

  ③ 如果 now > 2 ,在第①种情况下,再加上mul,因为now > 2,所以now = 1时,右边的数是什么无所谓,不会超范围,所以情况为 left*mul+mul

代码

#include<bits/stdc++.h>
using namespace std;
int main()
{
    int n,left,right,mul=1,now,ans=0;
    scanf("%d",&n);
    while(n/mul)
    {
        left=n/(mul*10);
        right=n%mul;
        now=n/mul%10;
        if(now==0)  ans+=left*mul;
        else if(now==1) ans+=left*mul+right+1;
        else    ans+=left*mul+mul;
        mul*=10;
    }
    printf("%d",ans);
    system("pause");
    return 0;
}

原文地址:https://www.cnblogs.com/VividBinGo/p/12236353.html

时间: 2024-07-30 15:31:25

PAT (Advanced Level) 1049 Counting Ones的相关文章

pat advanced level 1049

1049 Counting Ones (30)(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.

PAT (Advanced Level) 1004. Counting Leaves (30)

简单DFS. #include<iostream> #include<cstring> #include<cmath> #include<algorithm> #include<cstdio> #include<vector> using namespace std; const int maxn=100+10; vector<int>g[maxn]; int n,m; int ans[maxn]; int root; i

PAT (Advanced Level) 1115. Counting Nodes in a BST (30)

简单题.统计一下即可. #include<cstdio> #include<cstring> #include<cmath> #include<vector> #include<map> #include<queue> #include<stack> #include<algorithm> using namespace std; const int maxn=100000+10; struct Node {

Pat(Advanced Level)Practice--1043(Is It a Binary Search Tree)

Pat1043代码 题目描述: A Binary Search Tree (BST) is recursively defined as a binary tree which has the following properties: The left subtree of a node contains only nodes with keys less than the node's key. The right subtree of a node contains only nodes

Pat(Advanced Level)Practice--1044(Shopping in Mars)

Pat1044代码 题目描述: Shopping in Mars is quite a different experience. The Mars people pay by chained diamonds. Each diamond has a value (in Mars dollars M$). When making the payment, the chain can be cut at any position for only once and some of the diam

PAT (Advanced Level) 1093. Count PAT&#39;s (25)

预处理每个位置之前有多少个P,每个位置之后有多少个T. 对于每个A,贡献的答案是这个A之前的P个数*这个A之后T个数. #include<cstdio> #include<cstring> long long MOD=1e9+7; const int maxn=1e5+10; long long dp1[maxn],dp2[maxn]; char s[maxn]; int main() { scanf("%s",s); memset(dp1,0,sizeof d

PAT (Advanced Level) 1055. The World&#39;s Richest (25)

排序.随便加点优化就能过. #include<iostream> #include<cstring> #include<cmath> #include<algorithm> #include<cstdio> #include<map> #include<queue> #include<string> #include<stack> #include<vector> using names

Pat(Advanced Level)Practice--1018(Public Bike Management)

Pat1018代码 题目描述: There is a public bike service in Hangzhou City which provides great convenience to the tourists from all over the world. One may rent a bike at any station and return it to any other stations in the city. The Public Bike Management C

Pat(Advanced Level)Practice--1076(Forwards on Weibo)

Pat1076代码 题目描述: Weibo is known as the Chinese version of Twitter. One user on Weibo may have many followers, and may follow many other users as well. Hence a social network is formed with followers relations. When a user makes a post on Weibo, all hi