当前位置:网站首页>C language exercise 1
C language exercise 1
2022-06-13 04:47:00 【Stretch the curtain with the wind】
1. Given the number of seconds seconds , Turn seconds into hours 、 Minutes and seconds .
describe
Given the number of seconds seconds , Turn seconds into hours 、 Minutes and seconds .
Data range :0< seconds<100000000
Input description :
a line , Include an integer , That is, given the number of seconds .
Output description :
a line , Contains three integers , Enter the number of hours corresponding to the integer 、 Minutes and seconds ( It could be zero ), Separated by a space .
My code :
#define _CRT_SECURE_NO_WARNINGS 1
#include<stdio.h>
int main()
{
int a = 0;
int x = 0;
int y = 0;
int z = 0;
scanf("%d", &a);
x = a / 3600;
y = (a - 3600 * x) / 60;
z = a - 3600 * x - 60 * y;
printf("%d %d %d\n", x, y, z);
return 0;
}
Code :
2. Input 5 Results of students , Ask for their average score .
describe :
Input from keyboard 5 Results of students ( Integers ), Ask for their average score ( Floating point numbers , Keep one decimal place ).
Input description :
a line , Continuous input 5 It's an integer ( Range 0~100), Separate... With spaces .
Output description :
a line , Output 5 The average number of pieces ( Keep one decimal place ).
My code :
#define _CRT_SECURE_NO_WARNINGS 1
#include<stdio.h>
int main()
{
int a = 0;
int b = 0;
int c = 0;
int d = 0;
int e = 0;
float x = 0;
scanf("%d %d %d %d %d", &a, &b, &c, &d, &e);
x = (a + b + c + d + e) / 5.0;
printf("%.1f\n", x);
return 0;
}
Code 1:
Code 2( Improved version ):
3. Put a four digit number , Reverse output .
describe :
Put a four digit number , Reverse output .
Input description :
a line , Enter an integer n(1000 <= n <= 9999).
Output description :
Enter... For each group , The reverse output corresponds to four digits .
My code :
#define _CRT_SECURE_NO_WARNINGS 1
#include<stdio.h>
int main()
{
int x = 0;
int a = 0;
int b = 0;
int c = 0;
int d = 0;
scanf("%d", &x);
a = x % 10;
b = (x / 10) % 10;
c = (x / 100) % 10;
d = x / 1000;
printf("%d%d%d%d\n",a,b,c,d);
return 0;
}
Code 1:
Code 2( Sub version of the topic ):
4. Output three integers from large to small .
describe :
Write code to output three integers from large to small .
My code :
#define _CRT_SECURE_NO_WARNINGS 1
#include<stdio.h>
int main()
{
int a = 0;
int b = 0;
int c = 0;
int d = 0;
scanf("%d %d %d", &a, &b, &c);
if (a > b)
{
if (a > c)
{
if (b > c)
{
printf("%d %d %d\n", a, b, c);
}
else
{
printf("%d %d %d\n", a, c, b);
}
}
else
{
printf("%d %d %d\n", c, a, b);
}
}
else
{
if (b > c)
{
if(a>c)
{
printf("%d %d %d\n", b, a, c);
}
else
{
printf("%d %d %d\n", b, c, a);
}
}
else
{
printf("%d %d %d\n", c, b, a);
}
}
return 0;
}
Code 1:
5. greatest common divisor ( Minimum common multiple )
describe :
Given two numbers , Find the greatest common divisor of these two numbers
My code :
#define _CRT_SECURE_NO_WARNINGS 1
#include<stdio.h>
int main()
{
int a = 0;
int b = 0;
int i = 0;
scanf("%d %d", &a, &b);
if (a > b)
{
i = b;
while(i>=1)
{
if (a % i == 0 && b % i == 0)
{
printf("%d\n", i);
break;
}
i--;
}
}
else
{
i = a;
while (i >= 1)
{
if (a % i == 0 && b % i == 0)
{
printf("%d\n", i);
break;
}
i--;
}
}
return 0;
}
Code 1:
Code 2( division ):
The method of calculating the least common multiple :
1. Start with the two larger values and add one , Until there are several numbers that can divide them .
2. First, find the greatest common divisor , The least common multiple is the multiplication of these two numbers by the greatest common divisor .
6. Print leap year
describe :
Print 1000 Year to 2000 Leap year between years
My code :
#define _CRT_SECURE_NO_WARNINGS 1
#include<stdio.h>
int main()
{
int i = 1000;
while (i <= 2000)
{
if (i % 4 == 0 && i % 100 != 0)
{
printf("%d ", i);
}
else if (i % 400==0)
{
printf("%d ", i);
}
else
{
;
}
i++;
}
return 0;
}
Code 1:
Code 2:
7. Print prime numbers
describe :
Write a code : Print 100~200 The prime between
My code :
#define _CRT_SECURE_NO_WARNINGS 1
#include<stdio.h>
int main()
{
int i = 100;
int a = 2;
while (i <= 200)
{
while (a < i)
{
if (a == i - 1)
{
printf("%d ", i);
a = 2;
i++;
break;
}
if (i % a != 0)
{
a++;
continue;
}
if (i % a == 0)
{
i++;
a = 2;
break;
}
}
}
return 0;
}
Code 1:
Code 2( Improved version ):
Code 3( Super improved version ):
8. Count 9 The number of
describe :
Write a program, count 1 To 100 How many numbers appear in all integers of 9
My code :( error )
error correction : Should be a%9==0 and b%9==0 Change to a=9 and b=9, because 0%9 Also for the 0, disqualification .
Code 1:
answer :
9. Sum fractions
describe :
Calculation 1/1-1/2+1/3-1/4+1/5 …… + 1/99 - 1/100 Value , Print out the results
My code :( error )
error correction :
1. Score , Both ends of the division sign need to have decimals .
2.sum Should be defined as a floating point number .
Code 1:
answer :
10. For maximum
describe :
seek 10 The maximum of integers
My code :
Code 1:
11. Multiplication table
describe :
Output... On the screen 9*9 Multiplication table
My code :
Code 1:( There is a problem )
Running results 1:
notes : There are some places printed here that are not aligned , Because some places print one , Some places print two digits . resolvent : take %d Change to %2d that will do ,%2d When we print some numbers, we need two digits , If there are less than two digits, the blank space will be filled in , At this time, the digit is right , If change to %-2d Words , If there are not enough two characters, they will fill in the blank space , The digits are left , The following code 2 And code 3.
Code 2:
Running results 2:
Code 3:
Running results 3:
12. A code explanation
notes :
1. When the array passes parameters, it passes the address of the first element , Write arr[0] In fact, you can find the address of the first element through this address .
That is, in the main function , The function name can represent the array , It also means the address of the first element . When a function calls an array , Whether the formal parameter is received as a pointer or as arr[ ] This form of array receives ( These two forms have exactly the same meaning and function ), Only the address of the first element of the array is received , It's just that you can find it in the function by the address of the first element of the array arr[1] And so on , So you can also use... In functions arr[1] etc. .
2. The array name is equivalent to the address , So the time of transmission is the address , Equivalent to address calling .
13. Write function and print multiplication table
describe :
Implement a function , Print multiplication table , The number of rows and columns in the formula table can be specified by yourself
Such as : Input 9, Output 9*9 Pithy formula table , Output 12, Output 12*12 Table of multiplication formulas for .
My code :
Code 1:
14. Reverse string order ( Recursive implementation )
describe :
Write a function reverse_string(char * string)( Recursive implementation )
Realization : Invert the characters in the parameter string , Not in reverse order .
requirement : Out of commission C String manipulation functions in the function library .
such as :
char arr[] = "abcdef";
In reverse order, the contents of the array become :fedcba
Code 1:( Cycle to achieve , Parameters are in array form )
Code 2 :( Cycle to achieve , Parameters are in pointer form )
Code 3:( Recursive implementation , Parameters are in pointer form )
15. Calculate the sum of each of a number ( Recursive implementation )
describe :
Write a recursive function DigitSum(n), Enter a non negative integer , Returns the sum of the numbers that make up it
for example , call DigitSum(1729), You should go back to 1+7+2+9, Its sum is 19
Input :1729, Output :19
Code :
16. Recursive implementation n Of k Power
describe :
Write a function to implement n Of k Power , Use recursion to implement .
Code :
17、 The result of the following code is
#include <stdio.h>
int main()
{
int a, b, c;
a = 5;
c = ++a;
b = ++c, c++, ++a, a++;
b += a++ + c;
printf("a = %d b = %d c = %d\n:", a, b, c);
return 0;
}
answer :
18、 In statistical binary 1 The number of
describe :
Write a function to return the parameter binary 1 The number of .(n A complement placed in memory 2 In base 1 The number of )
such as : 15 0000 1111 4 individual 1
My code :
#include<stdio.h>
int one_point(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 s=one_point(n);
printf("%d\n", s);
return 0;
}
Code 1:( error , Only positive numbers , In the negative number memory, there is a complement , Can't calculate )
Code 2:( correct )
Code 3:( correct )
Code 4:( correct )( There are several 1 Cycle a few times , Code is more efficient )
notes :
1. Code 1,m=15
m=15 Binary system :00000000000000000000000000001111
Get the last 1(15%2=1), Then put the last one 1 Get rid of (15/2=7)( Same as decimal system )
15%2=1:00000000000000000000000000000001
15/2=7:00000000000000000000000000000111
2. Code 2,n=-1 After passing, it is unsigned , The program will put -1 The complement of is translated into an integer , Calculate
3. Code 3,m=-1
m=-1 The original code of :10000000000000000000000000000001
m=-1 The inverse of :11111111111111111111111111111110
m=-1 Complement :11111111111111111111111111111111
Want to find the lowest 1(m&1), Don't do this 1(m>>=1)
4. Code 4,m=15
m=m&(m-1), The analysis is as follows
m: 001111
m-1: 001110
m=m&(m-1): 001110
m-1:001101
m=m&(m-1): 001100
m-1:001011
m=m&(m-1):001000
m-1:000111
m=m&(m-1):000000
m=m&(m-1) This expression will put m The rightmost binary sequence of 1 Get rid of ,m=m&(m-1) Several times , It means that there are several 1.
This expression can determine whether a number is 2 Of k Power (m=m&(m-1) If this expression yields a number equal to 0, Then the number is 2 Of k Power )
19、 Find the number of different bits in two binary numbers
describe :
Programming to realize : Two int(32 position ) Integers m and n In the binary representation of , How many bits (bit) Different ?
Input example :
1999 2299
Output example :7
My code :
#include<stdio.h>
int main()
{
int x = 0;
int y = 0;
scanf("%d %d", &x, &y);
int z = x ^ y;
int i = 0;
int count = 0;
for (i = 0; i < 32; i++)
{
if (z & 1 == 1)
{
count++;
}
z = z >> 1;
}
printf("%d\n", count);
return 0;
}
Code 1:
Code 2:
notes : XOR operator , Same as 0, Dissimilarity is 1
20、 Print odd and even bits of integer binary
describe :
Get all even and odd bits in an integer binary sequence , Print out the binary sequence separately
My code :
#include<stdio.h>
void print(int n)
{
int i = 0;
// Print odd digits
printf(" Odd number : ");
for (i = 30; i >= 0; i -= 2)
{
printf("%d ", (n >> i) & 1);
}
printf("\n");
// Print even numbers
printf(" Even digit : ");
for (i = 31; i >= 1; i -= 2)
{
printf("%d ", (n >> i) & 1);
}
}
int main()
{
int n = 0;
scanf("%d", &n);
print(n);
return 0;
}
Code 1:
answer :
21、 The following code runs as a result
#include <stdio.h>
int i;
int main()
{
i--;
if (i > sizeof(i))
{
printf(">\n");
}
else
{
printf("<\n");
}
return 0;
}
Running results :
notes :
1. If a global variable is not initialized , The default is 0
2.sizeof The type of result evaluated by the operator is size_t, It's an unsigned integer , Signed and unsigned integers are evaluated , Will convert signed integers to unsigned integers
-1 Binary source code of :10000000000000000000000000000001
-1 Binary inverse code :11111111111111111111111111111110
-1 Complement :11111111111111111111111111111111
In this case, if the type is int The system will -1 The complement of is transformed into the original code for output , And if the type changes to an unsigned number , Then the number is considered to be a positive number , The original code is the same as the inverse code and the complement , here 11111111111111111111111111111111 The decimal system is 4294967295.
22、 Judge integer parity
describe :
KiKi Want to know the parity of an integer , Please help him judge . Enter any integer from the keyboard
( Range -231~231-1), Program to judge its parity .
Input description :
Multi group input , Each line of input includes an integer .
Output description :
Enter... For each line , The output number is odd (Odd) Or even (Even).
Code 1:
Code 2:
notes :
1.EOF:end of file, Is the end of file flag , It is usually placed at the end of the file , and scanf Generally, when reading fails or characters at the end of the file are encountered , Will return a EOF.
2.EOF In fact, it returns a -1,-1 The complement in memory is 11111111111111111111111111111111, if scanf("%d", &n) The returned value is EOF, In this case, reverse it ~scanf("%d", &n), Back to ~EOF Namely 00000000000000000000000000000000, That is to say 0, Judge as false, jump out of the loop .
23、 Judge whether it's a vowel or a consonant
describe :
KiKi Start learning English letters ,BoBo The teacher told him , There are five letters A(a), E(e), I(i), O(o),U(u) Called vowels , All other letters are called consonants , Please help him write a program to judge whether the input letter is a vowel (Vowel) Or consonants (Consonant).
Input description :
Multi group input , Enter one letter per line .
Output description :
Enter... For each group , Output as a line , If the input letter is a vowel ( Include case ), Output “Vowel”, If the input letter is a non vowel , Output “Consonant”.
Example :
Input :
A
bOutput :
Vowel
Consonant
Code 1:
Code 2:
Code 3:
notes :
1. When the value entered multiple times is a character , The buffer needs to be cleaned up
2.%c Is to take a character from the buffer , If there is \n,scanf I'll take the back \n Take it, too ( Regulations )
(%d Back +\n It may not work )
3. stay %c Preceded by a space , White space characters are skipped (\n Is a blank character )
24、 The running result of the code
The result of the following code is
#include <stdio.h>
int main()
{
int arr[] = {1,2,3,4,5};
short *p = (short*)arr;
int i = 0;
for(i=0; i<4; i++)
{
*(p+i) = 0;
}
for(i=0; i<5; i++)
{
printf("%d ", arr[i]);
}
return 0;
}
Running results :
notes :
1.arr The data storage in the array memory is shown in the following figure ( Small end storage )
01 00 00 00 | 02 00 00 00 | 03 00 00 00 | 04 00 00 00 | 05 00 00 00 |
2.short The type pointer dereferences two bytes at a time , first for After loop execution , Changed the first eight bytes
00 00 00 00 | 00 00 00 00 | 03 00 00 00 | 04 00 00 00 | 05 00 00 00 |
25、 The running result of the code
The result of the following code is
#include <stdio.h>
int main()
{
unsigned long pulArray[] = { 6,7,8,9,10 };
unsigned long* pulPtr;
pulPtr = pulArray;
*(pulPtr + 3) += 3;
printf("%d,%d\n", *pulPtr, *(pulPtr + 3));
return 0;
}
Running results :
notes :
pulArray The array is as follows
6 | 7 | 8 | 9 | 10 |
pulArray | pulArray+3 |
26、 The running result of the code
The result of the following code is
#include <stdio.h>
int main()
{
int a = 0x11223344;
char *pc = (char*)&a;
*pc = 0;
printf("%x\n", a);
return 0;
}
Running results :
notes :
1.a The situation in memory is as follows
44 | 33 | 22 | 11 |
pc It's a character pointer ,*pc=0 after ,a The situation in memory is as follows
00 | 33 | 22 | 11 |
here a The hexadecimal representation of is :0x11223300
2.%x It is printed in hexadecimal
27、 After the following procedures are executed , The output result is
describe :
After the following procedures are executed , The output result is
notes :
28、 Of the following procedure k The final value is
describe :
Of the following procedure k The final value is
notes :+ Is greater than *=, So count first i+j, Calculate again k=k*(i+j)
29、 The final output of the following program is
describe :
The final output of the following program is
notes :
1. One is global a Variable , One is local a Variable , Two variables are not the same a Variable
2. When global variables and local variables conflict , Local variables take precedence , therefore a+=1 Is for local variables a add 1
3. Out of function , local variable a Space is released , What is printed out is the global variable a value
30、 Variables cannot be made year The value in increases to 1010 The sentence is
describe :
If there are defined statements :int year=1009,int *p=&year; The following variables cannot be made year The value in increases to 1010 The sentence is
notes :
1. * Has a higher priority than +=,A Sure
2. ++ Has a higher priority than *, First of all, let's execute ++ Re execution *p, and ++ It's postposition ++, But the first thing to do , Aiming at p Pointer to variables instead of *p, So it's dereference and then on p Pointer to the variable ++,p Point to memory year The following integer variable address .D Wrong option . And *dest++=*src++ similar , The code can be divided into three steps :1.*dest=*src 2.dest++ 3.src++
31、 Select expression 11|10 Result
describe :
Select expression 11|10 Result ( All the values in this question are decimal )
notes :
1. One | Is a bitwise OR operation
2.11 Binary system :1011
10 Binary system :1010
11|10 Binary system :1011 This value is decimal 11
32、 Find the least common multiple of two numbers
describe :
Enter two numbers , Find the least common multiple of two numbers
Code 1:
Code 2:
Code 3:
33、 Inverted string
describe :
Invert the words of a sentence , Punctuation is not inverted .
such as :I like beijing. After the function becomes beijing. like I
Input description :
Each test input contains a test case :I like beijing. The length of the input case shall not exceed 100
Output description :
Output the inverted string in turn , Space off
My code :
#include<stdio.h>
#include<string.h>
int main()
{
char arr[101] = { 0 };
gets(arr);
int left = 0;
int right = strlen(arr) - 1;
int ch = 0;
int i = 0;
int j = 0;
arr[right + 1] = '\0';
// Reverse the entire string
while (right > left)
{
ch = arr[right];
arr[right] = arr[left];
arr[left] = ch;
left++;
right--;
}
// Reverse each word
while (arr[i] != '\0')
{
left = i;
while (arr[i] != ' ' && arr[i] != '\0')
{
i++;
}
right = i - 1;
while (right > left)
{
ch = arr[right];
arr[right] = arr[left];
arr[left] = ch;
left++;
right--;
}
i++;
}
printf("%s\n", arr);
return 0;
}
Code :
notes :
1. Ideas : First put each word in reverse order , Such as :I ekil .gnijieb
Then reverse the entire string , Such as :beijing. like I
2.gets The prototype of the function is :
# include <stdio.h>
char *gets(char *str);
This function is very simple , There is only one parameter . Parameter type is char* type , namely str It can be a character pointer variable name , It can also be a character array name
gets() The function reads a string from the input buffer and stores it in a character pointer variable str The memory space pointed to
gets() Functions are not only better than scanf concise , And you can directly enter a string with spaces
Use gets() The function needs attention : Use gets() when , The system will finally “ knock ” The newline character of is taken from the buffer , Then discard , So there will be no line breaks left in the buffer . That means , If you have used gets(), If you assign a value to the character variable from the keyboard later, you don't need to absorb carriage return and empty the buffer , Because the carriage return of the buffer has been gets() Take it out and throw it away
33、 Use the pointer to print the contents of the array
describe :
Write a function to print arr Contents of array , Do not use array subscripts , Use the pointer .
arr Is an integer one-dimensional array .
Code 1:( It doesn't fit the question )
Code 2:
Code 3:( Code two is simplified )
34、 Reverse character order
describe :
Put a string str It's the reverse of what we're talking about , And the output .
Data range :1<=len(str)<=10000
Input description :
Enter a string , There can be spaces
Output description :
Output string in reverse order
Example 1
Input
I am a studentOutput
tneduts a ma IExample 2
Input
nowcoderOutput
redocwon
Code :
notes :
The system will report gets unsafe , In fact, there are many functions that report unsafe , such as scanf、gets、strcpy、strcat、strcmp etc. , These are just relatively unsafe , It is not safe because these functions do not care whether the array memory can fit , Memory may be modified out of bounds , Here's the picture ( After modifying the memory, the system detects out of bounds access )
35、 Calculate the sum
describe :
seek Sn=a+aa+aaa+aaaa+aaaaa+...... Before n Sum of items , among a It's a number ,
for example :S5=2+22+222+2222+22222
Code :
36、 Print number of daffodils
describe :
Find out 0~100000 Between all “ Narcissistic number ” And the output .
“ Narcissistic number ” It means a n digit , Of its digits n The sum of the powers is exactly equal to the number itself , Such as :153=1^3+5^3+3^3, be 153 It's a “ Narcissistic number ”.
Code 1:( Error code , Loop body variable i Changed in the circulatory system )
Code 2:
notes :power The function needs to add math.h The header file
37、 Print diamond
describe :
use C The language outputs the following patterns on the screen :
My code :
#include<stdio.h>
int main()
{
int n = 0;
printf(" Enter the number of lines :");
scanf("%d", &n);
int i = 0;
int j = 0;
for (i = 1; i <= n; i++)
{
for (j = 0; j < n - i; j++)
{
printf(" ");
}
for (j = 0; j < 2 * i - 1;j++)
{
printf("*");
}
printf("\n");
}
for (i = 1; i < n; i++)
{
for (j = 0; j < i; j++)
{
printf(" ");
}
for (j = 0; j < (n - i) * 2 - 1; j++)
{
printf("*");
}
printf("\n");
}
return 0;
}
Code :( Look at my code )
notes :( My code )
notes :( Code )( Just look at the face note )
1. The middle line is divided into upper and lower parts for printing , Divide the middle row into the upper part , So the upper part has line That's ok , The lower half has line-1 That's ok .
2. Print spaces in the top half : First line print line-1 A space , The second line line-2 A space , And so on
3. The top half prints *: The first line is 2*0+1, The second line 2*1+1, By analogy
38、 Drink soda
describe :
Drink soda ,1 Bottle of soda 1 element ,2 An empty bottle can be exchanged for a soda , to 20 element , How much soda can I get ( Programming to realize )
Code :
39、 Adjust the order of odd and even numbers
describe :
Enter an array of integers , Implement a function , To adjust the order of the numbers in the array so that all the odd numbers in the array are in the first half of the array , All even Numbers are in the second half of the array
Code 1:
#include<stdio.h>
void print(int arr[], int sz)
{
int i = 0;
for (i = 0; i < sz; i++)
{
printf("%d ", arr[i]);
}
printf("\n");
}
void move(int* arr, int sz)
{
int* left = arr;
int* right = arr + sz - 1;
int tmp = 0;
while (left < right)
{
// Find an even number from left to right , stop
while ((*left) % 2 == 1)
{
left++;
}
// Find an odd number from right to left , stop
while ((*right) % 2 == 0)
{
right--;
}
if (left < right)
{
tmp = *left;
*left = *right;
*right = tmp;
}
}
}
int main()
{
int arr[] = { 1,2,3,4,5,6,7,8,9,0 };
int sz = sizeof(arr) / sizeof(arr[0]);
print(arr, sz);
move(arr, sz);
print(arr, sz);
return 0;
}
Code 2:
#include<stdio.h>
void print(int arr[], int sz)
{
int i = 0;
for (i = 0; i < sz; i++)
{
printf("%d ", arr[i]);
}
printf("\n");
}
void move(int* arr, int sz)
{
int* left = arr;
int* right = arr + sz - 1;
int tmp = 0;
while (left < right)
{
// Find an even number from left to right , stop
while ((left < right) && (*left) % 2 == 1)
{
left++;
}
// Find an odd number from right to left , stop
while ((left < right) && (*right) % 2 == 0)
{
right--;
}
if (left < right)
{
tmp = *left;
*left = *right;
*right = tmp;
}
}
}
int main()
{
int arr[] = { 1,2,3,4,5,6,7,8,9,0 };
int sz = sizeof(arr) / sizeof(arr[0]);
print(arr, sz);
move(arr, sz);
print(arr, sz);
return 0;
}
notes :
1. Find an even number from left to right , This address is for left, Find an odd number from right to left , This address is for right, Two address exchange , By analogy , until left>right until
2. There should be judgment when exchanging ,left Is less than right, If you don't judge , It is possible for this code to loop a certain time left>right Of , It will exchange the possible correct sorting, resulting in errors .
3. If the array is full of odd numbers or even numbers , Looking straight back or straight ahead will result in cross-border visits , Therefore, we should also be conditional when looking for , Such as code 2 Shown
40、 The execution result of the program is
describe :
The execution result of the program is
int main()
{
unsigned char a = 200;
unsigned char b = 100;
unsigned char c = 0;
c = a + b;
printf(“%d %d”, a+b,c);
return 0;
}
Running results :
notes :
1.200 The binary sequence of :00000000000000000000000011001000
a Data stored in :11001000
100 The binary sequence of :00000000000000000000000001100100
b Data stored in :01100100
2.c=a+b,a+b Perform integer lift when (ab Are unsigned variables , Therefore, integer lifting compensation 0)
a After integer lifting :00000000000000000000000011001000
b After integer lifting :00000000000000000000000001100100
a+b:00000000 00000000 00000001 00101100 This number is a positive number , The former is the same as the latter , Values for 300
c=a+b(c yes unsigned char type ), Variable c:00101100
%d Print ,c Symbol bit 0 Think of it as a positive number , repair 0 by 00000000000000000000000000101100, This number is a positive number , The former is the same as the latter , Values for 44
41、 Yang hui triangle
describe :
Print Yang Hui triangle on the screen
1
1 1
1 2 1
1 3 3 1
……
My code :
#include<stdio.h>
int main()
{
int arr[10][10] = { 0 };
int i = 0;
int j = 0;
arr[0][0] = 1;
arr[1][0] = 1;
arr[1][1] = 1;
for (i = 0; i < 10; i++)
{
for (j = 0; j <= i; j++)
{
if (j == 0)
{
arr[i][j] = 1;
}
if (i >= 2 && j > 0)
{
arr[i][j] = arr[i - 1][j] + arr[i - 1][j - 1];
}
printf("%-4d", arr[i][j]);
}
printf("\n");
}
return 0;
}
Code :
42、 Guess the killer
describe :
There was a murder somewhere in Japan , Through investigation, the police determined that the killer must be 4 One of the suspects .
The following is a 4 A confession from a suspect :
A say : Is not my .
B say : yes C.
C say : yes D.
D say :C It's bullshit
It is known that 3 I told you the truth ,1 I'm telling a lie .
Now, based on this information , Write a program to determine who the killer is .
Code :
notes : Assume... In turn abcd For the murderer , from a Start to verify later , If it is three truths and one falsehood , Then the murderer is the one who is being verified at this time
43、 Guess the place
describe :
5 Three athletes took part in 10 Meter diving competition , They were asked to predict the outcome of the game :
A Said the player :B second , My third ;
B Said the player : I'm second ,E Fourth ;
C Said the player : I'm number one ,D second ;
D Said the player :C Last , My third ;
E Said the player : My fourth ,A First of all ;
After the game , Each player is half right , Please program to determine the position of the competition .
My code :
#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) && 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;
}
Code :
notes : If all the above conditions are met, there will be many duplicate positions when you directly output , such as a=3,b=3,c=1,d=3,e=4 etc. , At this point, you need to filter the results that meet the conditions , That is to say 1 To 5 All nouns must have , Just use the expression a*b*c*d*e==120 To screen
44、 Change the odd number of each bit of a number to 1 Even number changed to 0
describe :
Little Lele likes numbers , Especially like 0 and 1. He now has a number , I want to change the number of each person into 0 or 1. If a bit is odd , Just turn it into 1, If it's even , Then turn it into 0. Please answer what he finally got .
Input description :
Input contains an integer n (0 ≤ n ≤ 109)
Output description :
Output an integer , That is, the number obtained by xiaolele after modification .
Example 1:
Input :222222
Output :0
Example 2:
Input :123
Output :101
My code :
#include<stdio.h>
#include<math.h>
int main()
{
int n = 0;
scanf("%d", &n);
int arr[100] = { 0 };
int i = 0;
int j = 0;
int s = 0;
int sum = 0;
// take n Each bit of is stored in the array arr in , among arr[0] Save a bit ,arr[1] Save ten ...... arr[i] Save last
while (n)
{
arr[i] = n % 10;
n = n / 10;
i++;
}
// utilize j from 0 visit arr Array until i, Take out the numbers , use 1/0 Multiplied by the corresponding weight, there exists sum in
for (j = 0; j <= i; j++)
{
if (arr[j] % 2 == 0)
{
s = 0 * (int)pow(10, j);
}
else
{
s = 1 * (int)pow(10, j);
}
sum = sum + s;
}
printf("%d\n", sum);
return 0;
}
Code :
45、n A stair , One go 1 or 2 rank , How many ways of walking
describe :
Xiaolele needs to go to class n Steps , Because he has long legs , So you can choose to go one step or two steps at a time , So how many ways does he walk ?
Input description :
Input contains an integer n (1 ≤ n ≤ 30)
Output description :
Output an integer , That is, the number of ways xiaolele can go .
My code 1:
#include<stdio.h>
int zoutaijie(int n)
{
if (n == 1)
{
return 1;
}
else if (n == 2)
{
return 2;
}
else
{
return zoutaijie(n - 1) + zoutaijie(n - 2);
}
}
int main()
{
int n = 0;
scanf("%d", &n);
int ret = zoutaijie(n);
printf("%d\n", ret);
return 0;
}
My code 2:
#include<stdio.h>
int zoutaijie(int n)
{
int a = 1;
int b = 2;
int c = 0;
if (n == 1)
{
return 1;
}
if (n == 2)
{
return 2;
}
while (n > 2)
{
c = a + b;
a = b;
b = c;
n--;
}
return c;
}
int main()
{
int n = 0;
scanf("%d", &n);
int ret = zoutaijie(n);
printf("%d\n", ret);
return 0;
}
Code :
46、 Enter a string of numbers , Remove the weight and output from small to large
describe :
The teacher gave xiaolele a sequence of positive integers , Ask xiaolele to de duplicate this sequence and sort it from small to large . But the sequence given by the teacher is too long , Little Lele can't patiently redo and sort , Please help him .
Input description :
The first line contains a positive integer n, Indicates that the sequence given by the teacher has n Number . Next there is n That's ok , One positive integer per line k, Is the value of each element in the sequence .(1 ≤ n ≤ 105,1 ≤ k ≤ n)
Output description :
Output one line , For the de reordered sequence , There is a space after each number .
Example 1:
Input :4
2
2
1
1Output :1 2
Example 2:
Input :5
5
4
3
2
1Output :1 2 3 4 5
My code :( There is a problem , Tips : Run timeout or array out of bounds )
#include<stdio.h>
int main()
{
int n = 0;
scanf("%d", &n);
int p = n;
int arr[100000] = { 0 };
int i = 0;
int j = 0;
int exp = 0;
// Store all the input numbers in the array arr in
while ((scanf("%d", &arr[p - 1])) != EOF)
{
p--;
}
// Sort the numbers in the array from small to large
for (i = 0; i < n - 1; i++)
{
for (j = 0; j < n - 1 - i; j++)
{
if (arr[j] > arr[j + 1])
{
exp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = exp;
}
}
}
// Print the elements in the array , If the latter one is the same as the previous one, it will not be printed
printf("%d ", arr[0]);
for (i = 1; i < n; i++)
{
if (arr[i] != arr[i - 1])
{
printf("%d ", arr[i]);
}
}
return 0;
}
Code :
47、 Matrix transposition
describe :
KiKi There's a matrix , He wants to know the transposed matrix ( The new matrix obtained by exchanging the row and column of matrix is called transpose matrix ), Please program to help him solve .
Input description :
The first line contains two integers n and m, Indicates that a matrix contains n That's ok m Column , Separate... With spaces . (1≤n≤10,1≤m≤10)
from 2 To n+1 That's ok , Enter... On each line m It's an integer ( Range -231~231-1), Separate... With spaces , Co input n*m Number , Represents the elements in the first matrix .
Output description :
Output m That's ok n Column , Is the result of matrix transposition . There is a space after each number .
Example 1:
Input :2 3
1 2 3
4 5 6Output :1 4
2 5
3 6
My code :
#include<stdio.h>
int main()
{
int n = 0;
int m = 0;
scanf("%d %d", &n, &m);
int i = 0;
int j = 0;
int arr[10][10] = { 0 };
// Enter the elements of the array , There is arr[10][10] in
for (i = 0; i < n; i++)
{
for (j = 0; j < m; j++)
{
scanf("%d", &arr[i][j]);
}
}
// Output the elements of the array
for (i = 0; i < m; i++)
{
for (j = 0; j < n; j++)
{
printf("%d ", arr[j][i]);
}
printf("\n");
}
return 0;
}
Code :
48、 Deletes the specified number from the sequence
describe :
There is a sequence of integers ( There may be duplicate integers ), Now delete a specified integer , Output the sequence after deleting the specified number , There is no change in the front and back positions of the undeleted numbers in the sequence .
Data range : Both the length of the sequence and the values in the sequence satisfy 1 \le n \le 501≤n≤50
Input description :
Enter an integer in the first line (0≤N≤50).
Second line input N It's an integer , Enter space delimited N It's an integer .
On the third line, enter an integer you want to delete .
Output description :
Output as a line , Delete the sequence after the specified number .
Example 1:
Input :6
1 2 3 4 5 9
4Output :1 2 3 5 9
Example 2:
Input :5
1 2 3 4 6
5Output :1 2 3 4 6
My code :
#include<stdio.h>
int main()
{
int n = 0;
scanf("%d", &n);
int arr[60] = { 0 };
int i = 0;
int k = 0;
// The entered number exists arr Array
for (i = 0; i < n; i++)
{
scanf("%d", &arr[i]);
}
// Enter the number to delete
scanf("%d", &k);
// The number in the array that is not equal to this number is output
for (i = 0; i < n; i++)
{
if (arr[i] != k)
{
printf("%d ", arr[i]);
}
}
return 0;
}
Code :
notes :
In the code i Variables are those that traverse the elements of an array ,j Variables are where records should exist .
49、 Right triangle pattern with space
describe :
KiKi I learned the cycle ,BoBo The teacher gave him a series of printing exercises , The task is to print “*” Right triangle pattern with space .
Input description :
Multi group input , An integer (2~20), Represents the length of the right side of a right triangle , namely “*” The number of , It also means the number of output lines .
Output description :
Enter... For each line , For output “*” Right triangles of corresponding length formed by , Every “*” There is a space after .
Example 1:
Input :5
Output :
![]()
Example 2:
Input :4
Output :
My code :
#include<stdio.h>
int main()
{
int n = 0;
int i = 0;
int j = 0;
while (scanf("%d", &n) != EOF)
{
for (i = 1; i <= n; i++)
{
for (j = 0; j < (n - i) * 2; j++)
{
printf(" ");
}
for (j = 0; j < i; j++)
{
printf("* ");
}
printf("\n");
}
}
return 0;
}
Code :
notes :
1.
2. Think of it as a n*n The square of an array , The element in the upper left corner of the diagonal prints two spaces , Other elements print asterisks + Just blank space .
3. The elements in each diagonal parallel to the sub diagonal have the same properties : The sum of their horizontal and vertical coordinates is an equal number .
50、 Hollow triangle pattern
describe :
KiKi I learned the cycle ,BoBo The teacher gave him a series of printing exercises , The task is to print “*” Composed of “ Hollow ” Triangle pattern .
Input description :
Multi group input , An integer (3~20), Represents the number of rows output , It also means that the sides of a triangle are made up of “*” The number of .
Output description :
Enter... For each line , For output “*” Composed of “ Hollow ” triangle , Every “*” There is a space after .
Example 1:
Input :4
Output :
Example 2:
Input :5
Output :
![]()
My code :
#include<stdio.h>
int main()
{
int n = 0;
int i = 0;
int j = 0;
while (scanf("%d", &n) != EOF)
{
// Print the first two lines
printf("* ");
printf("\n");
printf("* * ");
printf("\n");
// Print the middle line ( The middle row has n-3 That's ok , Row number i from 1 Start to n-3, At the beginning and end of each line is * , In the middle is the number of lines i*2 A space )
for (i = 1; i <= n - 3; i++)
{
printf("* ");
for (j = 0; j < 2 * i; j++)
{
printf(" ");
}
printf("* ");
printf("\n");
}
// Print the last line (n individual * )
for (i = 0; i < n; i++)
{
printf("* ");
}
printf("\n");
}
return 0;
}
Code :
notes :
1.
2. Think of it as a n*n The square of an array , The first column of these elements 、 Diagonal elements 、 The last line of elements prints an asterisk + Space , Print two spaces for all other elements .
边栏推荐
- Analysis of scoped attribute principle and depth action selector
- Vercel uses HTTP caching
- Internet people a few years ago vs Internet people today
- Message scrolling JS implementation
- SQL notes
- rainbow
- Collection of some compatibility issues
- 用navicat将mysql数据转化为postgresql
- Several methods of identifying equivalent circuit of circuit drawing
- On switch() case statement in C language
猜你喜欢
How to implement a custom jdbc driver in only four steps?
Avantages de win8.1 et win10
2022 ICLR | CONTRASTIVE LEARNING OF IMAGE- AND STRUCTURE BASED REPRESENTATIONS IN DRUG DISCOVERY
Analysis of scoped attribute principle and depth action selector
Ctfshow SQL injection (231-253)
Redis主从复制、哨兵模式、集群
Li Kou brush question 338 Bit count
LeetCode第297场周赛(20220612)
Keil uses j-link to burn the code, and an error occurs: Flash download failed - one of the "Cortex-M3" solutions
Analysis on the similarities and differences of MVC, MVP and mvvc
随机推荐
php开发14 友情链接模块的编写
Several methods of identifying equivalent circuit of circuit drawing
Chinese trumpet creeper
是“凯撒密码”呀。(*‘▽‘*)*
[LeetCode]-滑动窗口
Cesium:cesiumlab makes image slices and loads slices
C disk lossless move file
Vercel 使用 HTTP 缓存
2022 chlorination process operation certificate examination question bank and simulation examination
2022 ICML | Pocket2Mol: Efficient Molecular Sampling Based on 3D Protein Pockets
利用Javeswingjdbc基於mvc設計系統
2022 ICLR | CONTRASTIVE LEARNING OF IMAGE- AND STRUCTURE BASED REPRESENTATIONS IN DRUG DISCOVERY
PowerShell:因为在此系统上禁止运行脚本,解决方法
推荐的图片临时在线压缩工具
【JS解决】leedcode 200. 岛屿数量
How to use redis
Third party comment plugin
Blockly learning ----2 Code generation, grid, scaling, events, storage
The differences between the four startup modes of activity and the applicable scenarios and the setting methods of the two startup modes
Converting MySQL data to PostgreSQL with Navicat