0
点赞
收藏
分享

微信扫一扫

蓝桥杯--门牌制作--c++和python

德州spark 2022-04-05 阅读 44

c++代码

其中sum用来求2 的总数,
all用来求,每一个数字中包含2的个数,
temp表示对每个数字先取最后一位,再去掉最后一位的操作
j用来存储变量i

需要注意的一点是每次取完一个数字之后要对all进行清零操作。

#include<bits/stdc++.h>
using namespace std;
const int n=2020;
int temp=0;
int all=0;	 
int sum=0;
int main(){
	int i,j;
	for(i=1;i<=n;i++){
		all=0; 
		j=i;
		while(j!=0){
			temp=j%10;
			if(temp==2){
				all++;
			}
			j=j/10;
		}
		sum=sum+all;
	}
	cout<<sum<<endl;
	return 0;
}

python代码:

n=2020
su=0
for i in range(1,n+1):
    j=i
    al=0
    while(j!=0):
        temp=j%10
        if temp ==2:
            al = al + 1
        j=j//10
    su=su+al
print(su)

输出结果:
624

题目链接:
https://www.lanqiao.cn/courses/5494/learning/?id=252366

举报

相关推荐

0 条评论