题目描述
输入描述:
每组数据输入一个数n(n<=100000)
输出描述:
每组数据输出一行,小于等于n的幸运数个数。
#include<bits/stdc++.h>
using namespace std;
int main()
{
int n;
while(cin>>n)
{
int count=0;
for(int i=1;i<=n;i++)
{
int temp1=i,temp2=i,count1=0,count2=0;
while(temp1!=0)
{
count1+=temp1%10;
temp1=temp1/10;
}
// cout<<count1<<endl;
while(temp2!=0)
{
count2+=temp2%2;
temp2=temp2/2;
}
// cout<<count2<<endl;
if(count1==count2)
count++;
}
cout<<count<<endl;
}
return 0;
}
函数版
#include<bits/stdc++.h>
using namespace std;
int shijinzhi(int temp1)
{
int count1=0;
while(temp1!=0)
{
count1+=temp1%10;
temp1=temp1/10;
}
return count1;
}
int erjinzhi(int temp2)
{
int count2=0;
while(temp2!=0)
{
count2+=temp2%2;
temp2=temp2/2;
}
return count2;
}
int main()
{
int n;
while(cin>>n)
{
int count=0;
for(int i=1;i<=n;i++)
{
int count1=shijinzhi(i);
int count2=erjinzhi(i);
//int temp1=i,temp2=i,count1=0,count2=0;
// while(temp1!=0)
// {
// count1+=temp1%10;
// temp1=temp1/10;
//}
// cout<<count1<<endl;
// while(temp2!=0)
// {
// count2+=temp2%2;
// temp2=temp2/2;
//}
// cout<<count2<<endl;
if(count1==count2)
count++;
}
cout<<count<<endl;
}
return 0;
}