#define _CRT_SECURE_NO_WARNINGS 1
#include <stdio.h>
#include <stdlib.h>
int count_bit_one(unsigned int a)
{
int count_f = 0;
while (a)
{
a = (a - 1) & a;
count_f++;
}
return count_f;
}
int count_diff_bit(unsigned a, unsigned b)
{
int diff = a ^ b;
return count_bit_one(diff);
}
double Pow(int n, int k)
{
//n^k = n * n^(k-1)
if (k < 0)
{
return (1.0 / Pow(n, -k));
}
else if (k == 0)
{
return 1;
}
else
{
return n * Pow(n, k - 1);
}
}
int DigitSum(int num_f)
{
if (num_f > 9)
{
return num_f % 10 + DigitSum(num_f/ 10);
}
else
{
return num_f;
}
}
int main()
{
int a = 0;
scanf("%d", &a);
int count = count_bit_one(a);
printf("%d 二进制位(补码)中1的个数是:%d\n\n", a, count);
int a = 0;
int b = 0;
scanf("%d%d", &a,&b);
int count = count_diff_bit(a,b);
printf("%d 和 %d 二进制位(补码)不同的个数是:%d\n\n", a,b, count);
//n的k次方递归
int k = 0;
int n = 0;
scanf("%d%d", &n, &k);
double ret = Pow(n, k);
printf("ret = %lf\n", ret);
//输入一个数求每一位之和
int num = 0;
scanf("%d", &num);
int ret = DigitSum(num);
printf("%d\n", ret);
//打印三角形
int n = 0;
int i = 0;
int j = 0;
int k = 0;
printf("请输入要打印的三角形的行数>:");
scanf("%d", &n);
for (i = 1; i <= n; i++)
{
for (j = 1; j <= n - i; j++)
{
printf(" ");
}
for (k = 1; k <= 2*i-1; k++)
{
printf("*");
}
printf("\n");
}
system("pause");
return 0;
}
C语言学习笔记19
最新推荐文章于 2024-04-22 22:03:22 发布