///*
//**查找一个数的二进制形式中‘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;
}