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

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值