c语言程序设计精髓 第6周练兵题

1绘制金字塔(4分)

题目内容:

要求用户从键盘输入一个大写字母,使用嵌套循环产生像下面这样的金字塔图案:

A

ABA

ABCBA

ABCDCBA

程序运行结果示例1:

Please input a capital:

D↙

____A

___ABA

__ABCBA

_ABCDCBA

程序运行结果示例2:

Please input a capital:

F↙

______A

_____ABA

____ABCBA

___ABCDCBA

__ABCDEDCBA

_ABCDEFEDCBA

(说明:上面运行结果示例中,每行字母前面的下划线"_"代表屏幕上实际输出的是空格,最后一行前面有一个空格,倒数第二行有两个空格,以此类推。)

输入提示信息:“Please input a capital:\n”

输入格式: “%c”

输出格式:"%c"

#include <stdio.h>
#include <stdlib.h>
#include <math.h>
  int main()
  {
      char c;
      int line;
      printf("Please input a capital:\n");
      scanf(" %c",&c);
      line=c-'A'+1;
      for(int i=0;i<line;i++){
         for(int j=0;j<line-i;j++){
            printf("%c",'_');
         }
         for(int j='A';j<='A'+i;j++){
            printf("%c",j);
         }
         for(int j='A'+i-1;j>'A'-1;j--){
            printf("%c",j);
         }
       if(i != line-1)
        printf("%c",'\n');
      }
    return 0;
  }

2循环嵌套的应用(4分)

题目内容:

编写程序产生如下输出:

F

FE

FED

FEDC

FEDCB

FEDCBA

输入格式: 无

输出格式:"%c"

#include <stdio.h>
#include <stdlib.h>
#include <math.h>
  int main()
  {
    char c='F';
    for(int i='F';i>='A';i--){
        for(int j='F';j>=i;j--){
            printf("%c",j);
        }
        printf("\n");
    }
    return 0;
  }

3利用泰勒级数计算sinx的值(4分)

在这里插入图片描述

#include <stdio.h>
#include <stdlib.h>
#include <math.h>
  double getItem(int n,double x);
  int main()
  {
      double x,sum=0,item,absItem,ss;
      printf("Input x:\n");
      scanf("%lf",&x);
      int n = 0;
      do{

        item=getItem(n,x);
        sum=sum+item;
        n++;
        if(item<0){
           absItem=- item;
        }else{
            absItem= item;
        }


      }while(absItem - 0.00001>0);


     printf("sin(x)=%.3f,count=%d\n",sum,n);

    return 0;
  }

  double getItem(int n,double x){
   //符号判断
   int flag = n%2==1?-1:1;
   double fenzi=1;
   double fenmu=1;
   //确定分母分子
   int s = 2*n+1;
   for(int i=1;i<=s;i++){
    fenmu*=x;
    fenzi*=i;
   }
   return flag*fenmu/fenzi;
  }

4计算100~200之间的所有素数之和(4分)

题目内容:

计算100~200之间的所有素数之和,判别一个数是否是素数请用给定的函数实现。

函数原型:int fun(int m);

说明:

参 数:m 是要进行判断的数;

返回值:若数 m 是素数,则返回值为1;否则返回值为0。

输入格式: 无

输出格式: “sum=%d\n”

#include <stdio.h>
#include <stdlib.h>
#include <math.h>
  int fun(int m);
  int main()
  {
     int sum = 0;
    for(int i=100;i<200;i++){
        if(fun(i)){
            sum+=i;
        }
    }
    printf( "sum=%d\n",sum);

    return 0;
  }
  int fun(int m){

    for(int i = 2; i<=sqrt(m);i++){
        if(m%i==0) return 0;
    }
    return 1;
  }

5编程实现一个输入指定范围内的整数的函数(4分)

题目内容:

编程实现一个输入指定范围内的整数的函数getint,其完整的函数原型为:int getint(int min, int max);,它负责接收用户的输入进行验证,保证接收的一定是一个介于min和max之间([min, max]区间内)的一个整数并最后返回该整数。如果用户输入不合法,则会提示继续输入,直到输入合法时为止。要求编写完整的程序并测试你所写的getint函数。

程序的运行结果示例:

Please enter min,max:

3,100↙

Please enter an integer [3…100]:

-2↙

Please enter an integer [3…100]:

0↙

Please enter an integer [3…100]:

116↙

Please enter an integer [3…100]:

58↙

The integer you have entered is:58

输入提示信息:“Please enter min,max:\n”

           "Please enter an integer [%d..%d]:\n"

输入格式:

输入数据区间的最小值和最大值:"%d,%d"

输入指定范围内的整数: “%d”

输出格式:“The integer you have entered is:%d\n”

#include <stdio.h>
#include <stdlib.h>
#include <math.h>
  int fun(int m);
  int main()
  {
    int max,min,indig,ret;
    printf("Please enter min,max:\n");
    scanf("%d,%d",&min,&max);

    do{
        fflush(stdin);
        printf("Please enter an integer [%d..%d]:\n",min,max);
        ret = scanf("%d",&indig);
    }while(ret != 1 || indig < min || indig> max);

    printf("The integer you have entered is:%d\n",indig);

    return 0;
  }

6程序改错v2.0(5分)

题目内容:

下面代码的功能是将百分制成绩转换为5分制成绩,具体功能是:如果用户输入的是非法字符或者不在合理区间内的数据(例如输入的是a,或者102,或-45等),则程序输出 Input error!,并允许用户重新输入,直到输入合法数据为止,并将其转换为5分制输出。目前程序存在错误,请将其修改正确。并按照下面给出的运行示例检查程序。

#include<stdio.h>
   int main()
   {
       int score;
       char grade;
       printf("Please input score:");
       scanf("%d", &score);
       if (score < 0 || score > 100)   
             printf("Input error!\n");
        else if (score >= 90) 
             grade = 'A’;
        else if (score >= 80)
             grade = 'B';   
        else if (score >= 70)
             grade = 'C';  
        else if (score >= 60)
             grade = 'D'; 
        else
             grade = 'E'; 
        printf("grade:%c\n", grade);
        return 0;
}

程序运行结果示例1:

Please input score:

a↙

Input error!

Please input score:

-12↙

Input error!

Please input score:

230↙

Input error!

Please input score:

92↙

grade: A

程序运行结果示例2:

Please input score:

88↙

grade: B

程序运行结果示例3:

Please input score:

73↙

grade: C

程序运行结果示例4:

Please input score:

65↙

grade: D

程序运行结果示例5:

Please input score:

27↙

grade: E

输入提示信息:“Please input score:\n”

输入格式: “%d”

输出格式:

输入错误时的提示信息:“Input error!\n”

输出格式:“grade: %c\n” (注意:%c前面有一个空格)

#include <stdio.h>
#include <stdlib.h>
#include <math.h>
   int main()
   {
       int score,ret;
       char grade;
       do{
        printf("Please input score:\n");
        ret = scanf("%d", &score);
        if(ret != 1){
            printf("Input error!\n");
            fflush(stdin);
        }
       }while(ret != 1 || score < 0 || score > 100);

        if (score >= 90)
             grade = 'A';
        else if (score >= 80)
             grade = 'B';
        else if (score >= 70)
             grade = 'C';
        else if (score >= 60)
             grade = 'D';
        else
             grade = 'E';
        printf("grade: %c\n", grade);
        return 0;
}

7编程计算a+aa+aaa+…+aa…a(n个a)的值(4分)

题目内容:

编程计算 a+aa+aaa+…+aa…a(n个a)的值,n和a的值由键盘输入。例如,当n=4,a=2,表示计算2+22+222+2222的值。

程序运行结果示例:

Input a,n:

2,4↙

sum=2468

输入提示信息:“Input a,n:\n”

输入格式: “%d,%d”(先输入a,后输入n)

输出格式: “sum=%ld\n”

#include <stdio.h>
#include <stdlib.h>
#include <math.h>
   int main()
   {
       int a,b;
       long sum=0,item=0,c=1;
        printf("Input a,n:\n");
        scanf("%d,%d", &a,&b);
        for(int i=0;i<b;i++){
            item= a * c +item;
            sum+=item;
            c=c *10;
        }
        printf("sum=%ld\n", (long)sum);
        return 0;
}

8搬砖问题(4分)

题目内容:

n块砖( 27<n<=77 ),36人搬,男搬4,女搬3,两个小孩抬一块砖,要求一次搬完,问男人、女人和小孩各需多少人?请用穷举法编程求解,n的值要求从键盘输入。输出结果按照男人数量升序给出(见下面示例3)。

程序的运行结果示例1:

Input n(27<n<=77):

28↙

men=0,women=4,children=32

程序的运行结果示例2:

Input n(27<n<=77):

36↙

men=3,women=3,children=30

程序的运行结果示例3:

Input n(27<n<=77):

60↙

men=2,women=14,children=20

men=7,women=7,children=22

men=12,women=0,children=24

输入提示: “Input n(27<n<=77):\n”

输入格式: “%d”

输出格式:“men=%d,women=%d,children=%d\n”

#include <stdio.h>
#include <stdlib.h>
#include <math.h>
   int main()
   {
       int a,d;
       long sum=0,item=0,c=1;

        printf("Input n(27<n<=77):\n");
        scanf("%d", &d);
        for(int i=0;i<=17;i++){
           for(int j=0;j<=36;j++){
               a = 36-i-j;
               if(4*i+0.5*j+3*a==d && a>=0){
                printf("men=%d,women=%d,children=%d\n", i,a,j);
               }
           }
        }
        return 0;
}

9编程输出某年某月有多少天(考虑到闰年)(5分)

题目内容:

从键盘输入一个年份和月份,输出该月有多少天(考虑闰年),用switch语句编程。

程序运行结果示例1:

Input year,month:

2015,3↙

31 days

程序运行结果示例2:

Input year,month:

2015,4↙

30 days

程序运行结果示例3:

Input year,month:

2016,2↙

29 days

程序运行结果示例4:

Input year,month:

2014,2↙

28 days

程序运行结果示例5:

Input year,month:

2015,13↙

Input error!

输入提示信息:“Input year,month:\n”

输入格式: “%d,%d”

输出格式:

输入错误时的提示信息:“Input error!\n”

输出格式:

      "31 days\n"

      "30 days\n"

      "29 days\n"

      "28 days\n"
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
   int main()
   {
       int year,month,flag,days;
       int a[][12] = {{31,28,31,30,31,30,31,31,30,31,30,31},{31,29,31,30,31,30,31,31,30,31,30,31}};

        printf("Input year,month:\n");
        scanf("%d,%d", &year,&month);

        if(month<1 ||month>12){
          printf("Input error!\n");
        }

        if((year%4==0 && year%100 !=0) || year % 400 ==0){
            days= a[1][month-1];
        }else{
            days = a[0][month-1];
        }
        if(days == 28){
           printf("28 days\n");
        }
        if(days == 29){
           printf("29 days\n");
        }
        if(days == 30){
           printf("30 days\n");
        }
        if(days == 31){
           printf("31 days\n");
        }
        return 0;
}
  • 5
    点赞
  • 17
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
C语言程序设计精髓MOOC》第三主要内容是关于指针和数组的学习。 首先是指针的介绍和使用。指针是C语言中一个非常重要的概念,它可以用来间接访问内存中的数据,通过指针可以实现对变量地址的操作。在学习过程中,我们了解了指针的定义和声明,以及指针与数组之间的关系。指针在程序设计中的应用非常广泛,特别是在动态内存分配和函数调用等方面,有着重要的作用。 其次是数组的使用。数组是一种由相同类型的元素组成的集合,它在C语言中非常常用。在第三的学习中,我们了解了数组的定义、初始化和遍历等基本操作,还学习了一些在数组中常用的算法和技巧。通过多维数组和指针数组的学习,我们可以更灵活地处理多个数据。 除了指针和数组,第三还涉及到了C语言中的结构体(struct)和文件的输入输出操作等内容。结构体是一种可以封装多个不同类型的数据的自定义数据类型,它在实际的程序设计中经常被用于组织和管理数据。文件的输入输出操作涉及了C语言中如何读写文件以及相关的文件处理函数等知识点。 通过学习《C语言程序设计精髓MOOC》第三的内容,我们对指针和数组有了更深入的认识,并且掌握了它们的基本用法和应用技巧。这对于进一步学习和理解C语言程序设计以及其他高级编程语言都非常有帮助。此外,通过作业和练习的完成,我们可以检验和巩固所学的知识,提高我们自己的编程能力。希望通过这门课程的学习,能够让我们对C语言有更全面和深入的了解,为以后的学习和工作打下坚实的基础。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值