思路1:
在for循环中寻找符合条件的------含有7的数字和7的倍数
AC代码:
#include<iostream>
using namespace std;
int main(){
//第一种方式就是一位一位的查找
int n;
while(~scanf("%d",&n))
{
int count=0;
for(int i=1;i<=n;i++)
{
if(i%7==0)
count++;
else if(((i%10)==7)||((i/10)%10==7)||((i/100)%10==7)||((i/1000)%10==7))
{
count++;
}
else
continue;
}
cout<<count;
}
return 0;
}
思路2:
运用C++中的string 中的find查找函数,将数字变成字符串然后转存进数组中
AC代码:
#include<iostream>
using namespace std;
int main(){
//运用数组存放string的方法
int n;
while(~scanf("%d",&n))
{
int count=0;
string str;
for(int i=7;i<=n;i++)
{
if(i%7==0)
count++;
else
{
str=to_string(i);
//find返回的是一个迭代器
int pos=str.find('7');
if(pos!=-1) //如果pos==-1那么就是没有找到‘7‘
count++;
}
}
//用find函数查找含有7的字符串
cout<<count<<endl;
}
return 0;
}
如有错误,多多指教!