content
2. Adjust the order of odd and even numbers
1. Drinking soda problems
//Drink soda, 1 bottle of soda is 1 yuan, 2 empty bottles can be exchanged for a bottle of soda, give 20 yuan, how much soda can be
// Method 1: normal solution
#include<stdio.h>
int main()
{
int money = 0;
int total = 0;
scanf("%d", &money);
int empty = 0;
total += money;
empty += money;
while (empty >= 2)
{
total += empty / 2;
empty = empty / 2 + empty % 2;
}
printf("%d\n", total);
return 0;
}
When we give enough examples, we can find a rule: the number of bottles = the number of money * 2-1
Therefore, the second method (special solution) is derived
#include<stdio.h>
int main()
{
int money = 0;
int total = 0;
scanf("%d", &money);
if (money <= 0)
total = 0;
else
total = money * 2 - 1;
printf("%d\n", total);
return 0;
}
2. Adjust the order of odd and even numbers
Require:
//Input an integer array, implement a function,
//to adjust the order of the numbers in the array so that all odd numbers in the array are in the first half of the array,
//all even numbers are in the second half of the array.
#include<stdio.h>
void print(int arr[], int sz)
{
int i = 0;
for (i = 0; i <sz; i++)
{
printf("%d ", arr[i]);
}
}
void move(int arr[], int sz)
{
int* left = arr;
int* right = arr + sz - 1;
while (left < right)
{
//从左向右找一个偶数
while ((left < right) && *left % 2 != 0)
{
left++;
}
//从右向左找一个奇数
while ((left < right) && *right % 2 != 1)
{
right--;
}
//将奇数和偶数相交换
if (left < right)
{
int tmp = 0;
tmp = *left;
*left = *right;
*right = tmp;
}
}
}
int main()
{
int arr[] = { 1,2,3,4,5,6,7,8,9,10 };
int sz = sizeof(arr) / sizeof(arr[0]);
print(arr, sz);
move(arr, sz);
printf("\n");
print(arr, sz);
return 0;
}
3. Yanghui Triangle
//1
//1 1
//1 2 1
//1 3 3 1
//……
#include<stdio.h>
int main()
{
int i = 0;
int j = 0;
int arr[10][10] = { 0 };
for (i = 0; i < 10; i++)
{
for (j = 0; j <=i; 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] + arr[i - 1][j - 1];
}
printf("%d ", arr[i][j]);
}
printf("\n");
}
return 0;
}
4. Guess the murderer
//The following are the confessions of the 4 suspects:
//A said: not me.
//B says: yes C.
//C says: yes D.
//D says: C is talking nonsense
//3 people are known to tell the truth, and 1 person is false.
// Now, based on this information, write a program to determine who the murderer is.
#include<stdio.h>
int main()
{
int killer = 0;
for (killer = 'a'; killer <= 'd'; killer++)
{
if ((killer != 'a') + (killer == 'c') + (killer == 'd') + (killer != 'd') == 3)
{
printf("%c\n", killer);
}
}
return 0;
}
5. Guess the ranking
//5 athletes participated in the 10m platform diving competition, and someone asked them to predict the result of the competition:
//Athlete A said: B was second, I was third;
//Athlete B said: I was second, E was fourth;
// Player C said: I was first, D was second;
// Player D said: C was last, I was third;
// Player E said: I was fourth, A was first;
// After the game, each player said Half right, please program the ranking of the competition.
#include<stdio.h>
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;
}