1. Element Interchange
Defines an integer array num[10], randomly initializes the element values in the array, and then swaps the Kinsoku elements 22, outputting the swapped array values.
Code
#include <stdio.h>int main (void) { int num[10] = {}; int i = 0; int temp; for (i = 0; i < i++) { scanf ("%d", &num[i]); } for (i = 0; i < 10/2; i++) { temp = num[i]; Num[i] = num[9-i]; Num[9-i] = temp; } for (i = 0; i < i++) { printf ("%d", Num[i]); } printf ("\ n"); return 0;}
2. Matrix flipping
Defines a two-dimensional array a[3][4], randomly initializes the element values in the array, then flips the a array to the b[4][3] and outputs B.
Analysis
Input:
3 5 1 4
7 2 6 8
0 9 4 6
Output:
3 7 0
5 2 9
1 6 4
4 8 6
Code
#include <stdio.h>int main (void) { int a[3][4] = {}; int b[4][3] = {}; int i = 0; int j = 0; int temp; for (i = 0; i < 3; i++) {for (j = 0; J < 4; j + +) { scanf ("%d", &a[i][j]);} } for (i = 0; i < 3; i++) {for (j = 0; J < 4; j + +) { B[j][i] = a[i][j]; } } for (i = 0; i < 4; i++) { for (j = 0; J < 3; j + +) { printf ("%d", b[i][j]); } printf ("\ n"); } printf ("\ n"); return 0;}
3, Circle Count
There are m individuals in a circle, began to count, Report N, exit, ask what the last remaining is the date. (starting from number 1th)
Analysis
The M-person is stored in an array of a[m], and all of the elements are recorded as 1, and when a count is started, when N is reported, the person exits, and the value of the element is set to 0. Returns the first element count back when the last element of the array is reported. Repeat until the last element with a value of 1 is left.
Code
#include <stdio.h>int main (void) { int m = 0;//number of int n = 0;//off int a[10] = {}; int i = 0, j = 0, K = 0;//k indicates the current number of out laps// printf ("Input:"); scanf ("%d%d", &m, &n);// printf ("Input:"); for (i = 1; I <= m; i++) { a[i] = 1; } i = 1; while (1) { if (a[i] = = 1) { j + +; } if (j = = N) { A[i] = 0; j = 0; k++; } if (k = = m-1) break ; i++; if (i > M) { i = 1; } } printf ("output:\n"); for (i = 1; I <= m; i++) { if (a[i] = = 1) printf ("%d\n", I); } return 0;}
4, increment, decrement sequence
Input 10 number, any adjacent two number different, output all increment, decrement the sequence.
Analysis
Input:
1 5 9 8 12 21 3 0-1 9
Output:
1 5 9
9 8
8 12 21
21 3 0-1
-1 9
Code
#include <stdio.h>int main (void) { int i, k =-1; int a[10]; for (i = 0; i < i++) { scanf ("%d", &a[i]); } for (i = 1; i < i++) { if (k = = (A[i] > a[i-1]) { printf ("%d", A[i]); } else { k = (A[i] > A[i-1]); printf ("\n%d%d", a[i-1], a[i]); } } printf ("\ n"); return 0;}
5, the number of occurrences of the most
Enter 10 numbers to find the most frequently occurring number (if multiple side-by-side, output separately in the order of numbers)
Analysis
Input:
1 5 27 33 24 1 27 18 19 20
Output:
1 27
Code
#include <stdio.h>int main (void) { int a[10]; int i = 0, j = 0; int count[10] = {1,1,1,1,1,1,1,1,1,1}; int max = 0; for (i = 0; i < i++) { scanf ("%d", &a[i]); } for (i = 0; i < 9; i++) { //count[i] = 0; for (j = i + 1; j <; J + +) { if (a[i] = = A[j]) { if (count[i]! = 0) { count[i] + +; } Count[j] = 0; } } /* for (i = 0; i < i++) { if (count[i]! = 0) { printf ("%d", A[i]);} } */ max = 0; for (i = 1; i < i++) { if (Count[i] > Count[max]) { max = i; } } for (i = 0; i < i++) { if (count[i] = = Count[max]) { printf ("%d", A[i]);} } return 0;}
6. Spiral Type NXN Matrix
Enter two numbers, the first number determines a nxn matrix, and the second number determines the upper bound of the assignment from 1.
Analysis
Input:
5 23
Output:
1 2 3) 4 5
16 17 18) 19 6
15 0 0) 20 7
14 23 22) 21 8
13 12 11) 10 9
Code
#include <stdio.h>int main (void) { int i, J, r = 1; int k = 1; int n, m; int a[20][20] = {}; scanf ("%d%d", &n, &m); for (j = 0; J < N; j + +) { a[0][j] = k++; } for (i = n-1; I >= n/2; I-) {for (j = r; J <= I; j + +) { a[j][i] = k++; } for (j = i; J >= R; j--) { a[i][j-1] = k++; } for (j = i; j > R; j--) { a[j-1][r-1] = k++; } for (j = R; J < I; j + +) { a[r][j] = k++; } R + +; } for (i = 0; i < n; i + +) {for (j = 0; J < N; j + +) { if (A[i][j] > m) { A[i][j] = 0;< c36/>} printf ("%3d", A[i][j]); } printf ("\ n"); } return 0;}
7. Array interpolation
There is an array that is already sorted. Now enter a number that requires it to be inserted into the array as it was originally.
"Difficulty factor" ▲▲
Analysis
Code
#include <stdio.h>int main (void) {int arr[5] = {1,2,3,5,6};int i = 0;int index = 0;int num = 0;scanf ("%d", &num); Locate the position that should be inserted for (i = 0; i < 5; i + +) {if (num < arr[i]) {index = I;break;}} If NUM should be inserted in the position labeled 0~4, then insert//otherwise do not insert if (i! = 5) {for (i = 4; i > Index; i-) {arr[i] = arr[i-1];} Arr[index] = num;} for (i = 0; i < 5; i + +) {printf ("%d", Arr[i]);} printf ("\ n"); return 0;}
8. Reverse Array
To reverse an array
"Difficulty factor" ▲▲▲
Analysis
Code
#include <stdio.h>int main (void) {int arr[] = {1,2,3,4,5,6};int i = 0;int temp = 0;int len = sizeof (arr)/sizeof (arr[0 ]); for (i = 0; i < LEN/2; i + +) {temp = Arr[i];arr[i] = arr[len-1-i];arr[len-1-i] = temp;} for (i = 0; i < len; i + +) {printf ("%d", Arr[i]);} printf ("\ n"); return 0;}
9, Yang Hui Triangle
Print out the Yang Hui triangle (requires 10 lines to be printed)
1
1 1
1 2 1
1 3 3 1
1 4 6) 4 1
1 5 10 10 5 1
"Difficulty factor" ▲▲▲▲▲
Analysis
Yang Hui triangle:
Law:
When the value for the first column or row and column is equal to 1
otherwise a[i][j] = A[i-1][j-1] + a[i-1][j]
Code
#include <stdio.h>int main (void) {int arr[10][10] = {};int I, j;for (i = 0; i <; i + +) {for (j = 0; J <= I; j + +) {if (j = = 0 | | i = = j) Arr[i][j] = 1;elsearr[i][j] = arr[i-1][j-1] + arr[i-1][j];p rintf ("%4d", Arr[i][j]);} printf ("\ n");} return 0;}
10. Mantissa Front
There are n integers, so that they move backward in order of m positions, and the last m number becomes the first number of M.
"Difficulty factor" ▲▲▲▲
Analysis
C language provisions: When declaring an array, the length of the array must be constant, but the GCC compiler allows the declaration of the array with variables, but before declaring the array the variable must be assigned and once declared, the length of the array cannot be changed, and the array cannot be initialized, but can be assigned later.
Code
#include <stdio.h>void arr_move (int arr[], int len, int m) {int i = 0, j = 0;int temp = 0;for (i = len-m; i < len ; i + +) {temp = Arr[i];for (j = i; j > I-(len-m); J--) {arr[j] = arr[j-1];} ARR[J] = temp;}} int main (void) {int n = 0;int i = 0;int m = 0;scanf ("%d", &n); int arr[n];for (i = 0; i < n; i + +) {Arr[i] = i + 1;prin TF ("%d", Arr[i]);} printf ("\ n"); scanf ("%d", &m); Arr_move (arr, n, m); for (i = 0; i < n; i + +) {printf ("%d", Arr[i]);} printf ("\ n"); return 0;}
11. Statistics string
Enter a line of characters with a maximum length of 100, and count how many strings are in it, not punctuation. For example:
Input: Hi, Welcome to saif!!
Output: 4
"Difficulty factor" ▲▲▲
Analysis
Code
#include <stdio.h>int is_alpha (char ch) {if (ch >= ' a ' && ch <= ' z ' | | ch >= ' a ' && ch <= ' Z ') return 1;elsereturn 0;} int is_digit (char ch) {if (ch >= ' 0 ' && ch <= ' 9 ') return 1;elsereturn 0;} int Is_biaodian (char ch) {if (ch < ' 0 ' | | ch > ' 9 ' && Ch < ' a ' | | ch > ' Z ' && ch < ' a ' | | ch > ' z ') return 1;else return 0;} int main (void) {char str[100] = {};int counter = 0;int i = 0;scanf ("%[^\n]", str), while (str[i]! = ' + ') {if (Is_alpha (str[i) ) || Is_digit (Str[i])) && Is_biaodian (str[i + 1])) {counter + +;} i + +;} printf ("\n%d\n", counter); return 0;}
12. Find the longest length string
Enter a line of characters to find the word with the largest length. Requires a line of characters from the keyboard to print the string with the largest length. For example:
Input: When I am young I ' d listen to the radio waiting for my favorite songs
Output: Favorite
"Difficulty factor" ▲▲▲▲
Analysis
"Code"
#include <stdio.h>int main (void) {char str[100] = {};int i = 0;int counter = 0;int max = 0;int index = 0;int pos = 0; scanf ("%[^\n]", str), while (1) {if (Str[i]! = ") {counter + +;p rintf (" counter =%d\n ", counter);} if (str[i] = = ' | | str[i] = = ' + ') {if (Max < counter) {max = Counter;index = pos;printf ("max =%d, index =%d\n", Max, I Ndex);}} if (str[i] = = ' && str[i + 1]! = ') {pos = i + 1;counter = 0;printf ("pos =%d\n", POS);} if (str[i] = = ' + ') Break;i + +;} i = Index;while (str[i]! = "&& str[i]! = ' + ') {printf ("%c ", str[i]); i + +;} printf ("\ n"); return 0;}
13. Matrix Transpose
For example:
1 2 3 4 1 5 3 4
5 6 7 8->2 6 2 7
3 2 5 9 3 7 5 2
4 7 2 3 4 8 9 3
"Difficulty factor" ▲▲▲▲
Analysis
Code
#include <stdio.h>int main (void) {int n = 0;int I, J;int temp = 0;scanf ("%d", &n); int arr[n][n];for (i = 0; i < N i + +) {for (j = 0; J < N; j + +) {scanf ("%d", &arr[i][j]);}} for (i = 0; i < n; i + +) {for (j = 0; J < N; j + +) {printf ("%d", Arr[i][j]);} printf ("\ n");} for (i = 0; i < n; i + +) {for (j = 0; J < i; J + +) {temp = Arr[i][j];arr[i][j] = arr[j][i];arr[j][i] = temp;}} for (i = 0; i < n; i + +) {for (j = 0; J < N; j + +) {printf ("%d", Arr[i][j]);} printf ("\ n");} return 0;}
An array of classical programming in C language