题解 |简单取模和位运算 #幸运数#
幸运数
https://www.nowcoder.com/practice/4d1afe11171c44a385287e29092cdb3f
#include <stdio.h> int num2(int a) { int i=0; int sum=0; while(a>>i>=1) { int num=(a>>i); sum+=num%2; i++; } return sum; } int num10(int a) { int sum=0; while(a>=1) { sum+=a%10; a/=10; } return sum; } int main() { int num=0; int flag=0; scanf("%d",&num); for(int i=1;i<=num;i++) { if(num2(i)==num10(i)) flag++; } printf("%d\n",flag); }