preface:
I haven't seen C language for more than a month, but the final exam of the last semester of freshman year is coming soon
Some time ago, I was thinking about the direct naked exam,,,,,,,,
Later, I thought I'd better do some after-school exercises. After all, I still have no confidence,
Hey, come on, come on...
Chapter V
#include<stdio.h> int main() { int sum1 = 0; int sum2 = 0; int a = 0;//Our a value int n = 0;//Number of items scanf("%d %d", &a, &n); int i = 0; for (i = 0; i <n ; i++) { sum1 += pow(10, i) * a; sum2 += sum1; } return 0; }
#include<stdio.h> int main() { int sum, i,ret; ret = 1; for (i = 1; i <=20; i++) { ret *= i; sum += ret; } printf("%d", sum); return 0; }
Sum the following formula:
#include<stdio.h> int main() { int sum1, sum2; float sum3; for (int i = 1; i <= 100; i++) { sum1 += i; if (i <= 10) { sum3 += 1 / i; } if (i <= 50) { sum2 += i * i; } } return 0; }
#include<stdio.h> int main() { int i = 0; for (i = 100; i <= 999; i++) { int a = i / 100;//Get a hundred digits int b = i / 10 % 10;//Get ten digits int c = i % 100;//Get a digit number if (a * a * a + b * b * b + c * c * c == i) { printf("It's daffodils"); } } return 0; } private static void question8_2(){ for (int i =100; i <=999; i++) { int sum=0; int count=3; while (count!=0){ sum+=Math.pow(i%10,3); i/=10; count--; } if(sum==i){ System.out.println("It's daffodils"); } } }
Expansion: suppose you are given a number at will to judge whether it is a number of daffodils?
#include<stdio.h> #include<math.h> int main() { //1. First judge the number of digits int n, count,sum; scanf("%d", &n);//Enter a number with more than three digits int temp; temp = n; while (temp) { count++; temp /= 10; } //2. Get the number of each digit //3. Sum after the power of each digit int temp; temp = n; while (temp) { sum += pow(n % 10, count); temp /= 10; } if (sum == n) { printf("What's that number"); } return 0; }
1 is not perfect
#include<stdio.h> int main() { int i, factor,sum; for (i = 2; i <= 1000; i++) { sum = 1; for (factor = 2; factor <= i / 2; factor++) { if (i % factor == 0) { sum += factor; } } if (sum == i) { printf("%d All factors are 1", i, factor); for (i = 2; i <= 1000; i++) { for (factor = 2; factor <= i / 2; factor++) { if (i % factor == 0) { printf("%d",factor); } } } } } return 0; }
#include<stdio.h> int main() { int a, b,i; double sum;//Sum a = 2;//Molecular initial value b = 1;//Denominator initial value for (i = 0; i < 20; i++) { sum += a / b; int temp = a; a = a + b; b = temp; } return 0; }
#include<stdio.h> int main() { double height, sum; int i; for (i = 0; i < 10; i++) { sum += height;//Falling height height /= 2;//You can only bounce half the height at a time sum += height;//Rebound height } sum -= height;//Subtract the height of the last rebound return 0; }
#include<stdio.h> #include<math.h> int main() { double x0, x1,a; printf("Please enter a positive number"); scanf("%lf", &a); x0 = a / 2; //The key point is why it is assigned a/2 here, because the root sign a is greater than a/2. We assign a number closest to the root sign a, that is, the root sign a x1 = (x0 + a / x0) / 2; do { x0 = x1; x1 = (x0 + a / x0) / 2; } while (fabs(x0 - x1) < le - 5); printf("%lf The square root of is%lf", a, x1); return 0; }
derivatives:
After derivation:
Extracting the common factor can omit many complex things written by the multiplication of x
#include<stdio.h> #include<math.h> int main() { double x0, x1, f, f1; x0 = 1.5; do { x0 = x1; f = ((2 * x0 - 4) * x0 + 3) * x0 - 6; f1 = (6 * x0 - 8) * x0 + 3; x1 = x0 - f / f1; } while (fabs(x0 - x1) >= le - 5) printf("At 1.5 The nearby root is:%lf", x1); return 0; }
The middle number of mid is continuously brought into the equation
We extract the common factor:
#include<stdio.h> int main() { double left, right, mid,temp; left = -10; right = 10; temp = 10; do//Make sure to cycle at least once to update our temp value { mid = (left + right) >> 1; temp = mid * (2 * mid * mid - 4 * mid + 3) - 6; if (temp > 0) { right = mid; } else if(temp<0) { left = mid; } } while (fabs(temp) >= le - 5); printf("So the equation is(-10,10)The root of is%lf", mid); return 0; }
When printing, print the space first and then the line number
#include<stdio.h> int main() { int n,i,j; printf("Please enter any number"); scanf_s("%d", &n); for (i = 0; i < n; i++) { 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++) { for (j = 0; j < i + 1; j++) { printf(" "); } for (j = 0; j < 2 * (n - 1) - 1 - 2 * i; j++) { printf("*"); } printf("\n"); } return 0; }
One thing to note: A, B, C have different targets
#include<stdio.h> int main() { int A, B, C; //1. List all the opponents of A for (A = 'X'; A <= 'Z'; A++) //Since the three characters x, y and Z are connected, the difference between all known ASCII values is 1 { for (B = 'X'; B <= 'Z'; B++) { for (C = 'X'; C <= 'Z'; C++) { if (A != 'X' && C != 'X' && C != 'Z' && A != B && A != C && B != C) { printf("A Battle%c,B Battle%c,C Battle%c", A, B, C); } } } } return 0; }
Chapter VI:
#include<stdio.h> int main() { int i, j; int sum1, sum2; int arr[3][3]; //Receive data in matrix for (i = 0; i < 3; i++) { for (j = 0; j < 3; j++) { scanf("%d", &arr[i][j]); } } //Upper left corner to lower right corner for (i = 0; i < 3; i++) { sum1 += arr[i][i]; } //Top right corner to bottom left corner for (i = 0, j = 2; i < 3; i++, j--) { sum2 += arr[i][j]; } printf(""); return 0; }
When the diagonal line from the upper right corner to the lower left corner, the element subscript: the abscissa increases by 1 and the ordinate decreases by 1
In the end, either end is in the position of - 1 or smaller than key
#include<stdio.h> int main() { int arr[10] = { 1,2,3,4,5,7 }; int sz = sizeof(arr) / sizeof(arr[0]); int end = sz - 1; int key = 6; while (end > 0 && arr[end] > key) { arr[end + 1] = arr[end]; end--; } //There are two situations for exiting // 1. End < = 0 is inserted into the first bit // 2. Arr [End] < = key: insert it in the middle // arr[end + 1] = key; return 0; }
The loop ends when end==begin
#include<stdio.h> int main() { int arr[10] = { 8,6,5,4,1 }; int begin = 0; int end = sizeof(arr) / sizeof(arr[0]) - 1; while (begin < end) { int temp = arr[begin]; arr[begin] = arr[end]; arr[end] = temp; begin++; end--; } return 0; }
#include<stdio.h> int main() { int i, j; int arr[30][30] = { 0 }; for (i = 0; i < 10; i++) { for (j = 0; j <= i; j++) { if (i == j||j==0) { arr[i][j] = 1; } else { arr[i][j] = arr[i - 1][j] + arr[i - 1][j - 1]; } } } return 0; }
#include<stdio.h> //Steps to write magic array: // 1. First place 1 in the middle of row 0 // 2. The next element is stored in the previous row and next column of the current element // 3. If there are elements in the previous row and the next column, they will be placed in the next row of the current column // 4. When crossing the range of the plane, we should regard it as surrounding // int main() { int N = 0; printf("Please enter the order of a magic array"); while (true) { scanf("%d", &N); //Current rule: we specify an odd order magic array and the order is less than 100 if (N % 2 == 1 && N < 100) { break; } } int arr[N][N] = { 0 }; int row = 0;//that 's ok int col = 0;//column int prerow = 0;//Record the line number of the previous element int precol = 0;//Record the column number of the previous element // 1. First place 1 in the middle of row 0 arr[row][N / 2] = 1; //Store the remaining N*N-1 elements int i = 0; for (i = 0; i < N * N - 1; i++) { //The next element is stored in the previous row and the next column row--; col++; //When crossing the line if (row<0) { row = N - 1; } //When the column crosses the line if (col > N) { col = 0; } if (arr[row][col] != 0) { //If there is an element in the previous row and the next column, it is saved to the next row in the same column of the current element row = prerow - 1; col = precol; } //Record the subscript of the current element prerow = row; precol = col; //All the coordinate changes are done, and then you can store this element arr[row][col] = i; } return 0; }
#include<stdio.h> // // 1. Find the largest element on the row and determine the column number of the element // // 2. Confirm whether the element is the smallest element on the column // int main() { int arr[3][4] = { 0 }; printf("Play the input by yourself. It is omitted here. Only the code to judge whether it is a saddle point is written"); int max =arr[0][0]; int maxcolindex = 0; int flag = 1; for (int i = 0; i < 3; i++) { for (int j = 0; j < 4; j++) { if (max < arr[i][j]) { max = arr[i][j]; maxcolindex = j;//Record column number } } //Find the largest element in a row and judge whether it is the minimum value of the column according to the stored column number for (int j= 0; j< 3;j++) { if (arr[j][maxcolindex] < max) { flag = -1; break; } } if (flag == 1) { printf("Saddle point exists"); printf("Abscissa is%d,The ordinate is%d", i, maxcolindex); } if (flag == -1) { printf("No saddle point"); } } return 0; }
#include<stdio.h> int main() { char text[3][80] = { 0 }; for (int i = 0; i < 3; i++) { gets(text[i]);//Cannot receive with scanf because there may be spaces in the middle of the text } //2. Statistics //Three line text for (int i = 0; i < 3; i++) { //Every detail character in the text for (int j = 0; text[j] != '\0'; j++) { if (text[i][j] >= 'A' && text[i][j] <= 'Z') { } } } return 0; }
What is the penultimate letter of ch? The formula in the figure is wrong. It should be 26-(ch-'A')-1, because we start with subscript 0....
Encoding formula: the encoding and decoding formulas are the same:
#include<stdio.h> int main() { char leo[32]; printf("Enter a line of message"); gets(leo); for (int i = 0; leo[i] != '\0'; i++) { if (leo[i] > 'A' && leo[i] < 'Z') { //After encryption, a - > Z, Z - > A, B - > y, and so on.... //When we find the law, we often use the method of hypothesis to bring in the value //Suppose leo[i] is an A character, ok //Another question is: why subtract an extra 1? Because it's a subscript, baby leo[i] = 'A' + (26 - (leo[i] - 'A') - 1); } if (leo[i] > 'a' && leo[i] < 'z') { leo[i] = 'a' + (26 - (leo[i] - 'a') - 1); } } return 0; }
#include<stdio.h> int main() { char dest[200] = { 0 };//We copy str1 and str2 to the dest target string char str1[100] = { 0 }; char str2[100] = { 0 }; printf("Please enter string 1"); gets(str1); printf("Please enter string 2"); gets(str2); int i = 0; while (str1[i] != '\0') { dest[i] = str1[i]; i++; } int j = 0; while (str2[j] != '\0') { dest[i++] = str2[j++]; } //The last place to copy \ 0 dest[i] = '\0'; return 0; }
#include<stdio.h> int main() { char str1[100] = { 0 }; char str2[100] = { 0 }; printf("Please enter string 1"); gets(str1); printf("Please enter string 2"); gets(str2); int i = 0; int ret = 0; while (str1[i] != '\0' && str2[i] != '\0') { ret = str1[i] - str2[i]; if (ret != 0) { break; } i++; } //When one string reaches null and the other does not //Assuming that the previous characters have equal degrees, the ret value should be updated at this time if (str1[i] == '\0' || str2[i] == '\0') { ret = str1[i] - str2[i]; } return 0; }
Chapter VII:
#include<stdio.h> #include<math.h> float x1, x2, disc, p, q; void greater_than_zero(int a, int b) { x1 = (-b + sqrt(disc)) / 2 * a; x2 = (-b - sqrt(disc)) / 2 * a; } void equal(int a, int b) { x1 = x2 = (-b) / 2 * a; } void less_than_zero(int a, int b) { p = (-b) / 2 * a; q = sqrt(-disc) / 2 * a; } int main() { void greater_than_zero(int a, int b); void equal(int a, int b); void less_than_zero(int a, int b); int a, b, c; printf("Please enter a b c"); scanf("%d %d %d", &a, &b, &c); //Computational discriminant disc = b * b - 4 * a * c; if (disc < 0) { less_than_zero(a, b); printf("There are two imaginary roots %f %f", p + q, p - q); } else if(disc==0) { equal(a, b); printf("There are two equal real roots %f %f", x1, x2); } else { greater_than_zero(a, b); printf("There are two equal real roots %f %f", x1, x2); } return 0; }
Note the cycle critical condition of exchange: J < I
public static void leo1(){ int[][] arr=new int[3][3]; for (int i = 0; i <3; i++) { for (int j = 0; j <i; j++) { int temp=arr[i][j]; arr[i][j]=arr[j][i]; arr[j][i]=temp; } } }
The input of scanf function will contain spaces
#include<stdio.h> void reverse(char str[32]) { int start = 0; int end = strlen(str) - 1; while (start < end) { int temp = str[start]; str[start] = str[end]; str[end] = temp; } start++; end--; } int main() { void reverse(char str[32]); char str[32]; printf("Please enter a string"); gets(str); reverse(str); return 0; }
#include<stdio.h> void contact(char dest[100], char str1[32], char str2[32]) { int i = 0; while (str1[i] != '\0') { dest[i] = str1[i]; i++; } int j = 0; while (str2[j] != '\0') { dest[i++] = str2[j++]; } dest[i] = '\0'; } int main() { char str1[32], str2[32], dest[100]; printf("Enter two strings"); gets(str1); gets(str2); contact(dest, str1, str2); }
#include<stdio.h> void strcpyss(char dest[100], char str[100]) { int i, j; while (str[i] != '\0') { if (str[i] =="vowel")//It's omitted here. Write it yourself { dest[j++] = str[i]; } i++; } } int main() { char dest[100]; char str[100]; printf("Please enter a string"); gets(str); strcpysss(dest, str); printf("%s", dest); }
#include<stdio.h> void outString(char digits[5]) { while (digits[i] != '\0') { printf("%c", digits[i]); if (digits[i + 1] != '\0') { break; } printf(" ");//Add a space i++; } } int main() { char digits[5] = { 0 }; gets(digits); outString(digits); return 0; }
Define two variables i and j and move the j subscript back
When the j subscript encounters a space, we can find the length of the string, which is len
When the len is greater than the length of the word array, we copy the string
#include<stdio.h> #include<math.h> double ret(int a, int b, int c, int d, double x) { double x0; double f, f1; do { x0 = x; f = a * pow(x0, 3) + b * pow(x0, 2) + c * x0 + d; f1 = 3 * a * pow(x0, 2) + 2 * b * x0 + c; x = x0 - f / f1; } while (fabs(x - x0) >= le - 3); return x; } int main() { int a, b, c, d; double x; printf("Please enter a factor a b c d x"); scanf("%d %d %d %d %lf", &a, &b, &c, &d, &x); double ret = root(a, b, c, d, x); printf("%lf", ret); return 0; }
#include<stdio.h> float Poly(int n, int x) { if (n == 0) { return 1; } else if (n == 1) { return x; } else { return ((2 * n - 1) * x - Poly(n - 1, x) - (n - 1) * Poly(n - 2, x)) / n; } } int main() { int n, x; scanf("%d %d", &n, &x); float res = Poly(n, x); return 0; }
#include<stdio.h> #define M 10 / / number of students #define N 5 / / number of courses float avg_stu[M]//Average score of each student float avg_coures[N]//Average score of each course int stu_index, coures_index;//Peak student and course subscript int r, c; void anverge(int arr[M][N]) { int i, j; float sum; for (i = 0; i <M; i++) { for (j = 0; j <N; j++) { sum += arr[i][j]; } printf("This is the second%d The average score of students is:%f", i + 1, sum / N); } } void everyanvergeclass(int arr[M][N]) { int i, j; float sum; for (j= 0;j<M; i++) { sum = 0.0; for (i= 0;i<N; j++) { sum += arr[i][j]; } printf("The average score of each course is:%f",sum / M); } } void highest(int arr[M][N] ) { float high; int i, j; high = arr[0][0];//The default first score is the highest score for (i = 0; i < M; i++) { for (j = 0; j < N; j++) { if (arr[i][j] > high) { high = arr[i][j]; r = i + 1; c = j + 1;//Which student is preservation, and which course is preservation } } } } float FangCha(int arr[M][N]) { float sum_avg_square = 0.0;//Mean sum of squares float sum_avg = 0.0;//Average score sum int i; for (i = 0; i < M; i++) { sum_avg_square += (avg_stu[i] * avg_stu[i]); sum_avg += avg_stu[i]; } return (sum_avg_square / n - (sum_avg_square / n) * (sum_avg_square / n)); } int main() { int i, j; int arr[M][N] = { 0 }; for (i = 0; i <M; i++) { for (j = 0; j <N; j++) { scanf("%d", &arr[i][j]);//Enter the student's grade } } anverge(arr); everyanvergeclass(arr); highest(arr); FangCha(arr); return 0; }
#include<stdio.h> #include<string.h> #define N 10; void input(int num[N], char name[N][8]) { int i; for (i = 0; i < N; i++) { printf("Please enter employee number"); scanf("%d", &num[i]); printf("Please enter employee name"); getchar();//Absorb spaces gets(name[i]);//Because scanf will stop input when encountering spaces, scanf cannot be used } } void sort(int num[N], int name[N][8]) { int i, j, minindex,temp; char temp2[8]; for (i = 0; i < N; i++) { min = i; for (j = 0; j < N - 1 - i; j++) { if (num[mindex] > num[j]) { minindex = j; } } temp = num[minindex]; strcpy(temp2, name[minindex]); num[minindex] = num[i]; strcpy(name[minindex],name[i]); num[i] = temp; strcpy(name[i],temp2); } } void search(int n, int num[N], char name[N][8]) { int begin = 0; int end = N - 1; int mid = 0; int loca = 0; int sign = 1; if ((n < num[0]) || n > num[N - 1]) { loca = -1; } while ((sign == 1) && (begin <= end)) { mid = (begin + end) / 2; if (num[mid]>n) { end = mid - 1; } else if (num[mid] < n) { begin = mid + 1; } else { printf("eureka"); local = mid;//Location found printf("His student number is%d His name is%s", num[mid], name[local]); } } if (sign == 1 || local == -1) { printf("can't find"); } } int main() { //Employee number int num[N], number, falg = 1, c; char name[N][8];//Names of 10 faculty members input(num, name); sort(num, name); while (flag == 1) { printf("Please enter the number you are looking for"); scanf("%d", &number); search(number, num, name); } return 0; }
How to convert a character to a decimal number
#include<stdio.h> int To(char hex[9]) { unsigned int result = 0; int i; while (hex[i] != '\0') { if (hex[i] > 'A' && hex[i] < 'Z') { hex[i] = hex[i] * 16 + hex[i] - 'A' + 10; } else if (hex[i] > 'a' && hex[i] < 'z') { hex[i] = hex[i] * 16 + hex[i] - 'a' + 10; } else { hex[i] = hex[i] * 16 + hex[i] - '0'; } i++; } return result; } int main() { char hex[9] = { 0 }; printf("Please enter a hexadecimal number"); scanf("%s", hex); unsigned int result = To(hex); return 0; }
#include<stdio.h> void ZHUANZAI(int num) { if (num / 10 != 0) { ZHUANZAI(num / 10); } printf("%c", num % 10 + '0'); } int main() { int num; printf("Please enter an integer to be reproduced"); scanf("%d", &num); ZHUANZAI(num); return 0; }
#include<stdio.h> int main() { int year, month, day,i,Date; scanf("%d %d %d", &year, &month, &day); int days[20] = { 0,31,28,31,30,31,30,31,31,30,31,30,31 }; for (i = 1; i < month; i++) { Date += days[i]; } Date += day; if (month > 2) { if (year % 400 == 0 || year % 4 == 0 && year % 100 != 0) { Date += 1; } } printf("This is the second%d day", Date); return 0; }
Chapter VIII
The exchange of values between pointers only changes the direction of the pointer
#include<stdio.h> #include<string.h> int main() { char str1[32], str2[32], str3[32]; char* p1 = str1, * p2 = str2, * p3 = str3; char* temp; if (strcmp(p1, p2) > 0) { temp = p1; p1 = p2; p2 = temp; } if (strcmp(p1, p3) > 0) { temp = p1; p1 = p3; p3 = temp; } if (strcmp(p2, p3) > 0) { temp = p2; p2 = p3; p3 = temp; } printf("%s %s %s", p1, p2, p3); return 0; }
#include<stdio.h> #include<string.h> //Capture these ten numbers void input(int arr[]) { for (int i = 0; i < 10; i++) { scanf("%d",arr+i); } } //Exchange function objective: put the smallest number first and the largest number last void Sort(int arr[]) { //1. Find the maximum value first int max = *arr;//initialization int min = *arr; int maxindex = 0; int minindex = 0; int sz = sizeof(arr) / sizeof(arr[0]); for (int i = 0; i < sz; i++) { if (*(arr + i) > max) { max = *(arr + i); maxindex = i; } if (*(arr + i) < min) { min = *(arr + i); minindex = i; } } //When the first position is the maximum value, we should also save the minimum value when we exchange the minimum value if (max == *arr) { int temp = *(arr + maxindex); *(arr + maxindex) = *(arr + minindex); *(arr + maxindex) = temp; maxindex = minindex; } else//Common exchange minimum to first bit { int temp = *arr; *arr = min; min = temp; } //Swap the maximum to the last bit int temp = *(arr + sz - 1); *(arr + sz - 1)=*(arr+maxindex); *(arr + maxindex) = temp; } void Print(int arr[]) { int sz = sizeof(arr) / sizeof(arr[0]); for (int i = 0; i < sz; i++) { printf("%d", *(arr + i)); } } int main() { int arr[] = { 0 }; input(arr); Sort(arr); Print(arr); return 0; }
Idea:
First take out the penultimate element for backup and saving.
Then overwrite the previous elements later,,,
Then exchange this element with the element with index position of subscript 0,
and so on:
Take out the last m-1 element and exchange it with the element at index 1,,, until the exchange is completed.. Repeat the above operation
#include<stdio.h> void move(int arr[],int n,int m) { //Adjust the number of m in the back for (int i = 0; i < m; i++) { //Save the penultimate element first int temp = *(arr +n-m+i); //Move the n-m number back, and start from the last element, otherwise it will produce coverage for (int j =n-m+i; j > i;j-- ) { *(arr + j) = *(arr + j - 1);//arr represents the address of the first element } //Give the saved element to the front arr[i] = temp; } } int main() { int arr[32] = { 0 }; int* p_arr = arr; printf("Please enter one by one n and m Value of"); scanf_s("%d %d", &n, &m); for (int i = 0; i < n; i++) { scan_s("%d", p_arr+ i); } move(arr,n,m); //n numbers after output for (int i = 0; i < n; i++) { printf("%d", *(arr + i)); } return 0; }
Array writing:
void move(int arr[32], int m, int n) { int i, j,temp; //We act on the reciprocal m numbers for (i=0;i<m;i++) { temp = arr[n - m + i]; for (j = n - m + i; j > i; j--) { arr[j] = arr[j - 1]; } arr[i] = temp; } }
#include<stdio.h> int main() { int n = 0; printf("Please enter the total number of people"); scanf("%d", &n);//Total number int person[1000] = { 0 }; int* p = person; //Number these people for (int i = 0; i < n; i++) { person[i] = i+1;//The number starts from No. 1 } int surnumber =n;//Current number of survivors int number =1;//Current report no while (surnumber > 1) { p = person; while (p!=person+n)//Indicates that someone else exists in the next location { if (*p != 0) { number++; if (number == 3) { *p = 0;//Assign the damn to 0 surnumber--;//The number of survivors is reduced by one number = 1;//Count off and restart } } p++; } } for (int i = 0; i < n; i++) { if (person[i] != 0) { printf("The survivor's number is:%d", person[i]); break;//It is possible that the array is very large, so you should exit directly } } return 0; }
#include<stdio.h> int my_strlen(char str[32]) { char* p = str; int count = 0; while (*p != '\0') { p++; count++; } return count; } int main() { char str[32]; printf("Please enter a string"); gets(str); int ret = my_strlen(str); return 0; }
#include<stdio.h> int main() { char leo1[1024], leo2[1024]; printf("Please enter a string"); scanf("%s", leo1); printf("Please enter a m value"); scanf("%d", &m); char* p1 = leo1+m; char* p2 = leo2; while (*p1!='\0') { *p2 = *p1; p1++; p2++: } *p2 = '\0';//End with a \ 0 as the end printf("%s\n", leo2); }
#include<stdio.h> void reseve(int arr[3][3]) { int i, j,temp; for (i = 0; i < 3; i++) { //Remember not to write J < 3. Think about why, //Because it will be exchanged twice, resulting in no exchange in the end for (j = 0; j < i; j++) { if (i != j) { temp = arr[i][j]; arr[i][j] = arr[j][i]; arr[j][i] = temp; } } } } int main() { int i, j; int arr[3][3]; //Enter a 3 * 3 matrix first for (i = 0; i < 3; i++) { for (j = 0; j < 3; j++) { scanf("%d", &arr[i][j]); } } reseve(arr); return 0; }
Two dimensional arrays are stored in memory in the form of one-dimensional arrays
The core of this problem is: how to convert a two-dimensional array into a one-dimensional array by passing parameters
We mark the subscript:
Since this array is a two-dimensional array name, the offset is not what we want when offsetting
Therefore, when we pass parameters, we should forcibly convert them to int * type
If you can offset the range of int size at one time, it is equivalent to passing this two-dimensional array as a one-dimensional array
void sort(int* arr, int row, int col) { int i, j,k; int max = *arr,min=*arr; int minindex[4]; //Here we are equivalent to using a one-dimensional array //Find a maximum for (i = 0; i < row * col; i++) { if (*(arr + i) > max) { max = *(arr + i); } } //Find a total of four minimum values for (i = 0; i < 4; i++) { min = *(arr + row * col - 1); //Traverses the subscripts of all elements for (j = 0; j < row * col; j++) { for (k = 0; k < i; k++) { //If the subscript of the minimum stored value is exactly equal to our j //There is no need to compare if (minindex[k] == j) { break; } } //There is no need to compare if (k != i) { continue; } //Store another when the conditions are met if (min > *(arr + j)) { min = *(arr + j); minindex[i] = j; } } } }
min_idx []: indicates the array storing the subscript of the minimum value element, not the array representing the value of the minimum value. Find out the primary and secondary.
Interface used:
#include<stdio.h> #include<string.h> void sort(char str[][32], int n) { int i, j; for (i = 0; i < n; i++) { for (j = 0; j < n - 1 - i; j++) { char temp[32][32]; if (strcmp(str[j], str[j + 1]) > 0) { strcpy(temp, str[j]); strcpy(str[j], str[j + 1]); strcpy(str[j + 1], temp); } } } } int main() { char str[32][32]; printf("Please enter the number you want to enter"); int n,i; scanf("%d", &n); for (i = 0; i < n; i++) { gets(str[i]);//Enter n strings and save them into this two-dimensional array //The number of lines in this two-dimensional array represents n strings //The number of columns represents the maximum length limit of each string } sort(str, n); //Output the sorted string for (i = 0; i < n; i++) { printf("%s", str[i]); } return 0; }
Create a character pointer array. Each element in the pointer array is a pointer,
Each pointer points to a string....
We didn't swap the whole string,
We just exchanged the pointer points, which is relatively efficient,,,
#include<stdio.h> #include<string.h> void sort(int n, int* p[32]) { for (int i = 0; i < n; i++) { for (int j = 0; j < n - 1 - i; j++) { if (strcmp(*(p +j-1), *(p + j)<0) { int temp= *(p + i); *(p + i)= *(p + j); *(p + j)=temp; } } } } int main() { char arr[32][32] = { 0 }; char* p[32]= arr; for (int i = 0; i < 32; i++) { //A maximum of 32 pointers will point to the 32 strings that exist in this case //Each string can be up to 32 characters long p[i] = arr[i]; } printf("Enter the number of strings you want to enter"); for (int i = 0; i <n; i++) { scanf("%s", p[i]);//Enter these n strings } sort(n, p); //Output the n ordered strings for (int i = 0; i < n; i++) { printf("%s", p[i]); } return 0; }
#include<stdio.h> void reversesort(int arr[]) { int* begin = arr; int sz = sizeof(arr) / sizeof(arr[0]); int* end = sz - 1; while (begin<end) { int temp = *begin; *begin = *end; *end = temp; } } int main() { int n = 0;//How many numbers do you want to enter? int arr[] = { 0 }; scanf("%d", &n); //Enter these numbers for (int i = 0; i < n; i++) { scanf("%d", arr + i); } reversesort(arr); return 0; }
#include<stdio.h> void anverge(int scores[5][5]) { int sum = 0; for (int i = 0; i < 4; i++) { sum += *(*(scores + i) +0); } printf("%d", sum / 4); } void find(int scores[5][5]) { int count = 0; int sum = 0; for (int i = 0; i < 4; i++) { count = 0; sum = 0; for (int j = 0; j < 5; j++) { if (*(*(scores + i) + j) < 60) { count++; } } if (count < 2) { continue; } printf("Student No.:%d", i + 1); for (int j = 0; j < 5; j++) { printf("%d", *(*(scores + i) + j)); sum += *(*(scores + i) + j); } printf("The average score is:%d", sum / 5); } } void exc(int scores[5][5]) { int count = 0; int sum = 0; for (int i = 0; i < 4; i++) { count = 0; sum = 0; for (int j = 0; j < 5; j++) { if (*(*(scores + i) + j) > 85) { count++; } sum += *(*(scores + i) + j); } //Find out and output all the students' grades if (sum / 5 > 90 || count == 5) { for (int j = 0; j < 5; j++) { printf("%d", *(*(scores + i) + j)); } } } } int main() { printf("Please enter the grades of four students in five courses"); int scores[5][5] = { 0 }; //Enter grade for (int i = 0; i < 4; i++) { scanf("%d %d %d %d", *(*(scores + i) + 0), *(*(scores + i) + 1), *(*(scores + i) + 2), *(*(scores + i) + 3), *(*(scores + i) + 4)); } //Find the average score of the first course anverge1(scores); //Find the students who fail in more than two courses and output the average grade of student number and the grade of all courses find(scores); //Find out if the average score is above 90 or all subjects are above 85 exc(scores); return 0; }
#include<stdio.h> void anverge(int scores[4][5]) { int i; float sum = 0; for (i = 0; i < 4; i++) { sum += scores[i][0]; } printf("Average score of the first course%d", sum / 4); } void findlose(int scores[4][5]) { int i, j, count,sum; for (i = 0; i < 4; i++) { count = 0; sum = 0.0; for (j = 0; j < 5; j++) { if (scores[i][j] < 60) { count++; } } if (count < 2) { //Return directly to j + + continue; } printf("His student number is%d", i + 1); for (j = 0; j < 5; j++) { sum += scores[i][j]; } printf("average%d", sum / 5); } } void findwell(int scores[4][5]) { int i, j,count; float sum; for (i = 0; i < 4; i++) { sum = 0.0; count = 0; for (j = 0; j < 5; j++) { sum += scores[i][j]; if (scores[i][j] > 85) { count++; } } if (count == 5 || (sum / 5) > 90) { for (j = 0; j < 5; j++) { printf("%d", scores[i][j]); } } } } int main() { //Find the average score of the first course void anverge(int scores[4][5]); void findlose(int scores[4][5]); void findwell(int scores[4][5]); int i, j; int scores[4][5]; printf("Please enter the five grades of these four students"); for (i = 0; i < 4; i++) { for (j = 0; j < 5; j++) { scanf("%d", &scores[i][j]); } } anverge(scores); findlose(scores); findwell(scores); return 0; }
The difficulty of this problem lies in the iteration of storing numbers and the nesting of multi-layer loops
If you save 123 to a[0] at the beginning: a[0]==0 at the beginning
A [0] = 10 * a [0] + 1 = 1 - > iterate to a [0] = 10 * a [0] + 2 = 12 - > iterate again a[0]=10*a[0]+3=123
#include<stdio.h> int main() { char str[32]; printf("Please enter a string"); gets(str); //Create an array to hold the numbers we separated int arr[32]; int* p = str; int count=0;//Record the length of the separated digital string while (*p != '\0') { if (*p > '0' && *p < '9') { while (*p > '0' && *p < '9') { arr[count] = arr[count] * 10 + *p - '0';//iteration p++; } count++; } p++;//Why do if and while loops repeat here, and the conditions are the same //That's why p + +, when we point to a non numeric character //We quit the cycle, //At this time, we need p + + to see if the next point is a number //Wonderful printf("Total%d Number", count); //Output these numbers int i; for (i = 0; i < count; i++) { printf("%d", arr[i]); } } }
#include<stdio.h> int my_strcmp(char leo1[30], char leo2[30]) { char* p1 = leo1; char* p2 = leo2; int ret=0; while (*p1 != '\0' && *p2 != '\0') { if (*p1 != *p2) { ret = *p1 - *p2; break; } p1++; p2++; } //Suppose one of them jumps out after it is empty, and we don't get ret, so let's do it again if (*p1 == '\0' || *p2 == '\0') { ret = *p1 - *p2; } return ret; } int main() { char leo1[30]; char leo2[30]; printf("Please enter two strings to compare"); gets(leo1); gets(leo2); int ret = my_strcmp(leo1, leo2); return 0; }
This problem requires you to use a secondary pointer
#include<stdio.h> struct date { int year; int month; int day; }; int main() { //Defines an array of days per month int days[] = { 0,31,28,31,30,31,30,31,30,31,31,30,31 }; struct date d; //Enter month, year and day scanf("%d %d %d", &d.year, &d.month, &d.day); int Day = 0;//Record the day of the year for (int i = 0; i < d.month; i++) { Day += days[i]; } //Add the remaining days Day += d.day; //If the month is greater than 2, we have to consider whether it is a leap year. If it is an additional day if (d.month > 2) { if (d.year % 400 == 0 || (d.year % 4 == 0 && d.year % 100 != 0)) { Day += 1; } } printf("This is the third time of the year%d day", Day); return 0; }
#include<stdio.h> struct date { int year; int month; int day; }; int days(struct date leo) { int month[] = { 0,31,28,31,30,31,30,31,30,31,31,30,31 }; int Day = 0; for (int i = 0; i < leo.month - 1; i++) { Day += month[i]; } Day += leo.day; if (leo.year%400==0||(leo.year%4==0&&leo.year%100!=0)) { if (leo.month > 2) { Day += 1; } } return Day; } int main() { struct date leo; printf("Please enter the date"); scanf("%d %d %d", &leo.year, &leo.month, &leo.day); messi(leo); return 0; }
The parameter num in the print function indicates the number of students passed in...
#include<stdio.h> struct Student { char Id[20]; char name[20]; float scores[3]; }; void input(struct Student stu[10]) { int i; printf("Please enter the scores of these ten students"); for (i = 0; i < 10; i++) { scanf("%s %s %f %f %f", stu[i].Id, stu[i].name, stu[i].scores[0], stu[i].scores[1], stu[i].scores[2]); } } void maxSocre(struct Student stu[10]) { int i,index; int max = stu[0].scores[0] + stu[1].scores[1] + stu[2].scores[2]; for (i = 1; i < 10; i++) { if (max < stu[i].scores[0] + stu[i].scores[1] + stu[i].scores[2]) { max = stu[i].scores[0] + stu[i].scores[1] + stu[i].scores[2]; index = i; } } //Data of the students with the highest scores printf("%s %s %f %f %f", stu[index].Id, stu[index].name, stu[index].scores[0], stu[index].scores[1], stu[index].scores[2]); } void anverge(struct Student stu[10]) { float a,b,c; for (int i = 0; i < 10; i++) { a += stu[i].scores[0]; b+= stu[i].scores[0]; c += stu[i].scores[0]; } printf("%f %f %f", a / 10.0, b / 10.0, c / 10.0);//Average score of three courses } int main() { struct Student stu[10]; input(stu); maxSocre(stu); anverge(stu); return 0; }
First define the attribute num:
num indicates sequence number. Next usually indicates the next point to...