PTA Chapter 3 Homework Questions

Insert image description here


Preface

Let me explain the homework questions of Chapter 3 of AYIT. First, evaluate these questions. In fact, these questions are very classic and suitable for introductory learning. Some classic methods and conclusions need to be learned.


7-1 Compare size

Ⅰ. Method 1: Direct judgment method


#include<stdio.h>
int main()
{
    
    
    int a,b,c;
    scanf("%d %d %d",&a,&b,&c);
    if(a > b && b > c)
    printf("%d %d %d",c,b,a);
    else if(b > a && a > c)
    printf("%d %d %d",c,a,b);
    else if(c > a && a > b)
    printf("%d %d %d",b,a,c);
    else if(a > c && c > b)
    printf("%d %d %d",b,c,a);
    else if(b > c && c > a)
    printf("%d %d %d",a,c,b);
    else if(c > b && b > a)
    printf("%d %d %d",a,b,c);
    return 0;
}

Ⅱ. Method 2: Exchange method

Just look at the first method, you can learn more about the following methods

Because it goes from small to large, the left side of the if must be bigger and the right side is smaller.
Then the three numbers are judged in all three situations and then the exchange method is used.

#include<stdio.h>
int main()
{
    
    
    int x,y,z,min;
    scanf("%d %d %d",&x,&y,&z);
    if(x > y)
    {
    
    
  	    min = x;
  	    x = y;
  	    y = min;
    }
    if(x > z)
    {
    
    
  	    min = x;
  	    x = z;
  	    z = min;
    }
    if(y > z) 
    {
    
    
  	    min = y;
  	    y = z;
  	    z = min;
  }
    printf("%d %d %d",x,y,z);
    return 0;
}

7-2 Compare the size of two numbers

Ⅰ. Method: direct judgment method

#include<stdio.h>
int main()
{
    
    
    int a,b;
    scanf("%d %d",&a,&b);
    if(a>=b)
    {
    
    
        printf("%d %d",b,a);
    }
    else if(a<b)
    {
    
    
        printf("%d %d",a,b);
    }
    return 0;
}

7-3 Performance Level

Ⅰ. Method: direct judgment method

#include<stdio.h>
int main()
{
    
    
    int n;
    scanf("%d",&n);
    if(n >= 90 && n <= 100)
    printf("A");
    if(n >= 80 && n <= 89)
    printf("B");
    if(n >= 70 && n <= 79)
    printf("C");
    if(n >= 60 && n <= 69)
    printf("D");
    if(n <60 && n >=0)
    printf("E");
    return 0;
}

7-4 Fishing and drying nets

Ⅰ. Method: direct judgment method

#include<stdio.h>
int main()
{
    
    
	int m,n,p;
	int a,y;
	scanf("%d %d %d",&m,&n,&p);
	if(n=1) a=0;//前0个月
	if(n=2) a=31;//前1个月
	if(n=3) a=59;//前2个月
	if(n=4) a=90;//前3个月
	if(n=5) a=120;
	if(n=6) a=151;
	if(n=7) a=181;
	if(n=8) a=212;
	if(n=9) a=243;
	if(n=10) a=273;
	if(n=11) a=304;
	if(n=12) a=334;//算天数用
	a += p;//加上本月的天数
	if((m%4==0&&m%100!=0||m%400==0))//判断闰年
	{
    
    
		a+=2;
	}
	y=a%5;//下面就是依据打鱼晒网的定义进行判断
	if(y==0||y==4)
	{
    
    
		printf("network");
	}
	if(y==1||y==2||y==3)
	{
    
    
		printf("fish");
	}
	return 0;
 } 

7-5 Calculating bonuses

Ⅰ. Method: direct judgment method

#include<stdio.h>
int main()
{
    
    
    double a;
    double m;
    scanf("%lf",&a);
    if(a <= 10.0 && a >= 0.0)
    {
    
    
        m = a*0.1*10000.0;
        printf("%.2lf元",m);
    }
    else if(a>10.0 && a<=20.0)
    {
    
    
        m = a*0.12*10000;
        printf("%.2lf元",m);
    }
    else if(a >20.0 && a<= 40.0) 
    {
    
    
		 m = a*0.14*10000;
        printf("%.2lf元",m);
    }
    else if(a > 40.0 && a <= 60.0)
    {
    
    
        m = a*0.16*10000;
        printf("%.2lf元",m);
    }
    else if(a > 60.0 && a<=100.0)
    {
    
    
        m = a*0.18*10000;
        printf("%.2lf元",m);
    }
    else
    {
    
    
        m=a*0.2*10000;
        printf("%.2lf元",m);
    }
    return 0;
}

7-6 Find solutions to equations

Ⅰ. Method: Brutal calculation

#include<stdio.h>
#include<math.h>
int main()
{
    
    
    int a,b,c;
    double x1,x2,m=0;
    scanf("%d %d %d",&a,&b,&c);
    m = (pow(b,2)-4*a*c);
    x1 = ((-b + sqrt(m))/(2*a));
    x2 = ((-b - sqrt(m))/(2*a));
    if(m < 0 && a != 0)
    {
    
    
        printf("无实数解!\n");
    }
    else if(m > 0 && a != 0)
    {
    
    
        printf("x1=%.2lf,x2=%.2lf\n",x1,x2);
    }
    else if(m == 0 && a != 0)
    {
    
    
        printf("x1=x2=%.2lf\n",x1);
    }
    return 0;
}

7-7 Find the number of chickens and rabbits

Ⅰ. Method: Brutal calculation

#include<stdio.h>
int main()
{
    
    
    int x,y;
    int m,n;
    scanf("%d %d",&x,&y);
    n=0.5*y-x;
    m=2*x-0.5*y;
    if(x<0 || y<0 || x%2!=0 || y%4!=0 || m <0 || n <0)
    printf("error");
    else
    printf("chicken:%d rabbit:%d",m,n);
    return 0;
}

7-8 Mogetsukiku

Ⅰ. Method: Brutal calculation

#include<stdio.h>
#include<math.h>
int main()
{
    
    
	int d,p;
	float r,m;
	scanf("%d%d%f",&d,&p,&r);
	if(p-d*r>0)
	{
    
    
	m=(log(p)-log(p-d*r))/log(1+r);
	printf("%.0f\n",ceil(m));
	}
	else
	{
    
    
		printf("error\n");
	}
	return 0;
}

7-9 Design Calculator

Ⅰ. Method: Brutal calculation

#include<stdio.h>
int main()
{
    
    
    float a,b;
    char ch;
    float m;
    scanf("%f%c%f",&a,&ch,&b);
    switch(ch)
    {
    
    
        case '+': m=a+b; break;
        case '-': m=a-b; break;
        case '*': m=a*b; break;
        case '/': m=a/b; break;
    }
    if(m-(int)m==0.0)
    printf("%.0f",m);
    else
    printf("%.1f",m);
    return 0;
}

7-10 letter encryption

Ⅰ. Method: Brutal calculation (if you don’t understand this question, you can ask 22 seniors)

#include<stdio.h>
int main()
{
    
    
    unsigned char c;
    int k;
    scanf("%c%d",&c,&k);
    c=c+k;
    if(c>'z')
    c=c-26;
    printf("%c",c);
    return 0;
}

7-11 Determine leap year

Ⅰ. Method: memorize conclusions

#include<stdio.h>
int main()
{
    
    
    int year;
    scanf("%d",&year);
    if(year%4==0&&year%100!=0||year%400==0)
    {
    
    
        printf("yes");
    }
    else
    {
    
    
        printf("no");
    }
    return 0;
}

Summarize

The homework in this chapter will be difficult when you first learn it, but as long as you are willing to study hard and ask questions, you will learn a lot. Some conclusions of the topic, such as the judgment of leap years, must be memorized by everyone.

Guess you like

Origin blog.csdn.net/congfen214/article/details/133265888