分析问题
请求出 1∼n中含有数字 0 的数,有多少个?
遍历从1-n的数字,判断每个数中是否含有0,有就记录一次
判断一数字是否含有0 ,一种是人工拆解,另一种利用循环拆解
建立模型
输入一个数字 n
遍历从1到n
拆解数字并逐位判断是否为0
如果有0 统计加一
编写代码
#include <bits/stdc++.h>
using namespace std;
int main(){
int n,tmp,count=0;
cin>>n;
for(int i=1;i<=n;i++){
tmp=i;//对tmp进行逐位判断
while(tmp){
if(tmp%10 == 0){
count++;
break;
}
tmp=tmp/10;//增量
}
}
return 0;
}