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 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

题目分析

已知一个正整数N,求1~N的正整数中出现1的次数(如:11算出现两次1)

解题思路

从低位到高位,计算每一位为1时的数字个数,进行累加

  • 如果当前位为0, 左边为0~left-1时都可以在当前位取1(此时右边可以取到的数字总数为0~99999...即:a个),因为当前位为0,所以左边取left时不能取1。
  • 如果当前位为1,左边为0~left-1时都可以在当前位取1(此时右边可以取到的数字总数为0~99999...即:a个),当前位为1时,右边只可以取到0~right(即:right个数)
  • 如果当前位大于2,左边为0~left时都可以在当前位取1(此时右边可以取到的数字总数为0~99999...即:a个)

易错点

  1. 若枚举1~N数字,再对数字统计出现1的次数,会超时(测试点4,6)

知识点

  1. 已知正整数为N,从右往左,指针i指向N的某个位置,a当前位的权重(如:i=2,a=10^2;i=0,a=10^0)
N/(a*10) //取指针i指向的数字左边数字
N/a%10 //取指针i指向的数字
N%a //取指针i指向的数字右边数字

Code

Code 01

#include <iostream>
using namespace std;
/*
测试点4,6超时
*/
int main(int argc,char * argv[]) {
    int n,t=0,a=1,left,now,right;
    scanf("%d",&n);
    while(n/a>0) {
        left = n/(a*10),now=n/a%10,right=n%a;
        if(now==0)t+=left*a;
        else if(now==1)t+=left*a+right+1;
        else t+=(left+1)*a;
        a*=10;
    }
    printf("%d",t);
    return 0;
}

原文地址:https://www.cnblogs.com/houzm/p/12258899.html

时间: 2024-10-01 04:09:51

PAT Advanced 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 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 (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

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 (Advanced Level) 1057. Stack (30)

树状数组+二分. #include<iostream> #include<cstring> #include<cmath> #include<algorithm> #include<cstdio> #include<map> #include<queue> #include<string> #include<stack> #include<vector> using namespace

PAT:1004. Counting Leaves (30) AC

#include<stdio.h> #include<vector> const int MAX=510; using namespace std; int n,m,le=0; //节点数,非叶子节点数,最深层叶层数 vector<int> child[MAX]; //存储孩子情况 int number[MAX]; //每一层叶子数 void DFS(int s,int l) { if(child[s].size()==0) { ++number[l]; if(le&l

PAT Advanced 1004 Counting Leaves

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

PAT Advanced 1155 Heap Paths (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

PAT Advanced 1155 Heap Paths (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