zxj3791

导航

C++ || 求一个数中1的位数

点击查看代码
#include<iostream>
using namespace std;

int f(int x)
{
	int count = 0;
	while(x)
	{		
		if (x % 10 == 1)
		{
			++count;
		}
		x /= 10;
	}
	return count;
}

int main()
{
	cout << f(111) << endl;
	return 0;
}

posted on 2022-04-17 16:03  有序  阅读(71)  评论(0编辑  收藏  举报