C语言习题小练习

本文包含六个部分(每个部分5个)的C语言小练习,具体如下:
一、
1.1韩信点兵 ------------ 1.2兰州烧饼 --------- 1.3进制转换 --------- 1.4第几天-----------1.5成绩转换
二、
2.1求实数的绝对值---- 2.2计算球体积 ------ 2.3两点距离 --------- 2.4ASCII码排序—2.5数值统计
三、
3.2公约数和公倍数---- 3.2五个数求最值-----3.3素数筛子算法----3.4分数加减法-----3.5数组求学生平均成绩
四、
4.1第二小整数---------- 4.2奇偶数分离------- 4.3奇偶位互换------ 4.4统计硬币------- 4.5汉字统计
五、
5.1偶数求和------------- 5.2杨辉三角---------- 5.3统计字符--------- 5.4 完数------------ 5.5素数回文
六、
6.1快速排序------------ 6.2开门人和关门人-- 6.3鸡兔同笼--------- 6.4日期计算------- 6.5开灯问题

1.1韩信点兵
让士兵先后以三人、五人、七人各排成一排,看队伍的排尾人数就知道总人数了(设总人数在10-100间)。
输入3个非负整数a,b,c ,表示每种队形排尾的人数(a<3,b<5,c<7),即余数,输出总人数的最小值(或报告无解)。

#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兰州烧饼
烧饼有两面,要做好一个兰州烧饼,要两面都弄热。有这么一个大平底锅,一次可以同时放入k个兰州烧饼。
一分钟能做好一面,而现在有n个兰州烧饼,至少需要多少分钟才能全部做好呢?

#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进制转换
输入一个10进制数n,将它转换成m进制数输出(最大为16进制)。

#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第几天?
给定一个日期,输出这个日期是该年的第几天。

#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,将其转换成对应的等级,具体转换规则如下:
90100为A;8089为B;7079为C;6069为D;0~59为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求实数的绝对值
输入7个数,输出其绝对值,保留2个小数。

#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计算球体的表面积和体积
根据输入的半径值,计算球的表面积和体积。

#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两点距离
入两点坐标(X1,Y1),(X2,Y2)(0<=x1,x2,y1,y2<=1000),计算并输出两点间的距离。

#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码排序
输入三个字符(可以重复)后,按各字符的ASCII码从小到大的顺序输出这三个字符。

#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数值统计
统计给定的n个数中,负数、零和正数的个数。

#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公约数和公倍数
输入2个正整数,求其最大公因数和最小公倍数;

#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五个数求最值
从5个随机整数中取最小数和最大数。

#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素数算法
输入正整数N,找出在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分数加减法
用整形数据实现两个分数的加减法。

#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数组求学生平均成绩
假设有一个学校,2个年级,3个班级,每个班级4学生,分别求他们的班级平均成绩,年级平均成绩以及校平均成绩。

#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第m小整数
求n个随机(也可以自己输入)整数中第m小的数。

#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奇偶数分离
随机给出一个100以内的正整偶数n,把1到n中的所有奇数和偶数分别从小到大输出。

#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字符串奇偶位互换
输入一个长度为偶数位的字符串(不含中文),请编程实现串的奇偶位互换。

#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统计硬币
假设一堆由1分、2分、5分组成的n个硬币总面值为m分,求一共有多少种可能的组合方式(某硬币数量可以为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汉字字数统计
统计给定一段文本中汉字的个数(可混入英文但是不能带空格)。

#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偶数序列求平均数
偶数序列(0-200)求前n个数每m个数的平均数,若后不足m个数,则取实际数量的平均值。

#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杨辉三角
打印n行杨辉三角图形。

#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统计字符
统计字符串s2中的各个字母在s1中出现的次数,包括空格。

#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完数
求1~n(包含n)间的所有完数,如6=1+2+3,且1不是完数.

#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素数回文
输出a~b(包含b且b<=100000)间所有的素数且是回文数

#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简单选择排序
对n个0~100之间的数字进行选择排序(网上排序方法很多,这里就用简单的选择排序法)

#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早到人与晚归人
给出员工的数量并输入其name编号以及早到和晚归时间,判断谁是最早到的人和谁和是最晚走的人。
因为考虑到时间的输入格式的判断,所以这段我写得很复杂。

#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鸡兔同笼
已知鸡和兔的总数量为n,总腿数为m。输入n和m,依次输出鸡和兔的数目。

#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日期计算
(与之前的第几天类似,详情参照之前的代码,故此处不给出代码。)
输入一个日期,格式如:2010 10 24 ,判断这一天是这一年中的第几天。
.
.

6.5开灯问题
有n盏灯,编号为1~n,第1个人把所有灯打开,后续第i个人按下所有编号为i倍数的开关
依此类推,一共有k人,输入:n和k,问最后有哪些灯开着? 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");
            }
        }
    }
}

如果对各个程序由不同意见或者更好的思路想法的,欢迎在评论区留言,一起探讨。
如果发现了程序中的bug和错误时,也欢迎指出,即时更正。
.
.

**声明:**本文习题除3.5外,其它全部来自于https://bingyishow.top/Technical-article/16.html的前6个部分,共有九个部分
·
·
但是本文的思路与解法都是自己摸索编写出来的,因为文中的问题我甚至都没看懂,大家感兴趣的可以去看看Ta的程序代码。
·
·
而后3个没时间写了,就先发这6部分的。

猜你喜欢

转载自blog.csdn.net/m0_52215008/article/details/111427051
今日推荐