查找一个数的二进制形式中‘1’的个数

///*
//**查找一个数的二进制形式中‘1’的个数**
//*/
# include <stdio.h>
int main(void)
{
	int i, j, k = 31;
	int a[32];
	int count = 0;
	printf("请输出十进制数字\n");
	scanf_s("%d", &i);
	while (i != 0)
	{
		j = i % 2;
		if (j == 1)
		{
			count++;
		}
		i = i / 2;
		a[k] = j;
		k--;
	}
	k++;
	for (; k <= 31; k++)
		printf("%d", a[k]);
	printf("\n");
	printf("%d\n",count);
	printf("\n");
	printf("main over\n");
}
/*
  利用 位与 进行优化
*/
#include <stdio.h>
#include <iostream>

using namespace std;

int main(void)
{
	int x = 0;
	int count = 0;
	cin >> x;
	while (x)
	{
		x = x & (x - 1);
		count++;
	}
	cout << count << endl;
	return 0;
}









/*
****输出100 -- 200 之间的质数*******

*/

#include<stdio.h>
#include<math.h>
#include<iostream>

using namespace std;



bool zhishu(int n)
{
    if (n < 2)
    {
        return false;
    }
        
    if (n == 2)
    {
        return true;
    }
        

    if (n % 2 == 0)
    {
        return false;
    }

    int i = n / 2;                                   //优化处理  :  int i = sqrt(n);
    for (int j = 2; j < i; ++j)                  //                         j <= i
    {
        if (n%j == 0)
        {
            return false;
        }
            
    }
    return true;
        
}

    
int main(void)
{
    /*int cur;
    printf("please input a number\n");
    scanf_s("%d",&cur);*/
    for (int cur = 100; cur < 200; ++cur)
    {
        if (zhishu(cur) == 1)
        cout << cur << endl;
    }
    return 0;
}        
   





/*
*********
输出9*9 乘法口诀表
*********

*/

# include <stdio.h>
#include<iostream>

using namespace std;

int main(void)
{
    for (int i = 1; i <= 9; ++i)
    {
        for (int j = 1; j <= i; ++j)
        {
            cout << j << '*' << i << '=' << j*i<<' ';
        }
        cout << endl;
    }
}        






*********
不使用临时变量转换两个变量的值
*********
#include<stdio.h>

#include<iostream>

using namespace std;
int main(void)
{
    int x = 0;
    int y = 0;


    cout << "Please input two numbers:" << endl;
    cin >> x;
    cin >> y;


    cout << "Before swap   " << x << ' ' << y << endl;
    x = x ^ y;
    y = x ^ y;
    x = x ^ y;

    cout << "After  swap   " << x << ' ' << y << endl;
    return 0;

 }



*********
求10个数中最大的
*********






#include <stdio.h>
#define LENTGH 10

int main(void)
{
    int a[LENTGH];
    int count;
    int max = a[0];                 
    printf("输入十个数\n");

    for (count = 0; count<10; count++)
    {
        scanf_s("%d", &a[count]);

        if (max<a[count]) max = a[count];    //把max和每个输入的数字进行比较
    }

    printf("最大值%d\n", max);

    return 0;

}


  • 3
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值