美文网首页PAT
甲级| 1049.Counting Ones

甲级| 1049.Counting Ones

作者: yzbkaka | 来源:发表于2019-08-10 17:57 被阅读2次

题目描述

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.

输入描述

Each input file contains one test case which gives the positive N (≤2​^30​​).

输出描述

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

输入例子

12

输出例子

5

我的代码

#include<stdio.h>
int count=0;
void getAns(int a){
    int k;
    if(a<10 && a==1){
        count++;
    }
    if(a>=10){
        while(a>=10){
            k=a%10;
            a=a/10;
            if(k==1){
                count++;
            }
            if(a==1){
                count++;
            }
        }
    }
}

int main(){
    int n,sum=0;
    scanf("%d",&n);
    for(int i=1;i<=n;i++){
        getAns(i);
    }
    printf("%d",count);
    return 0;
}

相关文章

  • 甲级| 1049.Counting Ones

    题目描述 The task is simple: given any positive integer N, yo...

  • numpy,scipy相关

    np.ones_likeReturn an array of ones with the same shape a...

  • What’s Behind Irrational Decisio

    We’re defined by our decisions, good ones or bad ones, in...

  • ones zeros函数

    一、ones产生全1数组,zeros产生全零数组。 ones(a,b)产生a行b列全1数组 ones(a)产生a行...

  • Python数据分析:Numpy学习笔记

    Numpy学习笔记 ndarray多维数组 创建 数组创建函数 arange ones/ones_like zer...

  • Ones and Zeroes

    题目In the computer world, use restricted resource you have...

  • Beloved ones

    我看到那个荷兰男人褐色的眼睛,深邃的,如若一汪深潭。 空气中一缕缕灰尘落入他黄褐色的瞳仁里,在那深邃中一圈一圈扩散...

  • Ones and Zeroes

    题目来源给你一堆0和1组成的字符串,以及m个1,n个0,问m个1,n个0最多能组成多少个字符串。没想到怎么用DP来...

  • The crazy ones

    The Crazy OnesAlbert Einstein, Bob Dylan, Martin Luther K...

  • Ones and Zeros

    才发现别人用的是mardown zzz题目地址:https://leetcode.com/problems/one...

网友评论

    本文标题:甲级| 1049.Counting Ones

    本文链接:https://www.haomeiwen.com/subject/sharjctx.html