C语言作业解#

1

给出一百分制成绩,要求输出成绩等级’A’、‘B’、‘C’、‘D’、‘E’。90分以上为’A’,8089分为’B’,7079分为’C’,60~69分为’D’,60分以下为’E’。

正确答案
#include <stdio.h>
int main()
  { float score;
    char grade;
    printf("请输入学生成绩:");
    scanf("%f",&score);
    while (score>100||score<0)
{printf("\n 输入有误,请重输");
scanf("%f",&score);
}
    switch((int)(score/10))
   {case 10:
case 9: grade='A';break;
case 8: grade='B';break;
case 7: grade='C';break;
case 6: grade='D';break;
case 5:
case 4:
case 3:
case 2:
case 1:
case 0: grade='E';
}
    printf("成绩是 %5.1f,相应的等级是%c\n ",score,grade);
return 0;
}
我的答案
#include<stdio.h>
int main()
{
    int chengji=0,grade;
 printf("输入成绩:");

while(chengji>100||chengji<0)
{
    scanf("%d", &chengji);

}
    grade = chengji / 10;
 switch (grade)
 {
 case 10:
 case 9:printf("A"); break;
 case 8: printf("B"); break;
 case 7:printf("C"); break;
 case 6:printf("D"); break;
 default:printf("E");
  break;
 }
 return 0;

}

2

给一个不多于5位的正整数,要求:
1)求出它是几位数;2)分别输出每一位数字;3)按逆序输出各位数字,例如原数为321,应输出123。

正确答案
#include <stdio.h>
#include <math.h>
int main()
{
  int num,indiv,ten,hundred,thousand,ten_thousand,place;      //分别代表个位,十位,百位,千位,万位和位数 
  printf("请输入一个整数(0-99999):");
  scanf("%d",&num);
  if (num>9999)
       place=5;
  else  if (num>999)
       place=4;
  else  if (num>99)
       place=3;
  else  if (num>9)
       place=2;
  else place=1;
  printf("位数:%d\n",place);
  printf("每位数字为:");
  ten_thousand=num/10000;
  thousand=(num-ten_thousand*10000)/1000;
  hundred=(num-ten_thousand*10000-thousand*1000)/100;
  ten=(num-ten_thousand*10000-thousand*1000-hundred*100)/10;
  indiv=num%10;
  switch(place)
    {case 5:printf("%d,%d,%d,%d,%d",ten_thousand,thousand,hundred,ten,indiv);
    printf("\n反序数字为:");
    printf("%d%d%d%d%d\n",indiv,ten,hundred,thousand,ten_thousand);
    break;
     case 4:printf("%d,%d,%d,%d",thousand,hundred,ten,indiv);
    printf("\n反序数字为:");
    printf("%d%d%d%d\n",indiv,ten,hundred,thousand);
    break;
     case 3:printf("%d,%d,%d",hundred,ten,indiv);
    printf("\n反序数字为:");
    printf("%d%d%d\n",indiv,ten,hundred);
    break;
     case 2:printf("%d,%d",ten,indiv);
    printf("\n反序数字为:");
    printf("%d%d\n",indiv,ten);
    break;
     case 1:printf("%d",indiv);
    printf("\n反序数字为:");
    printf("%d\n",indiv);
    break;
      }
  return 0;
 }
我的答案
# include <stdio.h>
int  main()
{
 int i, x, b, num=0;
 scanf("%d", &x);

if(x>0&&x<100000)
{
 for (i = 0;x>0; i++)
 {  
  b = x % 10;
  num = num * 10 + b;
  x = x / 10;
 }

 printf("%d位数\n逆序输出%d", i,num);
 }
 return 0;
}

3

输入两个正整数m和n,求其最大公约数和最小公倍数。

正确答案
#include <stdio.h>
int main()
 {
  int  p,r,n,m,temp;
  printf("请输入两个正整数n,m:");
  scanf("%d%d",&n,&m);
  if (n<m)
   {
    temp=n;
    n=m;
    m=temp;
   }
  p=n*m;
  while(m!=0)
   {
    r=n%m;
    n=m;
    m=r;
   }
  printf("它们的最大公约数为:%d\n",n);
  printf("它们的最小公倍数为:%d\n",p/n);
  return 0;
 }
我的答案
# include <stdio.h>

int  main(void)
{
 int x, y, num1, num2, temp;

 printf("请输入两个正整数:\n");
 scanf("%d %d", &num1, &num2);
 if (num1 < num2)
 {
  temp = num1;
  num1 = num2;
  num2 = temp;
 }
 x = num1;
 y = num2;
 while (y != 0)
 {
  temp =x % y;
  x = y;
  y = temp;
 }
 printf("它们的最大公约数为:%d\n", x);
 printf("它们的最小公倍数为:%d\n", num1 * num2 / x);

 return 0;
}

4

输入一行字符,分别统计出其中英文字母,空格,数字和其它字符的个数。

正确答案
#include <stdio.h>
int main()
 {
  char c;
  int letters=0,space=0,digit=0,other=0;
  printf("请输入一行字符:\n");
  while((c=getchar())!='\n')
   {
     if (c>='a' && c<='z' || c>='A' && c<='Z')
    letters++;
     else if (c==' ')
    space++;
     else if (c>='0' && c<='9')
    digit++;
     else
    other++;
    }
   printf("字母数:%d\n空格数:%d\n数字数:%d\n其它字符数:%d\n",letters,space,digit,other);
   return 0;
  }
我的答案
# include <stdio.h>

int  main()
{
 char S;
 int num =0, ch =0, space = 0,other = 0;
 for(;(S = getchar())!='\n';)
 {
 if(S>='0'&&S<='9')
 {
  num++;
 }
else if (S >= 'A' && S <= 'Z'|| S >= 'a' && S <= 'z')
 {
  ch++;
 }
else if (S == ' ')
 {
 space++;
 }
 else other++;
 }
 printf("数字个数:%d\n英文字符:%d\n空格个数:%d\n其它字符个数:%d\n", num, ch, space, other);
 return 0;
}

5

一个球从100米高度自由落下,每次落地后反跳回原高度的一半,再落下,再反弹。求它在第10次落地时,共经过多少米,第10次反弹多高。

正确答案
#include <stdio.h> 
int main()
 {
  double sn=100,hn=sn/2;
  int n;
  for (n=2;n<=10;n++)
   {
   sn=sn+2*hn;   /*第n次落地时共经过的米数*/
   hn=hn/2;      /*第n次反跳高度*/
   }
   printf("第10次落地时共经过%f米\n",sn);
   printf("第10次反弹%f米\n",hn);
   return 0;
   }
我的答案
        # include <stdio.h>
        int  main()
        {
         float i, h, s;
         for (i = 1, h = 100.0,s=0; i <= 10; i++)
         {
          s = s + h/2+h;
          h = 0.5*h;
         }
         s = s - h;
         printf("共经过%fm\n第10次反弹%fm\n", s, h);
        }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值