2020第十一届蓝桥杯大赛软件类国赛题目 C/C++ B 组

试题 A: 美丽的 2

本题总分:5 分
问题描述:在公元 1 年到公元 2020 年(包含)中,有多少个年份的数位中包含数字 2?

#include <stdio.h>
#include <stdlib.h>

/* run this program using the console pauser or add your own getch, system("pause") or input loop */

// 判断年份中是否含有2,即切分年份的每一位,含有2就返回1,否则一直切下去,直到n==0 
// 取数字的每一位:n%10; n=n/10 
int judge(int n){
    while(n!=0){
        if(n%10==2){
            return 1;
        }else{
            n = n/10;
        }
    }
    return 0;
}

int main(int argc, char *argv[]) {
    int i;
    int count=0;
    for(i=1;i<=2020;i++){
        if(judge(i)){
            count++;
        }
    }
    printf("%d",count);
    return 0;
}

运行结果

原文地址:https://www.cnblogs.com/ZZG-GANGAN/p/14663096.html