Chapter 3: Answers to After-Class Exercises


1. Multiple choice questions

1—5 CACBB
6—10 CDBBB
11—15 ADCCB
16—20 DCCBB

Two, fill in the blanks

  1. (a!=b)||(a<=c)
  2. (x>20)&&(x<30)||(x<-100)
  3. 0
  4. 1
  5. 1
  6. if((x%3==0) && (x%7==0) )

Three, programming questions

  1. Enter a positive integer from the keyboard to determine whether it is a multiple of 3 and 5, if it is, enter yes, otherwise output no
#include<stdio.h>
int main()
{
    
    
	int x;
	printf("请输入一个正整数");
	scanf("%d",&x);
	if((x%3==0)&&(x%5==0))
		printf("yes");
	else
		printf("no");
	return 0;
} 
  1. Write a program, enter the length of the three sides of the triangle, and find the area. Note: The sum of any two sides of the triangle must be greater than the third side. For unreasonable side length input, an error message is required.
#include<stdio.h>
#include<math.h> 
int main()
{
    
    
	double a,b,c,p,s;
	printf("输入三角形的3个边长");
	scanf("%lf%lf%lf",&a,&b,&c);
	if((a+b>c)&&(a+c>b)&&(b+c>a)){
    
    
		p=(a+b+c)/2;
		s=sqrt(p*(p-a)*(p-b)*(p-c));
	}
	else{
    
    
		printf("输入边长不合理");
		return 0;
	}
	printf("%0.2lf",s);
	return 0;
} 

  1. Write a program according to the following piecewise function, input an x ​​value, and output the corresponding y value
    Insert picture description here
#include<stdio.h>
int main()
{
    
    
	float x,y;
	printf("请输入x值");
	scanf("%f",&x);
	if((x>-5)&&x<0){
    
    
		y=x-1;
	}
	else if(x==0){
    
    
		y=x;
	}
	else if(x>0&&x<8){
    
    
		y=x+1;
	}
	else{
    
    
		y=10;
	}
	printf("%f",y);
	return 0;
} 

  1. Input a character from the keyboard, if it is a lowercase letter, it will be converted to uppercase and output, if it is a capital letter, it will be converted to lowercase and output, and other characters will be output as they are
#include<stdio.h>
int main()
{
    
    
	char a;
	a=getchar();
	if(a>='a'&&a<='z'){
    
    
		a=a-32;
		putchar(a);
	} 
	else if(a>='A'&&a<='Z'){
    
    
		a=a+32;
		putchar(a); 
	}
	else{
    
    
		putchar(a);
	}
	return 0;
} 
  1. Input 3 integers from the keyboard to the variables a, b, c, and sort these 3 numbers from small to large, a is the smallest number, and c is the largest number
#include<stdio.h>
int main()
{
    
    
	int a,b,c,t=0;
	scanf("%d%d%d",&a,&b,&c);
	if(a>b){
    
    
		t=a;
		a=b;
		b=t;
	}
	if(a>c){
    
    
		t=c;
		c=a;
		a=t;
	}
	if(b>c){
    
    
		t=b;
		b=c;
		c=t;
	}
	printf("a=%d,b=%d,c=%d",a,b,c);
	return 0;
} 

  1. Enter a 3-digit number to determine whether it is a daffodil number, and a prompt will be given when the input data is incorrect. The number of daffodils is a three-digit number, and the sum of the cubes of the digits is equal to the number itself.
#include<stdio.h>
int main()
{
    
    
	int x,g,s,b;
	scanf("%d",&x);
	if(x>999||x<0){
    
    
		printf("输入数据不正确");
		return 0; 
	}
	g=x%10;
	s=x/10%10;
	b=x/100;
	if(x==(g*g*g+s*s*s+b*b*b)){
    
    
		printf("%d是水仙花数",x);
	}
	else{
    
    
		printf("%d不是水仙花数",x);
	}
	return 0;
} 
  1. Enter the values ​​of 3 integers a, b, and c from the keyboard to find the root of the quadratic equation ax*x+bx+c=0 (a!=0), and the calculation result retains two decimal places
#include<stdio.h>
#include<math.h>
int main()
{
    
    
	int a,b,c,s,x1,x2;
	scanf("%d%d%d",&a,&b,&c);
	if(a==0){
    
    
		printf("输入数据错误"); 
	}
	s=b*b-4*a*c;
	if(s>0){
    
    
		x1=((-b)+sqrt(s))/(2*a);
		x2=((-b)-sqrt(s))/(2*a);
		printf("方程有2个解:x1=%d,x2=%d",x1,x2);
	}
	else if(s==0){
    
    
		printf("方程有1个解:x=%d",(-b)/(2*a));
	}
	else{
    
    
		printf("方程无解");
	}
	return 0;
} 
  1. Insert picture description here
#include<stdio.h>
int main()
{
    
    
	int year;
	double sum;
	printf("输入本金与期限");
	scanf("%lf%d",&sum,&year);
	if(year==8){
    
    
		printf("金额:%0.2lf,期限:%d",sum+sum*0.0032,year);
	}
	else if(year==5){
    
    
		printf("金额:%0.2lf,期限:%d",sum+sum*0.00275,year);
	}
	else if(year==3){
    
    
		printf("金额:%0.2lf,期限:%d",sum+sum*0.00245,year);	
	}
	else if(year==2){
    
    
		printf("金额:%0.2lf,期限:%d",sum+sum*0.00230,year);	
	}	
	else if(year==1){
    
    
		printf("金额:%0.2lf,期限:%d",sum+sum*0.00215,year);	
	}
	else{
    
    
		printf("不是上述年限,请重新输入"); 
	}
	return 0;
} 


#include<stdio.h>
int main()
{
    
    
	int year;
	double sum;
	printf("输入本金与期限");
	scanf("%lf%d",&sum,&year);
	switch(year){
    
    
		case 8: sum=sum+sum*0.0032;break;
		case 5: sum=sum+sum*0.00275;break;
		case 3: sum=sum+sum*0.00245;break;
		case 2: sum=sum+sum*0.00230;break;
		case 1: sum=+sum*0.00215;break;
		default :printf("不是上述年限,请重新输入"); return 0; 
	}
		printf("金额:%0.2lf,期限:%d",sum,year);	
		return 0;
} 



If you have any questions, you can leave a message in the comment area

Guess you like

Origin blog.csdn.net/buxiangquaa/article/details/114537291
Recommended