1. Topic: there are n people in a circle, ranking in order, reporting numbers from the first (from 1 to 3). If the person who reports to 3 exits the circle, the last one left is the one with the original number.
Tip: complete with array
#include <stdio.h> #define N 100 int fun (int n, int m );//Declarative statement int main() { int n,m; printf("Please enter the number of people (the maximum is 100):\n"); scanf("%d",&n); printf("please input m:\n"); scanf("%d",&m); fun(n,m) ;//Call the fun function return 0; } int fun(int n,int m) { int a[N];//The maximum number of people allowed to enter is 100 int i, count = n;//i is to set the number of cycles int cnt = 0;//b is used to count 3. When the count reaches 3, return to zero. // int left = 0; for(i = 0;i < n;i++) { a[i] = i+1;//Assign value to a[i], from 1 to n } while (count != 1)//It means that there is no cycle until the last one is left. { for (i = 0;i < n ;i++) { if(a[i] == 0) { continue;//After the second round, skip counting for the person whose previous round is assigned to 0 } cnt++; if (cnt == m)//Assign a value of 0 to the person whose count is full of 3 { cnt = 0; a[i] = 0; count--;//count is actually the number of people removed } } } for(i = 0;i < n; i++) { if(a[i] != 0) { printf("The number of the remaining people is:\n"); printf("%d\n",a[i]); } } }
2. Title: write a C function to invert "I am from shanghai" to "shanghai from am I", that is, to invert the word position in the sentence without changing the internal structure of the word.
#include <stdio.h> #include <string.h> #include <stdlib.h> int main() { char *str[] = { "I", "am","from","shanghai" ,NULL}; int i = 0; int j = 0; int len = 0; char *temp; while (str[i] != NULL) { i++; len++; } for(i = 0, j = len - 1; j > i; i++,j--) { temp =*(str+i); *(str+i) = *(str+j); *(str+j) = temp; } for(i = 0; i < len; i++) { printf("%s ", *(str+i)); } printf("\n"); return 0; }
3. Title: input a string, and input the frame header and frame footer (can be multiple characters) to recognize the legal frames in the string.
Tip: the frame header and the frame tail are the head and tail strings respectively, and the head hauboisoktail in "asd headhauboisoktail" is the legal frame.
#include <stdio.h> int main() { char *str[] = {"head", "tail"}; char p[100]; printf ("Please enter a string( < 100)\n"); fgets (p, 100, stdin); int i = 0; int j = 0; int index = 0; // Subscript of i char *p1 = p; int m = 0; int flag = 0, s = 0;// Flag bit for (; i < 100; i++ ) { while (j < 4) { if (p[i] == str[m][j]) { flag = 1; s = m; i++; j++; } else { s = 0; flag = 0; break; } } if (s == 1 && flag == 1) { p[i] = '\0'; break; } if (flag == 1 && m == 0) { p1 = &p[i - j]; m = m + 1; j = 0; } } printf ("%s\n", p1); return 0; }