• C语言编程作业参考答案


    编程题参考答案

    week1_test

    Write a program to output the average of 2 integers.

    #include 
    
    void main()
    
    {
        int a , b;
        double c;
        printf("Please enter 1 integers\n");
        scanf("%d",&a);
        printf("Please enter another integers\n");
        scanf("%d",&b);
        c = (a+b)/2.0;
        printf("result:\n");
        printf("%f",c);
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15

    选择结构-编程题

    在这里插入图片描述

    #include 
    int main()
    {
     int num = 0;
     char ch;
     printf("请输入带替换的语句\n");
     while (ch = getchar())
     {
     if (ch == '#')
     {
     printf("\n%d", num);
     return 0;
     }
     else if (ch == '.')
     {
     printf("!");
     ++num;
     }
     else if (ch == '!')
     {
     printf("!!");
     ++num;
     }
     else putchar(ch);
     }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26

    在这里插入图片描述

    #include
    int main()
    {
     printf("If you are Single, type 1\n");
     printf("If you are Head of Household, type 2\n");
     printf("If you are Married and Joint, type 3\n");
     printf("If you are Married and Separate, type 4\n");
     int num;
     scanf("%d", &num);
     printf("Type tax income:\n");
     int income;
     double tax;
     scanf("%d", &income);
     switch (num)
     {
     case 1:
     if (income <= 17850) tax = income * 0.15;
     else tax = 17850 * 0.15 + (income - 17850) * 0.28;
     break;
     case 2:
     if (income <= 23900) tax = income * 0.15;
     else tax = 23900 * 0.15 + (income - 23900) * 0.28;
     break;
     case 3:
     if (income <= 29750) tax = income * 0.15;
     else tax = 29750 * 0.15 + (income - 29750) * 0.28;
     break;
     case 4:
     if (income < 14875) tax = income * 0.15;
     else tax = 14875 * 0.15 + (income - 14875) * 0.28;
     break;
     }
     printf("%.2lf", tax);
     return 0;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35

    3
    字符型数据可以简单分为数字、大写字母、小写字母及其他字符4类。从键盘输入一个字符,输出它的类型。

    #include 
    int main()
    {
     char ch = getchar();
     if (ch >= '0' && ch <= '9') printf("数字");
     else if (ch >= 'a' && ch <= 'z') printf("小写字母");
     else if (ch >= 'A' && ch <= 'Z') printf("大写字母");
     else printf("其他字符");
     return 0;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10

    4
    设计简单的计算器程序。要求根据用户输入的表达式进行计算:

     操作数1     运算符op   操作数2
    
    • 1

    指定的运算符为+ - * /

    操作数均为浮点数。

    #include
    int main()
    
    {
    
     double a,b;
    
     char x;
     printf("Please enter the Formula\n");
     scanf("%lf %c %lf",&a,&x,&b);
     printf("result:\n");
        if(x=='+')
     
     printf("%lf",a+b);
    
        else if(x=='-')
    
     printf("%lf",a-b);
    
     else if(x=='*')
    
     printf("%lf",a*b);
    
     else if(x=='/'&& b!=0)
    
     printf("%lf",a/b);
    
     else
    
     printf("error!\n");
    
     return 0;
    
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34

    5
    编写程序判断日期是否有效。用户输入日期(年、月、日),输出相应的判断结果。

    提示:年year为正整数,月month为1-12的整数,日day为0-maxDay的整数,其中maxDay的值取决于该日期所在的年份(是否为闰年)和月份(大月、小月或2月)

    #include 
    
    int main()
    
    {
    
     int year, month, day;
    
     scanf("%d%d%d", &year, &month, &day);
    
     switch (month)
    
     {
    
     case 1:
    
     case 3:
    
     case 5:
    
     case 7:
    
     case 8:
    
     case 10:
    
     case 12:
    
     if (day > 0 && day <= 31) printf("有效");
    
     else printf("无效");
    
     return 0;
    
     case 4:
    
     case 6:
    
     case 9:
    
     case 11:
    
     if (day > 0 && day <= 30) printf("有效");
    
     else printf("无效");
    
     return 0;
    
     case 2:
    
     if (year % 4 && day > 0 && day <= 28) printf("有效");
    
     else if (year%4==0 && year%100!=0||year%400==0 && day > 0 && day <= 29) printf("有效");
    
     else printf("无效");
    
     return 0;
    
     default:
    
     printf("无效");
    
     return 0;
    
     }
    
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67

    循环结构上机练习

    输入一个不超过65535的十进制正整数,计算转化为二进制后包含的1个个数。例如,输入10,输出2; 输入1024,输出1。

    请上传源代码和运行结果截图。

    #include 
    
    int main()
    
    {
    
     int n, sum = 0;
    
     scanf("%d", &n);
    
     while (n >= 1)
    
     {
    
     if (n % 2) ++sum;
    
     n /= 2;
    
     }
    
     printf("%d", sum);
    
     return 0;
    
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25

    数组编程

    1
    随机产生10个100以内的自然数(保存到数组),统计大于等于平均值的元素个数。

    #include
    
    #include
    
    int main()
    
    {
    
        int i;
    
        int ave=0;//平均数
    
        int index=0;
    
        int a[10];
    
        for(i=0;i<10;i++)
    
        {
    
            a[i]=rand()%100+1;
            printf("%d\n",a[i]);
    
        }
    
        for(i=0;i<10;i++)
    
        {
    
            ave+=a[i];
    
        }
    
        ave=ave/10;
    
        for(i=0;i<10;i++)
    
        {
    
            if(a[i]>ave)
    
            index++;
    
        }
        printf("平均值为:%d\n",ave);
        printf("大于平均值的元素个数是:%d\n",index);
    
        return 0;
    
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50

    2
    (1)利用数组保存Fibonacci数列的前20项.

    (2)验证F(n-1)/F(n)是否接近黄金分割数(0.618)。

    #include
    #include
    #include
    int main()
    {
        int i;
        int f[20] = { 1,1 };
        for (i = 2; i < 20; i++)
        {
            f[i] = f[i - 2] + f[i - 1];
        }
        for (i = 0; i < 20; i++)
        {
            if ((i + 1) % 5 == 0)
            {
                printf("\n");
            }
            printf("%8d", f[i]);
        }
        
        for (i = 19; i >= 1; i--)
        {
            printf("\n第%d项比前一项\n",i+1);
            printf("%5f", (1.0*f[i-1]/f[i]));
    
        }
        return 0;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28

    3
    输入8个小数,用选择排序算法和冒泡排序算法对其进行降序排序。
    选择排序

    #include
    void arr_out(double a[8])
    {
        int i = 0;
        for(i = 0;i < 8;i++)
        {
            printf("%lf ",a[i]);
        }
        printf("\n");
    }
    void arr_sort(double *p,int n)
    
    {
        int i,j;
        int min = 0;
        for(i = 0;i < n - 1;i++)
        {
            min = i;
            for(j = i + 1;j < n;j++)
            {
                if(p[j] < p[min])
                {
                    min = j;
                }
            }
            if(i != min)
    
            {
                double temp = p[i];
                p[i] = p[min];
                p[min] = temp;
           }  
        }
    }
    int main()
    {
        double a[8] = {0.0};
        int i = 0;
        printf("请输入要排序的数据:\n");
        for(i = 0;i < 8;i++)
        {
            scanf("%lf",&a[i]);
        }
        arr_sort(a,8);
        arr_out(a);
        return 0;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47

    冒泡排序

    #include
    
    int main()
    
    {
       double data[8];
       printf("请输入数据:\n");
       int i,j;
       for(i = 0;i < 8;i ++)
       {
       scanf("%lf",&data[i]); 
       }
       for(i = 0;i < 8;i ++)  
       {
       for(j = 0;j < 8 - 1 - i;j++)
       {
         if(data[j] > data[j+1])     
    {
    double temp;        
    temp = data[j];
    data[j] = data[j+1];
    data[j+1] = temp;
    
    }
    }
       }
       printf("排序好的数组为:\n");
       for(i = 0;i < 8;i ++)     
       {
        printf("%lf ",data[i]);
       }
       return 0;
    } 
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33

    4
    将两个从小到大有序整型数组a和b合并出一个有序整数数组c。

    #include
    #define N 20
    int main()
    {
     
    	int a[N]={ 0 }, b[N]={ 0 };
    	int i,j,k,anum,bnum,c[N+N];
        printf("输入数组a元素个数\n");
    	scanf("%d",&anum);   //输入数组a元素个数
    	for(i=0;i<anum;i++)
    	{
            printf("输入数组a元素第%d个元素\n",i);
    		scanf("%d",&a[i]);
    	}
        printf("输入数组b元素个数\n");
    	scanf("%d",&bnum);   //输入数组b元素个数
    	for(i=0;i<bnum;i++)
    	{
            printf("输入数组b元素第%d个元素\n",i);
    		scanf("%d",&b[i]);
    	}
    	/***** 在数组a和b都有数据时比较两个数组 *****/
    	/********** Begin *********/
    	i=0,j=0,k=0;
        while(i<anum&&j<bnum)
        {
            if(a[i]>=b[j])
            {
                c[k]=b[j];k++;j++;
            }
            else
            {
                c[k]=a[i];k++;i++;
            }
        }
    	/********** End **********/
    	/***** 如果数组a还有数据 *****/
    	/********** Begin *********/
    	for(i;i<anum;i++)
        {
            c[k]=a[i];k++;
        }
    	/********** End **********/
     
    	/***** 如果数组b还有数据 *****/
    	/********** Begin *********/
    	for(j;j<bnum;j++)
        {
            c[k++]=b[j];
        }
    	/********** End **********/
    	/***** 输出数组c *****/
    	/********** Begin *********/
    	for(i=0;i<anum+bnum;i++)
        {
            printf ("%d ",c[i]); 
        }
    	/********** End **********/	
    	return 0;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60

    5
    把一个数组中重复元素去掉, 即不在数组中存储, 输出数组中剩余的元素及其数目。例如,对于数组a[12]={1,1,2,7,3,2,3,4,5,8,7,4},输出剩余7个数据:1,2,7,3,4,5,8。

    
    #include
    
    int main(void)
    
    {
    
     int i,n,a[100];
    
     int j, temp;
    
     printf("输入元素的个数:");
    
     scanf("%d",&n);
    
     for(i=0;i<n;i++)
    
     scanf("%d",&a[i]);
    
     for(i=0;i<n;i++)
    
     {
    
     for(j=i+1;j<n;j++)
    
     {
    
     if(a[i]==a[j])
    
     {
    
     for(temp = j;temp<n-1;temp++)
    
     a[temp]=a[temp+1];
    
     j--;
    
     n--;
    
     }
    
     }
    
     }
    
     printf("剩余元素个数: %d\n",n);
    
     for(i=0;i<n;i++)
    
     printf("%d ",a[i]);
    
     printf("\n");
    
     return 0;
    
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56

    6
    用筛法计算100之内的素数。
    算法概述:先把N个自然数按次序排列起来。1不是质数,也不是合数,要划去。第二个数2是质数留下来,而把2后面所有能被2整除的数都划去。2后面第一个没划去的数是3,把3留下,再把3后面所有能被3整除的数都划去。3后面第一个没划去的数是5,把5留下,再把5后面所有能被5整除的数都划去。这样一直做下去,就会把不超过N的全部合数都筛掉,留下的就是不超过N的全部质数。

    #include
    
    #include
    
    int main()
    
    {
    
        int i,j,k=0,a[100];
    
        for(i=0;i<100;i++)
    
        {
    
            a[i]=i+1;
    
        }
    
     a[0]=0;
    
        for(i=0;i<99;i++)
    
        {
    
            for(j=i+1;j<100;j++)
    
            {
    
                if(a[i]!=0&&a[j]!=0)
    
                {
    
                    if(a[j]%a[i]==0)
    
                    {
    
                        a[j]=0;
    
                    }
    
                }
    
            }
    
     }
    
        printf(" 筛选法求出100以内的素数为:\n");
    
        for(i=0;i<100;i++)
    
        {
    
            if(a[i]!=0)
    
            {
    
              printf("%4d",a[i]);          
    
              k++;
    
            }
    
            if(k%10==0)
    
            {
    
                printf("\n");
    
            }
    
        }
    
        printf("\n");
    
        return 0;
    
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77

    7
    随机产生20个不大于10的自然数,将其存储在一个二维数组中,且偶数与奇数分别存储在不同列上。输出该数组并统计偶数与奇数的个数。

    #include
    
    #include
    
    #include
    
    #include
    
    #include
    
    void main()
    
    {
    
     srand(time(NULL));
    
     int a[2][20] ,n[20],x=0,y=0;
    
     for(int m=0;m<=19;m++)
    
     {
    
     n[m]=rand()%10+1;
    
     if(n[m]%2==0)
    
     {
    
     a[0][x]=n[m];
    
     x++;
    
     }
    
     else
    
     {
    
     a[1][y]=n[m];
    
     y++;
    
     }
    
     }
    
     printf("偶数有%d个,奇数有%d个\n",x,y);
     printf("偶数\n");
     for(int c=0;c<=x;c++)
        {
        printf("%-3d",a[0][c]);
        }
    
    printf("\n奇数\n");   
    
     for(int c=0;c<=y;c++)
        {
             printf("%-3d",a[1][c]);
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60

    8
    输出杨辉三角形前10行。格式如下:

                            1
    
                         1      1
    
    				    1    2    1
    
    				  1    3    3    1
    
    			   1    4    6    4    1
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    #include "stdio.h"
    int main()
    {
        int i,j,a[11][11]= {0,1};
        for(i=1; i<=10; i++)
        {
            for(j=1; j<=i; j++)
            {
                a[i][j]=a[i-1][j-1]+a[i-1][j];
                printf("%d ",a[i][j]);
            }
            printf("\n");
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14

    9
    找出二维数组中的所有鞍点,既该位置的元素在该行上最大,但是在该列上最小。也有可能没有鞍点。

    #include
    #include
    #include 
    #define M 2
    #define N 10
    
    int main()
    {
    	int max, min, rowindex, colindex, flag = 0;
    	int array[M][N];
    	printf("随机生成%d行%d列的数组:\n", M, N);
    	for (int i = 0; i < M; i++)
    	{
    		for (int j = 0; j < N; j++)
                {
                    array[i][j]=rand()%100;
                    printf("%d ",array[i][j]);
                }
            printf("\n");
                
    	}
    
    	for (int i = 0; i < M; ++i)
    	{
    		// 找到i行上最大的元素,记录该元素在列号colindex
    		max = array[i][0];
    		for (int j = 0; j < N; ++j)
    		{
    			if (array[i][j] > max)
    			{
    				max = array[i][j];
    				colindex = j;
    			}
    		}
    
    		// 找max所在列colindex上最小的元素,并记录其所在的行
    		min = array[0][colindex];
    		for (int j = 0; j < M; ++j)
    		{
    			if (array[j][colindex] < min)
    			{
    				min = array[j][colindex];
    				rowindex = j;
    			}
    		}
    
    		// 如果最小元素与最小元素相同,并且最小元素也在第i行,则为鞍点
    		if (max == min && i == rowindex)
    		{
    			flag = 1;
    			printf("鞍点为:%d行%d列的元素%d", rowindex, colindex, max);
    			break;
    		}
    	}
    
    	if (0 == flag)
    		printf("没有鞍点");
    	return 0;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59

    10
    在这里插入图片描述

    #include 
    
    #include 
    
    #include 
    
    #include 
    
    #define MAX 100
    
    #define N 10
    
    int main(void)
    
    {
    
      int n[5][3];
    
      int i,j,k=0;
    
      double average;
    
      int max,temp;
    
      printf("输入五组两位数,每组三个数\n");
    
       for(i=0;i<5;i++)
    
       {
    
        for(j=0;j<3;j++)
    
      scanf("%d",&n[i][j]); 
    
       }
    
       for(i=0;i<5;i++)
    
       {
    
        for(j=0;j<3;j++)
    
      printf("%d ",n[i][j]); 
    
       }               
    
       printf("\n");        /*输入并打印数组*/
    
        for(i=0;i<5;i++)
    
       {
    
        for(j=0;j<3;j++)
    
      {
    
       k+=n[i][j];
    
        
    
      } 
    
       }
    
       average=k*1.0/15;
    
       printf("所有数的平均值为%.2f\n",average);/*计算所有值的平均值*/
    
       for(i=0;i<5;i++)
    
       {
    
        for(j=0;j<3;j++)
    
      {
    
       k+=n[i][j];
    
        
    
      } 
    
      average=k*1.0/5;
    
      printf("第%d组的平均值为%.2f\n",i+1,average); 
    
       }                                    /*计算每组平均值*/
    
       for(i=0;i<5;i++)
    
       {
    
        for(j=0;j<3;j++)
    
      {
    
       int max=n[i][j];
    
       if(max<n[i][j+1])
    
       {
    
         temp=max;
    
       max=n[i][j+1];
    
       n[i][j+1]=temp; 
    
     }
    
       
    
      } 
    
       }
    
       printf("最大值为%d",max);              
    
     return 0;
    
     
    
     
    
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
    • 92
    • 93
    • 94
    • 95
    • 96
    • 97
    • 98
    • 99
    • 100
    • 101
    • 102
    • 103
    • 104
    • 105
    • 106
    • 107
    • 108
    • 109
    • 110
    • 111
    • 112
    • 113
    • 114
    • 115
    • 116
    • 117
    • 118
    • 119
    • 120
    • 121
    • 122
    • 123
    • 124
    • 125

    函数编程2

    1
    注册邮箱:有效的邮箱用户名ID不少于6个字符并不多于12个字符,必须由大写字母、小写字母与数字组合而成,并且不能和已有ID冲突。输入1个字符串,检测是否是有效ID,如果有效则存储起来,若无效则输出提示并继续尝试注册。例如,

    输入Vivian247, 输出“注册成功”

    输入Vivi9,vivian, 123455, 输出“无效用户名”

    输入Vivian247, 输出“该用户名已注册”

    定义valid()函数检测ID是否有效;定义search()函数在已有ID中查找是否存在;定义save()函数将新的合法ID保存

    #include 
    #include
    bool valid(char* ch)
    
    {
    
    bool alpha1 = false, digit = false, alpha2 = false;
    
    for (int i = 0; i < 1000; ++i)
    
    {
    
    if (ch[i] == '\0' && (i >= 7 && i <= 13) && digit && alpha1 && alpha2)
    
    return false;
    
    if (ch[i] >= '0' && ch[i] <= '9')
    
    digit = true;
    
    if (ch[i] >= 'a' && ch[i] <= 'z')
    
    alpha1 = true;
    
    if (ch[i] >= 'A' && ch[i] <= 'Z')
    
    alpha2 = true;
    
    if (ch[i] == '\0')
    
    break;
    
    }
    
    return true;
    
    }
    
    bool search(char(*all)[13], char* ch, int n)
    
    {
    
    for (int i = 0; i < n; ++i)
    
    {
    
    for (int j = 0; j < 13; ++j)
    
    {
    
    if (all[i][j] != ch[j])
    
    break;
    
    if (j == 12)
    
    return true;
    
    }
    
    }
    
    return false;
    
    }
    
    void save(char(*all)[13], char* ch, int n)
    
    {
    
    for (int i = 0; i < 13; ++i)
    
    all[n][i] = ch[i];
    
    }
    
    int main()
    
    {
    
    char mail[1000][13], count = 0;
    
    char temp[1000] = {};
    printf("有效的邮箱用户名ID不少于6个字符并不多于12个字符,必须由大写字母、小写字母与数字组合而成,并且不能和已有ID冲突\n");
    while (true)
    
    {
    
    printf("注册请输入邮箱用户名ID,退出请输入0:\n");
    
    fgets(temp, 1000, stdin);
    
    if (temp[0] == '0' && temp[1] == '\n')
    
    break;
    
    if (valid(temp))
    
    {
    
    printf("无效用户名\n");
    
    continue;
    
    }
    
    if (search(mail, temp, count))
    
    {
    
    printf("该用户名已注册\n");
    
    continue;
    
    }
    
    save(mail, temp, count++);
    
    printf("注册成功\n");
    
    }
    
    printf("已退出注册");
    
    return 0;
    
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
    • 92
    • 93
    • 94
    • 95
    • 96
    • 97
    • 98
    • 99
    • 100
    • 101
    • 102
    • 103
    • 104
    • 105
    • 106
    • 107
    • 108
    • 109
    • 110
    • 111
    • 112
    • 113
    • 114
    • 115
    • 116
    • 117
    • 118
    • 119
    • 120
    • 121
    • 122
    • 123
    • 124
    • 125
    • 126
    • 127

    2
    Write a function str_index(char s[], char t[]), which returns the position of the rightmost occurrence of t in s, or -1 if there is none, where s and t store two strings. (例如:字符串s是hello,world 字符串t是wor,那么应该返回6)

    #include 
    #include 
    int str_index(char* a, char* b)
    
    {
    
    int pos = -1;
    
    bool judge = true;
    
    for (int i = 0; a[i] != '\0'; ++i)
    
    {
    
    for (int j = 0; a[i + j] != '\0' && b[j] != '\0'; ++j)
    
    {
    
    if (a[i + j] != b[j])
    
    {
    
    judge = false;
    
    break;
    
    }
    
    }
    
    if (judge)
    
    pos = i;
    
    judge = true;
    
    }
    
    return pos;
    
    }
    
    int main()
    
    {
    
    char str1[1000] = {}, str2[1000] = {};
    
    printf("请输入第一个字符串:");
    
    scanf("%s", str1);
    
    printf("请输入第二个字符串:");
    
    scanf("%s", str2);
    
    int pos = str_index(str1, str2);
    
    if (pos == -1)
    
    printf("不存在");
    
    else printf("存在,最后出现的起始下标为%d", pos);
    
    return 0;
    
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67

    3
    Write a function double stod(char s[]) that returns a double value corresponding to a given string: input “-123.56”, return the value-123.56. Call isspace to isdigit to skip preceding white spaces and handle unexpected inputs. (假设 给的字符串是可以转成double的格式合法的)

    #include 
    
    #include 
    
    #include 
    
    double stod(char* ch)
    
    {
    
     double num = 0;
    
     bool nag = false;
    
     for (int i = 0, j = 0; ch[i] != '\0'; ++i)
    
     {
    
     if (ch[i] == '-')
    
     nag = true;
    
     else if (ch[i] == '.')
    
     j++;
    
     else if (j && isdigit(ch[i]))
    
     num += ((ch[i] - '0') / pow(10, j++));
    
     else if (isdigit(ch[i]))
    
     num = num * 10 + (ch[i] - '0');
    
     }
    
     if (nag) return -num;
    
     return num;
    
    }
    
    int main()
    
    {
    
     char str[10000] = {};
    
     printf("请以字符串形式输入一个浮点数:");
    
     scanf("%s", str);
    
     double num = stod(str);
    
     printf("输入的浮点数是:%g", num);
    
     return 0;
    
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59

    4
    Generate an array with 500 or more random integers. Implement bubble, insertion, selection and two versions (recursive and non-recursive) of quick sorting programs.
    Compare their performance.

    #include 
    
    #include 
    
    #include 
    
    
    //500个太少了,计时精度不够
    void Bubble_sort(int arr[], int size)
    {
    	int j,i,tem;
    	for (i = 0; i < size-1;i ++)//size-1是因为不用与自己比较,所以比的数就少一个
    	{
    		int count = 0;
    		for (j = 0; j < size-1 - i; j++)	//size-1-i是因为每一趟就会少一个数比较
    		{
    			if (arr[j] > arr[j+1])//这是升序排法,前一个数和后一个数比较,如果前数大则与后一个数换位置
    			{
    				tem = arr[j];
    				arr[j] = arr[j+1];
    				arr[j+1] = tem;
    				count = 1;
    				
    			}
    		}
    		if (count == 0)			//如果某一趟没有交换位置,则说明已经排好序,直接退出循环
    				break;	
    	}
    
    }
    
    void InsertionSort(int* arr, int len) {
    
        int temp, i, j;
    
        for (i = 1; i <= len - 1; i++) {
    
            temp = arr[i];
    
            for (j = i - 1; j >= 0 && temp < arr[j]; j--) {
    
                arr[j + 1] = arr[j];
    
            }
    
            arr[j + 1] = temp;
    
        }
    
    }
    
    int Partition(int* arr, int low, int high)
    {
    	//一次划分
    	int tmp = arr[low];//基准
    	while (low < high)
    	{
    		//(1)从后往前找比基准小的数字
    		while (low<high && arr[high]>tmp)
    		{
    			high--;
    		}
    		if (low < high)
    		{
    			arr[low] = arr[high];
    		}
    		//(2)从前往后找比基准大的数字
    		while (low < high && arr[low] <= tmp)
    		{
    			low++;
    		}
    		if (low < high)
    		{
    			arr[high] = arr[low];
    		}
    	}
    	arr[low] = tmp;
    	return low;
    }
    void Quick(int* arr, int low, int high)
    {
    	int par = Partition(arr,low, high);
    	if (low < par - 1)//左边数据超过一个
    	{
    		Quick(arr, low, par - 1);
    	}
    	if (par + 1 < high)//右边数据超过一个
    	{
    		Quick(arr, par + 1, high);
    	}
    }
    void QuickSort(int* arr, int len)
    {
    	Quick(arr, 0, len - 1);//参数一致
    
    }
    void SelectionSort(int* arr, int len) {
    
        int min;
    
        int temp = 0;
    
        int k = 0;
    
        int i;
    
        for (i = 0; i <= len - 2; i++) {
    
            min = i;
    
            int j;
    
            for ( j = i + 1; j <= len - 1; j++) {
    
                if (arr[j] < arr[min])
    
                    min = j;
    
            }
    
            temp = arr[i];
    
            arr[i] = arr[min];
    
            arr[min] = temp;
    
        }
    
    }
    
    void main() {
    
        long i = 10000;
    
        int arr[5000];
    
        clock_t start, finish;
    
        double total_time;
    
        int n = 0;
        int j;
        printf("quick sort\n");
        start = clock();  
        while (--i) { 
            for (j = 0; j < 5000; j++) {
                arr[j] = rand() % 100 + 1;           
            }
    
        }
        QuickSort(arr, sizeof(arr) / sizeof(arr[0]));
        finish = clock(); 
        //for (j = 0; j < 5000; j++) {
        //printf("%d ",arr[j]);
        //}
        total_time = (double)(finish - start) / CLOCKS_PER_SEC;
        printf("\ntime cost of QuickSort = %lf seconds\n", total_time);
        printf("\nBubblesort\n");
        i = 10000;
        while (--i) { 
            for (j = 0; j < 5000; j++) {
                arr[j] = rand() % 100 + 1;        
            }   
        }
        start = clock();  
        Bubble_sort(arr, sizeof(arr) / sizeof(arr[0]));
        finish = clock(); 
        //for (j = 0; j < 5000; j++) {
        //printf("%d ",arr[j]);
        //}
        total_time = (double)(finish - start) / CLOCKS_PER_SEC;
        printf("\ntime cost of Bubble_sort = %lf seconds\n", total_time);
    
        printf("\nSelectionSort\n");
        i = 10000;
        while (--i) { 
            for (j = 0; j < 5000; j++) {
                arr[j] = rand() % 100 + 1;        
            }   
        }
        start = clock();  
        SelectionSort(arr, sizeof(arr) / sizeof(arr[0]));
        finish = clock(); 
        //for (j = 0; j < 5000; j++) {
        //printf("%d ",arr[j]);
        //}
        total_time = (double)(finish - start) / CLOCKS_PER_SEC;
        printf("\ntime cost of SelectionSort = %lf seconds\n", total_time);
        
    
        printf("\nInsertionSort\n");
        i = 10000;
        while (--i) { 
            for (j = 0; j < 5000; j++) {
                arr[j] = rand() % 100 + 1;        
            }   
        }
        start = clock();  
        InsertionSort(arr, sizeof(arr) / sizeof(arr[0]));
        finish = clock(); 
        //for (j = 0; j < 5000; j++) {
        //printf("%d ",arr[j]);
        //}
        total_time = (double)(finish - start) / CLOCKS_PER_SEC;
        printf("\ntime cost of InsertionSort = %lf seconds\n", total_time);
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
    • 92
    • 93
    • 94
    • 95
    • 96
    • 97
    • 98
    • 99
    • 100
    • 101
    • 102
    • 103
    • 104
    • 105
    • 106
    • 107
    • 108
    • 109
    • 110
    • 111
    • 112
    • 113
    • 114
    • 115
    • 116
    • 117
    • 118
    • 119
    • 120
    • 121
    • 122
    • 123
    • 124
    • 125
    • 126
    • 127
    • 128
    • 129
    • 130
    • 131
    • 132
    • 133
    • 134
    • 135
    • 136
    • 137
    • 138
    • 139
    • 140
    • 141
    • 142
    • 143
    • 144
    • 145
    • 146
    • 147
    • 148
    • 149
    • 150
    • 151
    • 152
    • 153
    • 154
    • 155
    • 156
    • 157
    • 158
    • 159
    • 160
    • 161
    • 162
    • 163
    • 164
    • 165
    • 166
    • 167
    • 168
    • 169
    • 170
    • 171
    • 172
    • 173
    • 174
    • 175
    • 176
    • 177
    • 178
    • 179
    • 180
    • 181
    • 182
    • 183
    • 184
    • 185
    • 186
    • 187
    • 188
    • 189
    • 190
    • 191
    • 192
    • 193
    • 194
    • 195
    • 196
    • 197
    • 198
    • 199
    • 200
    • 201
    • 202
    • 203
    • 204
    • 205
    • 206

    5
    编写函数 void inverse( int arr[ ][N] ) ,实现N*N的矩阵转置操作, 其中N为常量。

    #include 
    
    int n;
    
    void inverse(int(*arr)[100])
    
    {
    
    int temp[100][100] = {};
    
    for (int i = 1; i <= n; ++i)
    
    for (int j = 1; j <= n; ++j)
    
    temp[i][j] = arr[j][i];
    
    for (int i = 1; i <= n; ++i)
    
    for (int j = 1; j <= n; ++j)
    
    arr[i][j] = temp[i][j];
    
    }
    
    int main()
    
    {
    
    int num[100][100] = {};
    
    printf("请输入矩阵边长:");
    
    scanf("%d", &n);
    
    printf("请输入矩阵中的数据:\n");
    
    for (int i = 1; i <= n; ++i)
    
    for (int j = 1; j <= n; ++j)
    
    scanf("%d", &num[i][j]);
    
    inverse(num);
    
    printf("矩阵转置结果:\n");
    
    for (int i = 1; i <= n; ++i)
    
    {
    
    for (int j = 1; j <= n; ++j)
    
    printf("%d ", num[i][j]);
    
    putchar('\n');
    
    }
    
    return 0;
    
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61

    6
    统计M名学生的N门课的成绩, 分别实现以下功能:

    (1)输入M个学生的名字和成绩

    (2)double ave( int grade[][N], int n); 计算某名同学的平均分

    (3)double pass(int grade[][N], int n ); 统计某门课程的及格率。

    (4)void print(int grade[][N],char name[][10], int m)

    //char name[][10] 是存储名字的字符串数组

    按如下格式输出所有成绩。(无需输出横竖表格线)
    在这里插入图片描述

    要求: 不能把成绩数组定义为全局变量。

    #include 
    
    double Pass[3];
    
    int m;
    
    double ave(int(*grade)[3], int n)
    
    {
    
    return 1.0 * (grade[n][0] + grade[n][1] + grade[n][2]) / 3;
    
    }
    
    double pass(int(*grade)[3], int n)
    
    {
    
    double sum = 0;
    
    for (int i = 0; i < m; ++i)
    
    if (grade[i][n] >= 60)
    
    sum++;
    
    return sum / m;
    
    }
    
    void print(int(*grade)[3], char(*name)[10], int m)
    
    {
    
    printf("        ");
    
    for (int i = 0; i < m; ++i)
    
    printf("%9s ", name[i]);
    
    printf("及格率");
    
    for (int i = 0; i < 3; ++i)
    
    {
    
    if (i == 0) printf("\nMath    ");
    
    if (i == 1) printf("\nC       ");
    
    if (i == 2) printf("\nEnglish ");
    
    for (int j = 0; j < m; ++j)
    
    printf("%9d ", grade[j][i]);
    
    printf("%6.4g", Pass[i]);
    
    }
    
    printf("\naverage ");
    
    for (int i = 0; i < m; ++i)
    
    printf("%9.7g ", ave(grade, i));
    
    printf("%6.4g", (Pass[0] + Pass[1] + Pass[2]) / 3);
    
    }
    
    int main()
    
    {
    
    int grade[100][3] = {};
    
    char name[100][10] = {};
    
    printf("请输入学生人数:");
    
    scanf("%d", &m);
    
    printf("请输入每个学生的姓名和三科成绩:\n");
    
    for (int i = 0; i < m; ++i)
    
    {
    
    scanf("%s", name[i]);
    
    for (int j = 0; j < 3; ++j)
    
    scanf("%d", &grade[i][j]);
    
    }
    
    for (int i = 0; i < 3; ++i)
    
    Pass[i] = pass(grade, i);
    
    print(grade, name, m);
    
    return 0;
    
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
    • 92
    • 93
    • 94
    • 95
    • 96
    • 97
    • 98
    • 99
    • 100
    • 101
    • 102
    • 103
    • 104
    • 105

    参考了部分同学的优秀作业

  • 相关阅读:
    笔记本不用fn也能用功能键
    基于51单片机的可调节占空比四种三种波形发生器proteus仿真
    Mysql事务机制
    Linux中常用的性能分析工具
    MediaRecorder实现录音
    故障分析 | Sql_slave_skip_counter 使用不规范对复制的影响
    springboot疫情防控下基于微信小程序的食堂订餐系统毕业设计源码261620
    Flink-源算子Source(获取数据源)的使用
    hello world、hello 计科人
    [JS Framework] 当前运行的基座不包含原生插件[XXX],请在manifest中配置该插件,重新制作包括该原生插件的自定义运行基座
  • 原文地址:https://blog.csdn.net/qq_42749591/article/details/128017658