【上机】《C语言程序设计》第二次上机

第一题:输入实数x,按下列公式计算并输出x和y的值(保留两位小数)


答案:

#include <math.h>
#include <stdio.h>

int main()
{
    float x, y;
    scanf("%f", &x);
    if (x <= 0)
    {
        y = sin(x);
    } 
    else if (x <= 10)
    {
        y = x*x + 1;
    } 
    else
    {
        y= 1.0/(x*x*x + x*x + 1);
    }
    printf("x = %.2f,y = %.2f\n", x, y);
    
    return 0;
}

第二题:根据下表提供的数据,输入班号,输出该班的学生人数(用switch语句)

班级

2007

2008

2009

2010

2011

2012

人数

88

66

88

88

66

100

答案:

#include <stdio.h>
int main()
{
    int classID;//班号
    scanf("%d", &classID);
    switch (classID)
    {
    case 2007:printf("该班学生人数为\n");
        break;
    case 2008:printf("该班学生人数为\n");
        break;
    case 2009:printf("该班学生人数为\n");
        break;
    case 2010:printf("该班学生人数为\n");
        break;
    case 2011:printf("该班学生人数为\n");
        break;
    case 2012:printf("该班学生人数为\n");
        break;
    default:printf("班号输入有误!");

    }
    return 0;
}

或者合并部分语句:

#include <stdio.h>
int main()
{
    int classID;//班号
    scanf("%d", &classID);
    switch (classID)
    {
    case 2007:
    case 2009:
    case 2010:
        printf("该班学生人数为\n");
        break;
    case 2008:
    case 2011:
        printf("该班学生人数为\n");
        break;
    case 2012:printf("该班学生人数为\n");
        break;
    default:printf("班号输入有误!");

    }
    return 0;
}

第三题:输入一串字符(换行作为结束标志),分别统计其中字母数字空格其它字符各自出现的次数并输出。

答案:

#include <stdio.h>
int main()
{
    int letter=0,space=0,digit=0,others=0; //初始化为
    char c;  //接收字符串的变量
    while((c=getchar())!='\n')           
    { 
        if(c==' ')  // 检测到空格
            space++;
        else if(c>='0'&&c<='9') // 检测到数字
            digit++;
        else if((c>='a'&&c<='z')||(c>='A'&&c<='Z')) // 检测到字母,要同时考虑字母的大小写 
            letter++;
        else others++;
    }
    //输出结果
    printf("The number of letters is:%d\n",letter);
    printf("The number of spaces is:%d\n",space);
    printf("The number of digits is:%d\n",digit);
    printf("The number of other words is:%d\n",others);
    return 0;
}

第四题:输出下列数字金字塔

         1

       121

      12321

     1234321

    123454321

   12345654321

  1234567654321

 123456787654321

12345678987654321

答案:

#include<stdio.h>
int main() 
{ 
    int i,j,k=9; 
    for(i=1;i<=k;i++) 
    { 
        for(j=1;j<=k-i;j++)printf(" "); 
        for(j=1;j<=i;j++)printf("%d",j); 
        for(j=i-1;j>0;j--)printf("%d",j); 
        printf("\n"); 
    } 
}

第五题:一百匹马二百块砖驮上山,其中大马每匹驮三块、中马每匹驮两块,而小马两匹驮一块,计算并输出各种马匹数目(非负的整数)

答案:

设有大马X,中马Y,小马Z

X+Y+Z=100        (1)

3X+2Y+Z/2=200     (2)

(2)乘以2减(1)得:

5X+3Y=300

#include <stdio.h>
void main()
{
    int sum=0;
    for(int i=0;i<67;i++)//大马驮200/3
    { 
        for(int j=0;j<=100;j++) //中马驮200/2
        { 
            sum=5*i+3*j;
            if(sum==300)
            { 
                printf("大马=%d,中马=%d,小马=%d\n",i, j, 100-i-j);                
            }
        }
    }
}

 输出:

大马=0,中马=100,小马=0
大马=3,中马=95,小马=2
大马=6,中马=90,小马=4
大马=9,中马=85,小马=6
大马=12,中马=80,小马=8
大马=15,中马=75,小马=10
大马=18,中马=70,小马=12
大马=21,中马=65,小马=14
大马=24,中马=60,小马=16
大马=27,中马=55,小马=18
大马=30,中马=50,小马=20
大马=33,中马=45,小马=22
大马=36,中马=40,小马=24
大马=39,中马=35,小马=26
大马=42,中马=30,小马=28
大马=45,中马=25,小马=30
大马=48,中马=20,小马=32
大马=51,中马=15,小马=34
大马=54,中马=10,小马=36
大马=57,中马=5,小马=38
大马=60,中马=0,小马=40

欢迎纠错建议。不要发表赞,顶等无关信息。

 

原文地址:https://www.cnblogs.com/elesos/p/2767164.html