#include
int i;
int main()
{
i--;
if (i > sizeof(i))
{
printf(">\n");
}
else
{
printf("<\n");
}
return 0;
}
注:如果某个操作符的各个操作数属于不同的类型,那么除非其中一个操作数的转换为另一个操作数的类型,否则操作就无法进行。下面的层次体系称为寻常算术转换。
long double
double
float
unsigned long int
long int
unsigned int
int
如果某个操作数的类型在上面这个列表中排名较低,那么首先要转换为另外一个操作数的类型后执行运算。
int i;//全局变量如果没有初始化,默认会被初始化为0
int main()
{
i--;//-1
//sizeof是一个操作符,这个操作符返回的结果是size_t的,size_t是无符号整型
//算术转换
//11111111111111111111111111111111
//
if (i > sizeof(i))
{
printf(">\n");
}
else
{
printf("<\n");
}
return 0;
}
//>
//方法1
#include
int NumberOf1(int n)
{
int i = 0;
int count = 0;
for (i = 0; i < 32; i++)
{
if (((n >> i) & 1) == 1)
{
count++;
}
}
return count++;
}
int main()
{
int n = 0;
scanf("%d", &n);
int ret = NumberOf1(n);
printf("%d\n", ret);
return 0;
}
//方法2
#include
int NumberOf1(int n)
{
int count = 0;
while (n)
{
if (n % 2 == 1)
count++;
n /= 2;
}
return count;
}
int main()
{
int n = 0;
scanf("%d", &n);
int ret = NumberOf1(n);
printf("%d\n", ret);
return 0;
}
//n=n&(n-1)
//n=13
//1101 - n
//1100 - n-1
//1100 - n
//1011 - n-1
//1000 - n
//0111 - n-1
//0000 - n
//
//方法3
#include
int NumberOf1(int n)
{
int count = 0;
while (n)
{
n = n & (n - 1);
count++;
}
return count++;
}
int main()
{
int n = 0;
scanf("%d", &n);
int ret = NumberOf1(n);
printf("%d\n", ret);
return 0;
}
//方法1
#include
int main()
{
int m = 0;
int n = 0;
scanf("%d %d", &m, &n);
int i = 0;
int diff = 0;
for (i = 0; i < 32; i++)
{
if (((m >> i) & 1) != ((n >> i) & 1))
{
diff++;
}
}
printf("%d\n", diff);
return 0;
}
//方法2
#include
int main()
{
int m = 0;
int n = 0;
scanf("%d %d", &m, &n);
int i = 0;
int diff = 0;
int tmp = m ^ n;//异或,相同为0,不同为1
while (tmp)
{
tmp = tmp & (tmp - 1);
diff++;
}
printf("%d\n", diff);
return 0;
}
//方法1
#include
int main()
{
int m = 0;
scanf("%d", &m);
int i = 0;
//奇数位
for (i = 30; i >= 0; i -= 2)
{
printf("%d ", (m >> i) & 1);
}
printf("\n");
//偶数位
for (i = 31; i >= 1; i -= 2)
{
printf("%d ", (m >> i) & 1);
}
return 0;
}
//方法2
#include
int main()
{
int num = 0;
int i = 0;
scanf("%d", &num);
printf("奇数位:");
for (i = 31; i >= 1; i -= 2)
{
printf("%d ", ((num >> i) & 1));
}
printf("\n");
printf("偶数位:");
for (i = 30; i >= 0; i -= 2)
{
printf("%d ", ((num >> i) & 1));
}
return 0;
}
#include
int main()
{
int a = 10;
int b = 20;
printf("交换前:a = %d b = %d\n", a, b);
a = a ^ b;
b = a ^ b;
a = a ^ b;
printf("交换后:a = %d b = %d\n", a, b);
return 0;
}
//方法1
#include
void print(int *p,int sz)
{
int i = 0;
for (i = 0; i < sz; i++)
{
printf("%d ", *(p + i));
}
}
int main()
{
int arr[] = { 1,2,3,4,5,6,7,8,9,10 };
int sz = sizeof(arr) / sizeof(arr[0]);
print(arr, sz);
return 0;
}
//方法2
#include
int main()
{
int arr[] = { 1,2,3,4,5,6,7,8,9,10 };
int* p = arr;
int i = 0;
for (i = 0; i < 10; i++)
{
printf("%d ", *(p + i));
}
return 0;
}
//方法3
#include
void print(int* p, int sz)
{
int i = 0;
for (i = 0; i < sz; i++)
{
printf("%d ", *p);
p++;
}
}
int main()
{
int arr[] = { 1,2,3,4,5,6,7,8,9,10 };
int sz = sizeof(arr) / sizeof(arr[0]);
print(arr, sz);
return 0;
}
#include
#include
int main()
{
char arr[10000] = { 0 };
//i am a student
//scanf("%s",arr);scanf不能读取空格
gets(arr);
//逆序
int len = strlen(arr);
char* left = arr;
char* right = arr + len - 1;
while (left < right)
{
char tmp = *left;
*left = *right;
*right = tmp;
left++;
right--;
}
printf("%s\n", arr);
return 0;
}
//方法1
#include
int main()
{
int a = 0;
int n = 0;
scanf("%d %d", &a, &n);//2 5
int i = 0;
int sum = 0;
int k = 0;
//2+22+222+2222+22222
for (i = 0; i < n; i++)
{
k = k * 10 + a;
sum += k;
}
printf("%d\n", sum);
return 0;
}
//方法2
#include
int main()
{
int a = 0;
int sum = 0;
printf("请输入一个0-9的数字:");
scanf("%d", &a);
sum = a + a * 11 + a * 111 + a * 1111;
printf("sum=%d\n", sum);
return 0;
}
//方法1
#include
#include
int main()
{
int i = 0;
for (i = 0; i <= 100000; i++)
{
//判断i是否为自幂数
//1.计算i的位数
int n = 1;
int tmp = i;
while (tmp /=10)
{
n++;
}
//2.计算每一位的n的次方之和
tmp = i;
int sum = 0;
while (tmp)//1234
{
sum += (int)pow(tmp % 10, n);
tmp /=10;
}
//比较
if (sum == i)
{
printf("%d ", i);
}
}
return 0;
}
//求3位数的水仙花
#include
int main()
{
int i;
int a, b, c;//a为个位数字,b为十位数字,c为百位数字
for (i = 100; i < 1000; i++)
{
a = i % 10;
b = i / 10 % 10;
c = i / 100;
if (i == (a * a * a + b * b * b + c * c * c))
{
printf("%d\n", i);
}
}
return 0;
}
#include
int main()
{
int n = 0;
scanf("%d", &n);
//打印上半部分n
int i = 0;
for (i = 0; i < n; i++)
{
//打印一行
//打印空格
int j = 0;
for (j = 0; j < n - 1 - i; j++)
{
printf(" ");
}
//打印*
for (j = 0; j < 2 * i + 1; j++)
{
printf("*");
}
printf("\n");
}
//打印下半部分
for (i = 0; i < n-1; i++)
{
//打印一行
//打印空格
int j = 0;
for (j = 0; j <= i; j++)
{
printf(" ");
}
//打印*
for (j = 0; j < (n-1-i)*2-1; j++)
{
printf("*");
}
printf("\n");
}
return 0;
}
//方法1
#include
int main()
{
int money = 0;
scanf("%d", &money);//20
int total = money;
int empty = money;
//置换
while (empty >= 2)
{
total+= (empty / 2);
empty = empty / 2 + empty % 2;
}
printf("%d\n", total);
return 0;
}
//方法2
#include
int main()
{
int money = 0;
scanf("%d", &money);
int total = 0;
if (money <= 0)
{
total = 0;
}
else
{
total = money * 2 - 1;
}
printf("%d\n", total);
return 0;
}
#include
void move(int arr[], int sz)
{
int left = 0;
int right = sz - 1;
while (left < right)
{
while ((left < right) && (arr[left] % 2 == 1))
{
left++;
}
while ((left < right) && (arr[right] % 2 == 0))
{
right--;
}
if (left < right)
{
int tmp = arr[left];
arr[left] = arr[right];
arr[right] = tmp;
}
}
}
void print(int arr[], int sz)
{
int i = 0;
for (i = 0; i < sz; i++)
{
printf("%d ", arr[i]);
}
}
int main()
{
int arr[] = { 1,2,3,4,5,6,7,8,9,10 };
int sz = sizeof(arr) / sizeof(arr[0]);
move(arr, sz);
print(arr, sz);
return 0;
}
#include
#include
my_strlen(const char* str)
{
int count = 0;
assert(str != NULL);
while (*str != '\0')
{
str++;
count++;
}
return count;
}
int main()
{
int len= my_strlen("abcdef");
printf("%d\n", len);
return 0;
}
#include
#include
int main()
{
char arr1[] = "abcdef";
char arr2[] = { 0 };
strcpy (arr2, arr1);
printf("%s\n", arr2);
return 0;
}
int main()
{
unsigned char a = 200;
//00000000000000000000000011001000
//11001000 - a
unsigned char b = 100;
//00000000000000000000000001100100
//01100100 - b
unsigned char c = 0;
c = a + b;
//a和b都要发生整型提升
//存放到c中要发生截断
//
//00000000000000000000000011001000
//00000000000000000000000001100100
//00000000000000000000000100101100
//
//00000000000000000000000000101100 - c
printf(" %d %d", a + b, c);
return 0;
}
//300 44
//1
//1 1
//1 2 1
//1 3 3 1
//1 4 6 4 1
#include
int main()
{
int arr[10][10] = { 0 };
int i = 0;
for (i = 0; i < 10; i++)
{
int j = 0;
for (j = 0; j < 10; j++)
{
if (j == 0)
arr[i][j] = 1;
if (i == j)
arr[i][j] = 1;
if (i >= 2 && j >= 1)
arr[i][j] = arr[i - 1][j - 1] + arr[i - 1][j];
}
}
for (i = 0; i < 10; i++)
{
int j = 0;
int k = 0;
for (k = 0; k < 10 - i; k++)
{
printf(" ");
}
for (j = 0; j <= i; j++)
{
printf("%d ", arr[i][j]);
}
printf("\n");
}
return 0;
}
日本某地发生了一件谋杀案,警察通过排查确定杀人凶手必为4个嫌疑犯的一个。以下为4个嫌疑犯的供词:A说:不是我。B说:是C。C说:是D。D说:C在胡说。
已知3个人说了真话,1个人说的是假话。现在请根据这些信息,写一个程序来确定到底谁是凶手。
#include
int main()
{
int killer = 0;
//a b c d
for (killer = 'a'; killer <= 'd'; killer++)
{
if ((killer != 'a') + (killer == 'c') + (killer == 'd') + (killer!='d') == 3)
{
printf("%c\n",killer);
}
}
return 0;
}
5位运动员参加了10米台跳水比赛,有人让他们预测比赛结果:A选手说:B第二,我第三;B选手说:我第二,E第四;C选手说:我第一,D第二;D选手说:C最后,我第三;E选手说:我四,A第一;
比赛结束后,每位选手都说对了一半,请编程确定比赛的名次。
#include
int main()
{
int a = 0;
int b = 0;
int c = 0;
int d = 0;
int e = 0;
for (a = 1; a <= 5; a++)
{
for (b = 1; b <= 5; b++)
{
for (c = 1; c <= 5; c++)
{
for (d = 1; d <= 5; d++)
{
for (e = 1; e <= 5; e++)
{
if (((b == 2) + (a == 3) == 1) &&
((b == 2) + (e == 4) == 1) &&
((c == 1) + (d == 2) == 1) &&
((c == 5) + (d == 3) == 1) &&
((e == 4) + (a == 1) == 1))
{
if(a*b*c*d*e == 120)
printf("a=%d b=%d c=%d d=%d e=%d\n", a, b, c, d, e);
}
}
}
}
}
}
return 0;
}
输入描述:
一行输入整数n(0 <= n < 31)。
输出描述:
输出对应的2的n次方的结果。
#include
int main()
{
int n = 0;
scanf("%d",&n);
printf("%d\n", 1 << n);
return 0;
}
输入描述:
每一行,输入7个整数(0~100),代表7个成绩,用空格分隔。
输出描述:
每一行,输出去掉最高分和最低分的平均成绩,小数点后保留2位,每行输出后换行。
#include
int main()
{
int i = 0;
int score = 0;
int max = 0;
int min = 100;
int sum = 0;
for (i = 0; i < 7; i++)
{
scanf("%d", &score);
if (score > max)
{
max = score;
}
if (score < min)
{
min = score;
}
sum += score;
}
printf("%.2f\n", (sum - max - min) / 5.0);
return 0;
}
#include
int main()
{
int y = 0;
int m = 0;
int days[12] = { 31,28,31,30,31,30,31,31,30,31,30,31 };
while (scanf("%d %d", &y, &m) == 2)
{
int day = days[m - 1];
if ((y % 4 == 0 && y % 100 != 0) || (y % 400 == 0))
{
if (m == 2)
day += 1;
}
printf("%d\n", day);
}
return 0;
}