C language exercises small exercises

This article contains six parts (5 in each part) of small exercises in C language, as follows:
1.
1.1 Han Xin's soldiers ------------ 1.2 Lanzhou biscuits --------- - 1.3 Hexadecimal conversion --------- 1.4 Days ----------- 1.5 Score conversion
2.
2.1 Finding the absolute value of a real number --- 2.2 Calculating the volume of a ball -- ---- 2.3 Distance between two points --------- 2.4 ASCII code sorting - 2.5 Numerical statistics
3.
3.2 Common divisors and common multiples ---- 3.2 Finding the maximum value of five numbers ----- 3.3 Prime numbers Sieve Algorithm----3.4 Score Addition and Subtraction-----3.5 Array Finding the Average Grade of
Students
4.3 Parity exchange ------ 4.4 Statistics of coins -------- 4.5 Statistics of Chinese characters
5.
5.1 Sum of even numbers ----------- 5.2 Yanghui triangle ----- ------ 5.3 Statistical characters --------- 5.4 Complete number ------------ 5.5 Prime palindrome
6.
6.1 Quick sort -------- ---- 6.2 The person who opens the door and the person who closes the door-- 6.3 Chickens and rabbits in the same cage --------- 6.4 Date calculation------- 6.5 Problems with turning on the lights

1.1 Han Xin
ordered the soldiers to line up three, five, and seven soldiers one after another. The total number of people can be known by the number of people at the end of the line (set the total number between 10-100).
Input 3 non-negative integers a, b, c, indicating the number of people at the end of each formation (a<3, b<5, c<7), that is, the remainder, and output the minimum value of the total number of people (or report no solution) .

#include <stdio.h>
int main()
{
    
    
    int a,b,c,i;
    re_input:printf("\n");
    printf("Please input the value of a,b and c with space between them:");
    scanf("%d%d%d",&a,&b,&c);
    if(a>=3 || a<0 || b>=5 || b<0 || c>=7 || c<0)
    {
    
    
        printf("Input is wrong, please re-input.");
        goto re_input;
    }
    for(i=10;i<=100;i++)
        if(i%3==a && i%5==b && i%7==c)
        {
    
    
            printf("This army has %d people.",i);
            break;
        }
    if(i>100)
        printf("No answer!");
}

.
.
1.2 Lanzhou biscuits
There are two sides of biscuits. To make a Lanzhou biscuit, both sides must be heated. There is such a large pan, and k Lanzhou biscuits can be put in at the same time.
One side can be prepared in one minute, but now there are n Lanzhou biscuits, at least how many minutes will it take to prepare all of them?

#include <stdio.h>
int main()
{
    
    
    int n,k,total,result;
    printf("Please input the value of k and n with space between them:");
    scanf("%d%d",&k,&n);
    for(;;)
    {
    
    
        total=2*n;
    if(n<=k)
        {
    
    printf("It takes only two minutes."); break;}
    result=total/k;
    if(total%k==0)
        {
    
    printf("It takes %d minutes.",result); break;}
    else
        {
    
    printf("It takes %d minutes.",++result); break;}
    }
}

.
.
1.3 Hexadecimal conversion
Input a decimal number n, convert it into m-ary number output (the maximum is hexadecimal).

#include <stdio.h>
#include <string.h>
char trans(int n,int m);
int main()
{
    
    
    int n,m,i,t;
    char a[50];
    re_input:printf("\n");
    printf("Please input a decimalism number n:");
    scanf("%d",&n);
    printf("Please input another scale number m:");
    scanf("%d",&m);
    if(n<0 || m<0 || m>16)  {
    
    printf("Your input is wrong, please try it again.");  goto re_input;}
    for(i=0;i<50;i++)
        {
    
    
            a[i]=trans(n,m);
            n=n/m;
            if(n==0)  {
    
    t=i; break;}
        }
    for(i=t;i>=0;i--)
        printf("%c",a[i]);
    goto re_reput;
}


char trans(int n,int m)
{
    
    
    switch(n%m)
    {
    
    
    case 0:  return '0';  break;
    case 1:  return '1';  break;
    case 2:  return '2';  break;
    case 3:  return '3';  break;
    case 4:  return '4';  break;
    case 5:  return '5';  break;
    case 6:  return '6';  break;
    case 7:  return '7';  break;
    case 8:  return '8';  break;
    case 9:  return '9';  break;
    case 10: return 'A';  break;
    case 11: return 'B';  break;
    case 12: return 'C';  break;
    case 13: return 'D';  break;
    case 14: return 'E';  break;
    case 15: return 'F';  break;
    }
    return 0;
}

.
.
1.4 What day?
Given a date, output the day of the year the date is.

#include <stdio.h>
#include <string.h>
int trans(int mon, int m);
int main()
{
    
    
    int year,mon,day,i,m=0,sum=0;
    re_input:printf("\n");
    printf("Please input the value of year, mon and day:");
    scanf("%d%d%d",&year,&mon,&day);
    if((year%4==0 && year%100!=0) || year%400==0)     m=1;
    if(year<0 || mon<0 || mon>12 || day<0 || day>31 || mon==2 &&((m==1 && day>29) || (m==0 && day>28)))
        {
    
    printf("Your input is wrong, please try it again.");  goto re_input;}
    for(i=1;i<mon;i++)
        sum=sum+trans(i,m);
    sum=sum+day;
    printf("%d-%d-%d is the %dth day of the year.",year,mon,day,sum);
}

int trans(int mon, int m)
{
    
    
    switch(mon)
    {
    
    
        case 1:  return 31; break;
        case 2:
            {
    
    
                if(m==0) return 28;
                else     return 29;
                break;
            }
        case 3:  return 31; break;
        case 4:  return 30; break;
        case 5:  return 31; break;
        case 6:  return 30; break;
        case 7:  return 31; break;
        case 8:  return 31; break;
        case 9:  return 30; break;
        case 10: return 31; break;
        case 11: return 30; break;
        case 12: return 31; break;
    }
}

.. 1.5 Score Conversion Input (
randomly ) five percentile scores and convert them into corresponding grades. The specific conversion rules are as follows: 90 100 is A; 80 89 is B; 70 79 is C; 60 69 is D; 0 ~59 for E.


#include <stdio.h>
#include <string.h>
#include <time.h>
char trans(int score);
int main()
{
    
    
    int score[5],i;
    memset(score,0,sizeof(score));
    srand(time(0));
    for(i=0;i<5;i++)
    {
    
    
        re_input:printf("\n");
        printf("Please input the score of No.%d student:",i+1);
        scanf("%d",&score[i]);
        if(score[i]>100 || score[i]<0)
            {
    
    printf("Your input is wrong, please try it again.");  goto re_input;}
        //score[i]=rand()%101;  //随机获得五个0-100之间的整数值,可代替上4行。
        printf("The score of No.%d student is %d, corresponding level is:%c.\n",i+1,score[i],trans(score[i]));
    }
}

char trans(int score)
{
    
    
    if(score>=90)      return 'A';
    else if(score>=80) return 'B';
    else if(score>=70) return 'C';
    else if(score>=60) return 'D';
    else if(score>=0)  return 'E';
}

.
.
2.1 Finding the absolute value of a real number
Input 7 numbers, output its absolute value, keep 2 decimals.

#include <stdio.h>
int main()
{
    
    
    double num,tru;
    int i;
    for(i=1;i<=7;i++)
    {
    
    
        if(i==1)        printf("Please the %dst number:",i);
        else if(i==2)   printf("Please the %dnd number:",i);
        else if(i==3)   printf("Please the %drd number:",i);
        else            printf("Please the %dth number:",i);
        scanf("%lf",&num);
        if(num<0)       tru=-num;
        else            tru=num;
        printf("|%lf|=%.2lf\n",num,tru);
    }
}

.
.
2.2 Calculate the surface area and volume of a sphere
Calculate the surface area and volume of a sphere according to the input radius value.

#include <stdio.h>
#include <math.h>
#define PI 3.1415927
int main()
{
    
    
    double R,A,V;
    printf("Please enter the radius of sphere:");
    scanf("%lf",&R);
    A=4*PI*pow(R,2);
    V=4*PI*pow(R,3)/3;
    printf("The Area of sphere is %.3lf\nThe Volume of sphere is %.3lf",A,V);
}

.
.
2.3 Distance between two points
Enter the coordinates of two points (X1, Y1), (X2, Y2) (0<=x1, x2, y1, y2<=1000), calculate and output the distance between the two points.

#include <stdio.h>
#include <string.h>
#include <math.h>
int main()
{
    
    
    double x[2],y[2],dist;
    printf("Please enter the x and y coordinate of the first  point:");
    scanf("%lf%lf",&x[0],&y[0]);
    printf("Please enter the x and y coordinate of the second point:");
    scanf("%lf%lf",&x[1],&y[1]);
    dist=pow((pow(x[1]-x[0],2)+pow(y[1]-y[0],2)),0.5);
    printf("The distance of the first point and the second point is %.3lf:",dist);
}

.
.

2.4ASCII code sorting
After inputting three characters (can be repeated), output these three characters according to the ASCII code of each character in ascending order.

#include <stdio.h>
void main()
{
    
    
    char a,b,c,t;
    printf("Please enter three characters:");
    scanf("%c%c%c",&a,&b,&c);
    if(a>b)   {
    
    t=a; a=b; b=t;}
    if(a>c)   {
    
    t=a; a=c; c=t;}
    if(b>c)   {
    
    t=b; b=c; c=t;}
    printf("The result of sort:%c < %c <%c",a,b,c);
}

.
.

2.5 Numerical Statistics
Count the number of negative numbers, zeros and positive numbers among the given n numbers.

#include <stdio.h>
#include <string.h>
#include <math.h>
void main()
{
    
    
    int n,i,pos=0,zer=0,neg=0;
    srand(time(0));
    n=rand()%100+1;
    int a[n];
    printf("Total number: %d\n",n);
    for(i=0;i<n;i++)
    {
    
    
        a[i]=rand()%100-50;
        printf("%6d",a[i]);
        if((i+1)%10==0)      printf("\n");
        if(a[i]>0)           pos++;
        else if(a[i]==0)     zer++;
        else                 neg++;
    }
    printf("\nThe number of positive is: %d",pos);
    printf("\n       The number of 0 is: %d",zer);
    printf("\nThe number of negative is: %d\n",neg);
}

.
.
3.1 Common divisor and common multiple
Input 2 positive integers, find their greatest common factor and least common multiple;

#include <stdio.h>
void main()
{
    
    
    int m,n,i,t;
    printf("Please enter two numbers:");
    scanf("%d%d",&m,&n);
    for(i=1;i<=m*n;i++)
    {
    
    
        if(m%i==0 && n%i==0)      t=i;
        if(i%m==0 && i%n==0)
            {
    
    
                printf("%3d is the greatest common factor of %d and %d\n",t,m,n);
                printf("%3d is the lowest common multiple of %d and %d",i,m,n);
                break;
            }
    }
}

.
.

3.2 Find the most value of five numbers
Take the smallest and largest number from 5 random integers.

#include <stdio.h>
#include <time.h>
void main()
{
    
    
    int a[5],i,t;
    srand(time(0));
    for(i=0;i<5;i++)
        {
    
    a[i]=rand()%100-50;  printf("%-4d",a[i]);}
    for(i=1;i<5;i++)
        if(a[0]>a[i])     {
    
    t=a[0]; a[0]=a[i]; a[i]=t;}
    for(i=0;i<4;i++)
        if(a[4]<a[i])     {
    
    t=a[4]; a[4]=a[i]; a[i]=t;}
    printf("\n%3d is the min\n%3d is the max",a[0],a[4]);
}

.
.
3.3 Prime Number Algorithm
Input a positive integer N and find out all the prime numbers in the numbers 2…N.

#include <stdio.h>
#include <string.h>
#include <math.h>
int main()
{
    
    
    int n,i,j,ct=0;
    printf("Please enter the up limited value n:");
    scanf("%d",&n);
    for(i=2;i<=n;i++)
    {
    
    
        for(j=2;j<i;j++)
            if(i%j==0)
                break;
        if(j==i)
        {
    
    
            printf("%-4d ",i);
            ct++;
            if(ct%10==0)
                printf("%\n");
        }
    }
}

.
.

3.4 Score addition and subtraction
Use plastic data to realize the addition and subtraction of two fractions.

#include <stdio.h>
int main()
{
    
    
    int a,b,c,d,denom,numer,i,t;
    char fh;
    printf("Please enter the fraction equation(like a/b ± c/d):");
    re_input:printf("\n");
    scanf("%d/%d%c%d/%d",&a,&b,&fh,&c,&d);
        denom=b*d;
        if(denom==0) {
    
    printf("Your input is wrong, please try it again.\n");  goto re_input;}
        if(fh=='+')  {
    
    numer=a*d+b*c;     printf("The original answer is %d/%d",numer,denom);}
        if(fh=='-')  {
    
    numer=a*d-b*c;     printf("The original answer is %d/%d",numer,denom);}
        for(i=1;i<=(abs(denom)<abs(numer)?abs(denom):abs(numer));i++)
            if(denom%i==0 && numer%i==0)
                t=i;
        numer=numer/t;denom=denom/t;
        if(numer%denom==0)   printf("\nThe simplified answer is %d",numer/denom);
        if(numer*denom<0)    printf("\nThe simplified answer is -%d/%d",abs(numer),abs(denom));
        if(numer*denom>0)    printf("\nThe simplified answer is %d/%d",abs(numer),abs(denom));
}

.
. .
3.5 Find the average grade of students in an array
Suppose there is a school with 2 grades, 3 classes, and 4 students in each class. Find their class average grade, grade average grade and school average grade respectively.

#include <stdio.h>
#include <math.h>
#include <string.h>
int main()
{
    
    
    int i,j,k,g=2,c=3,s=4;
    double sc,ss,sum=0,a[s];//a[g][c][s];
    memset(a,0,sizeof(a));
    for(i=0;i<g;i++)
    {
    
    
        sc=0;
        for(j=0;j<c;j++)
        {
    
    
            ss=0;
            for(k=0;k<s;k++)
            {
    
    
                printf("请输入年级%d,%d班第%d名学生的成绩:",i+1,j+1,k+1);
                scanf("%lf",&a[k]);
                ss=ss+a[k];//a[i][j][k]
            }
            printf("\n年级%d,%d班学生的平均成绩为:%f\n\n",i+1,j+1,ss/s);
            sc=sc+ss;
        }
        printf("\n年级%d学生的平均成绩为:%f\n\n\n",i+1,sc/(s*c));
        sum=sum+sc;
    }
    printf("\n全校学生的平均成绩为:%f\n",sum/(s*c*g));
return 0;
}

.
.

4.1 The mth smallest integer
Find the mth smallest number among n random (you can also enter it yourself) integers.

#include <stdio.h>
#include <string.h>
#include <time.h>
int main()
{
    
    
    int n,m;
    srand(time(0));
    printf("Please enter the total number and the mth smallest number:");
    while(scanf("%d%d",&n,&m)!=EOF)
    {
    
    
        if(m>n)  {
    
    printf("Your input is wrong, please try it again.\n");  continue;}
        int a[n],i,j,t;
        for(i=0;i<n;i++)
            {
    
    a[i]=rand()%101-50;   printf("%5d",a[i]);}
        for(i=0;i<n;i++)
            for(j=i+1;j<n;j++)
                if(a[i]>a[j])      {
    
    t=a[i];  a[i]=a[j];  a[j]=t;}
        printf("\n");
        for(i=0;i<n;i++)
            printf("%5d ",a[i]);
        printf("\nThe %dth smallest number is %d:",m,a[m-1]);
    }
}

.
.
4.2 Separation of odd and even numbers
Randomly give a positive integer n within 100, and output all odd and even numbers from 1 to n from small to large.

#include <stdio.h>
#include <time.h>
void main()
{
    
    
    int i,n;
    srand(time(0));
    n=2*(rand()%51);
    printf("n=%d\n",n);
    printf("The even number:\n");
    for(i=0;i<n;i+=2)
        {
    
    
            printf("%3d",i);
            if((i+2)%20==0)  printf("\n");
        }
    printf("\nThe odd number:\n");
    for(i=1;i<n;i+=2)
        {
    
    
            printf("%3d",i);
            if((i+1)%20==0)  printf("\n");
        }
}

.
.
4.3 String Parity Swap
Input a string with an even number of digits (excluding Chinese characters), please program to realize the string parity swap.

#include <stdio.h>
#include <string.h>
int main()
{
    
    
    int n,i;
    char s[100];
    memset(s,0,100);
    printf("Please enter the character string:");
    while(scanf("%s",s)!=EOF)
    {
    
    
        n=strlen(s);
        if(n%2!=0)        {
    
    printf("Your input is wrong, Please try it again.\n"); continue;}
        int t;
        for(i=0;i<n;i+=2) {
    
    t=s[i]; s[i]=s[i+1]; s[i+1]=t;}
        for(i=0;i<n;i++)   printf("%c",s[i]);
    }
}

.
.
4.4 Statistical coins
Assuming a pile of n coins consisting of 1 cent, 2 cents and 5 cents has a total denomination of m cents, find out how many possible combinations there are (the number of certain coins can be 0).

#include <stdio.h>
int main()
{
    
    
    int i,j,k,n,m;
    printf("Please enter the total quantity and the total value of coin:");
    while(scanf("%d%d",&n,&m)!=EOF)
    {
    
    
        int ct=0;
        if(n>m || 5*n<m)  {
    
    printf("Your input is wrong, Please try it again.\n"); continue;}
        printf("One cent\t Two cents\t Five cents\t Total value");
        for(i=0;i<=n;i++)
            for(j=0;j<=n;j++)
                for(k=0;k<=n;k++)
                    if(i+j+k==n && i+2*j+5*k==m)
                        {
    
    printf("\n   %-8d%11d%16d%18d\n",i,j,k,m);  ct++;}
        if(ct==0)        printf("\n\nNo solution.\n");
    }
}

.
.
4.5 Counting the number of Chinese characters
Count the number of Chinese characters in a given piece of text (English can be mixed but no spaces are allowed).

#include <stdio.h>
#include <string.h>
int main()
{
    
    
    char s[1000];
    memset(s,0,1000);
    printf("Please enter the character string without space:");  //不带空格,否则字符串结束。
    while(scanf("%s",s)!=EOF)
    {
    
    
        int n=strlen(s),i,ct=0;
            for(i=0;i<n;i+=2)
                {
    
    
                    if(i==0 && s[i]>=0 && s[i]<=127)         {
    
    i--;   continue;}
                    if((s[i]>127 || s[i]<0) && (s[i+1]>127 || s[i+1]<0))  ct++;
                    else    i--;
                }
        printf("There are %d Chinese characters.\n",ct);
    }
}

.
.
5.1 Calculate the average of the even-numbered sequence
Even-numbered sequence (0-200) calculates the average number of every m numbers of the first n numbers, if the last number is less than m numbers, take the average of the actual number.

#include <stdio.h>
int main()
{
    
    
    int a[101],n,m,i,j;
    for(i=0;i<101;i++)
        {
    
    a[i]=2*i; printf("%-4d",a[i]);}
    printf("\nPlease enter the anterior number n and the average number m:");
    while(scanf("%d%d",&n,&m)!=EOF)
    {
    
    
        for(i=0;i<=n/m;i++)
        {
    
    
            int sum=0,t;
            if(i==n/m)         t=n-i*m;
            else               t=m;
            for(j=0;j<t;j++)   sum=sum+a[i*m+j];
            if(t!=0)           printf("%-4d",sum/t);
        }
        printf("\n");
    }
}

.
.
5.2 Yanghui triangle
Print n lines of Yanghui triangle graphics.

#include <stdio.h>
int main()
{
    
    
    int n,i,j;
    printf("Please enter the row quantity of Yang hui's triangle:");
    scanf("%d",&n);
    int a[n][n];
    for(i=0;i<n;i++)
        for(j=0;j<=i;j++)
        {
    
    
            if(j==0 || i==j)
                {
    
    a[i][j]=1;                     printf("%-6d",a[i][j]);}
            else if(i>1 && j>0)
                {
    
    a[i][j]=a[i-1][j-1]+a[i-1][j]; printf("%-6d",a[i][j]);}
            if(i==j)             printf("\n");
        }
}

.
.
5.3 Character statistics
Count the number of times each letter in the string s2 appears in s1, including spaces.

#include <stdio.h>
#include <string.h>
int main()
{
    
    
    int i,j,n,m=100;
    char s1[m],s2[m];
    memset(s1,0,100);
    memset(s2,0,100);
    printf("Please enter the character string s1:");
    fgets(s1,100,stdin);           
    printf("Please enter the character string s2:");
    fgets(s2,100,stdin);
    n=strlen(s2);
    int a[n];
    memset(a,0,sizeof(a));
    for(i=0;i<n;i++)
    {
    
    
        if(s2[i+1]=='\0')   break;   //fgets(str,n,*p)的长度比scanf多1,末尾的\0也计入长度,因此需要判断跳出循环。
        for(j=0;j<m;j++)
                if(s2[i]==s1[j])  a[i]++;
        printf("\nCharacter '%c' in s2 appears -%d- times in s1.\n",s2[i],a[i]);
    }
}

.
.
5.4 Complete numbers
Find all the perfect numbers between 1~n (including n), such as 6=1+2+3, and 1 is not a perfect number.

#include <stdio.h>
#include <string.h>
int main()
{
    
    
    int n;
    printf("Please enter the upper limited value n:");
    while(scanf("%d",&n)!=EOF)
    {
    
    
        int i,j,ct=0;
        for(i=2;i<=n;i++)
        {
    
    
            int sum=0;
            for(j=1;j<i;j++)
                if(i%j==0) sum=sum+j;
            if(sum==i)           {
    
    printf("%d is a perfect number.\n",i); ct++;}
            if(i==n-1 && ct==0)   printf("There are zero perfect number between 1 and %d.\n",n);
        }
        printf("\n");
    }
}

.
.
5.5 Prime number palindrome
Output all prime numbers between a~b (including b and b<=100000) and are palindrome numbers

#include <stdio.h>
#include <string.h>
int main()
{
    
    
    int a,b;
    printf("Please enter the number a and b:\n");
    while(scanf("%d%d",&a,&b)!=EOF)
    {
    
    
        if(b<=0 || a>100000 || a>b)
        {
    
    printf("Your input is wrong, please try it again:\n");  continue;}
        if(a<=0)  a=1;
        int i,j,ct=0;
        for(i=a;i<=b;i++)
        {
    
    
            for(j=2;j<i;j++)
                if(i%j==0)  break;
            if(i==j)
            {
    
    
// It can be considered to use loop bodies to replace this loop body, to make a simpler program probably.
                if(i<10)
                    {
    
    printf("%-6d",i); ct++; if(ct%10==0)  printf("\n");}
                else if(i<100 && i/10==i%10)
                    {
    
    printf("%-6d",i); ct++; if(ct%10==0)  printf("\n");}
                else if(i<1000 && i/100==i%10)
                    {
    
    printf("%-6d",i); ct++; if(ct%10==0)  printf("\n");}
                else if(i<10000 && i/1000==i%10 && (i/10)%10==(i%1000)/100)
                    {
    
    printf("%-6d",i); ct++; if(ct%10==0)  printf("\n");}
                else if(i/10000==i%10 && (i/10)%10==(i%10000)/1000)
                    {
    
    printf("%-6d",i); ct++; if(ct%10==0)  printf("\n");}
            }
        }
        printf("\n");
    }
}

.
.
6.1 Simple Selection Sort
Select and sort n numbers between 0 and 100 (there are many sorting methods on the Internet, here is a simple selection sort method)

#include <stdio.h>
#include <string.h>
#include <time.h>
void main()
{
    
    
    srand(time(0));
    int n,i,j;
    printf("Please enter the number n:");
    scanf("%d",&n); // It's better to make the n smaller than 100 to avoid generating too many repetitive numbers.
    int a[n];
    memset(a,0,n);
    printf("Original data:\n");
    for(i=0;i<n;i++)
        {
    
    a[i]=rand()%101; printf("%-4d",a[i]);}
    for(i=0;i<n;i++)
    {
    
    
        int k=i,temp;
        for(j=i+1;j<n;j++)
            if(a[k]>a[j])  k=j;
        temp=a[i];a[i]=a[k];a[k]=temp;
    }
    printf("\nSorted data:\n");
    for(i=0;i<n;i++)
        printf("%-4d",a[i]);
}

.
.

6.2 Early arrivals and late returners
Give the number of employees and enter their name numbers and the time of early arrival and late return to determine who is the earliest arrival and who is the latest to leave.
Because of the judgment of the input format of the time, I wrote this paragraph very complicated.

#include<stdio.h>
#include<string.h>
int trans(char at,char lt,int b,int c);
int ctoin(char c,int a);
struct staff
    {
    
    
        char name[1000];
        char at[1000];
        char lt[1000];
    }st[1000];
void main()
{
    
    
    int a_time[1000],l_time[1000];
    memset(a_time,0,1000);
    memset(l_time,0,1000);
    memset(st,0,1000);
    int n,i,j;
    printf("Please enter the number of staff:");
    scanf("%d",&n);
    printf("name format:whatever\n");
    printf("time format:22:22\n\n");
    for(i=0;i<n;i++)
    {
    
    
        printf("Please enter the name,arrival time and leaving time of No.%d staff:",i+1);
        scanf("%s%s%s",&st[i].name,&st[i].at,&st[i].lt);
        // 对时间格式进行严格判断。
        if(strlen(st[i].at)!=5  || strlen(st[i].lt)!=5  // 确保是五个字符
           || trans(st[i].at[0], st[i].lt[0],48,50)     // 确保time第一个字符只能是'0~2'
           || trans(st[i].at[1], st[i].lt[1],48,57)     // 确保time第一个字符只能是'0~9'
           || st[i].at[2]!=58 || st[i].lt[2]!=58        // 确保time第一个字符只能是': '
           || trans(st[i].at[3], st[i].lt[3],48,54)     // 确保time第一个字符只能是'0~6'
           || trans(st[i].at[4], st[i].lt[4],48,57))    // 确保time第一个字符只能是'0~9'
//上述代码是调用函数实现,也可直接使用下五行代码实现,但是会比较复杂。
//             || st[i].at[0]<48  || st[i].lt[0]<48  || st[i].at[0]>50 || st[i].lt[0]>50
//             || st[i].at[1]<48  || st[i].lt[1]<48  || st[i].at[1]>57 || st[i].lt[1]>57
//             || st[i].at[2]!=58 || st[i].lt[2]!=58
//             || st[i].at[3]<48  || st[i].lt[3]<48  || st[i].at[3]>54 || st[i].lt[3]>54
//             || st[i].at[4]<48  || st[i].lt[4]<48  || st[i].at[4]>57 || st[i].lt[4]>57)
        {
    
    
            printf("Your input is wrong, please try it again.\n");
            i--;    continue;
        }
        else
            for(j=0;j<5;j++)
            {
    
    
                if(j==2)  continue;
                a_time[i]=a_time[i]+ctoin(st[i].at[j],j);
                l_time[i]=l_time[i]+ctoin(st[i].lt[j],j);
            }
    }
    for(i=0;i<n;i++)
    {
    
    
        int cta=0,ctl=0;
        for(j=0;j<n;j++)
            {
    
    
                if(a_time[i]<=a_time[j])  cta++;
                if(l_time[i]>=l_time[j])  ctl++;
            }
        if(cta==n)   printf("\n%s was the first one to arrive, the arrival time is %s.\n",st[i].name,st[i].at);
        if(ctl==n)   printf("\n%s was the  last one to  leave, the leaving time is %s.\n",st[i].name,st[i].lt);
    }
}

// 子函数部分
int trans(char at,char lt,int b,int c)
{
    
    
    return(at<b || lt< b || at>c || lt>c);
}

int ctoin(char c,int a)
{
    
    
    if(a==0)   return(10*(c-48)*3600);
    if(a==1)   return(   (c-48)*3600);
    if(a==3)   return(10*(c-48)*60);
    if(a==4)   return(   (c-48)*60);
}

.
.

6.3 Chickens and rabbits in the same cage
It is known that the total number of chickens and rabbits is n, and the total number of legs is m. Input n and m, and output the number of chickens and rabbits in turn.

#include <stdio.h>
int main()
{
    
    
    int n,m,i,j;
    printf("Please enter the total number and total legs number:");
    while(scanf("%d%d",&n,&m)!=EOF)
    {
    
    
        if(n>m/2) {
    
    printf("Incorrect input! please try it again.");  continue;}
        if(m%2!=0){
    
    printf("No solution.");  continue;}
        for(i=0;i<=n;i++)
            for(j=0;j<=n;j++)
            {
    
     if(i+j==n && 2*i+4*j==m)  printf("%d chickens and %d rabbits.",i,j); }
    }
}

.
.
6.4 Date Calculation
(Similar to the previous day, please refer to the previous code for details, so the code is not given here.)
Input a date in the format: 2010 10 24 to determine which day this day is in this year sky.
.
.

6.5 The problem of turning on the lights
There are n lights, numbered 1~n, the first person turns on all the lights, and then the i-th person presses all the switches numbered as multiples of i, and so on. There are k people in total, input: n and
k , ask which lights were left on at the end? k≤n≤1000

#include <stdio.h>
int main()
{
    
    
    int n,k,i,j;
    printf("Please enter the number of light and people:");
    while(scanf("%d%d",&n,&k)!=EOF)
    {
    
    
        if(k>n) {
    
    printf("Incorrect input! please try it again.");  continue;}
        int light[n],ct=0;
        for(i=0;i<n;i++)
            light[i]=1;
        // 纵向思路     
        for(i=0;i<n;i++)
            for(j=2;j<=k;j++)
                if((i+1)%j==0)  light[i]=!light[i];
        // 横向思路        
        //for(i=2;i<=k;i++)
        //    for(j=0;j<n;j++)
        //        if((j+1)%i==0)  light[j]=!light[j];
        for(i=0;i<n;i++)
        {
    
    
            if(light[i]==1)
            {
    
    
                printf("%-5d",i+1);
                ct++;
                if(ct%10==0)  printf("\n");
            }
        }
    }
}

If you have different opinions or better ideas for each program, please leave a message in the comment area and discuss together.
If you find bugs and errors in the program, you are welcome to point them out and correct them immediately.
.
.

**Disclaimer:** Except for 3.5, all the exercises in this article come from the first 6 parts of https://bingyishow.top/Technical-article/16.html. There are nine parts in total
.
However
, the ideas and solutions of this article are all I wrote it by myself, because I didn't even understand the questions in the article. If you are interested, you can take a look at Ta's program code.
·
·
Then I don't have time to write the last 3 parts, so I will post these 6 parts first.

Guess you like

Origin blog.csdn.net/m0_52215008/article/details/111427051