[Take you through the questions by hand]-Introduction to C language programming (3)

Hello everyone, I am deep fish~

Table of contents

         Foreword:

1. Calculate the perimeter and area of ​​a triangle

2. Calculate the volume of the sphere

3. Score input and output

4. Mutant Daffodil

 5. Kiki counts

6. Single digit of floating point number

7. How many seconds can you live

 8. Time conversion

 9. Calculation of total score and average score

10. KiKi and Yogurt

Summarize:

Conclusion:


Foreword:

Today is the third day of updating the questions. For beginners, the questions are getting more and more difficult. I hope you can persevere. After completing these 130 questions, we will definitely make great progress. Let's go! ! !

 

1. Calculate the perimeter and area of ​​a triangle

Topic link: Calculating the perimeter and area of ​​a triangle

topic:

 Solution: This question only needs to learn two points:

<1> Know how to calculate the area of ​​a triangle by knowing the three sides of a triangle: Helen's formula S=sqrt(p*(pa)*(pb)*(pc)), where p is half of the perimeter of the triangle, that is, p=(a+ b+c)/2

<2> The function sqrt means the square root, it acts on variables of double type , use this function to lead the file #include<math.h>

Expansion: How to calculate the power in C language, here is a function: pow (double x, double y), its return value is also double type , eg: pow (2.0, 6.0) = 64, which is also in mathematics , so before using this function, you need to lead the file #include<math.h>

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

int main()
{
    double a=0.0;
    double b=0.0;
    double c=0.0;
    //输入
    scanf("%lf %lf %lf",&a,&b,&c);
    //计算
    double circumference=(a+b+c);//周长
    double p=circumference/2;
    double area=sqrt(p*(p-a)*(p-b)*(p-c));
    //输出
    printf("circumference=%.2lf area=%.2lf",circumference,area);
    return 0;
}

2. Calculate the volume of the sphere

Topic link: Calculating the volume of a sphere topic

topic:

 Solution: This question should pay attention to:

<1> When defining pi, it cannot be written as a float type, because the float type can only reach 6-7 digits after the decimal point , while the double type can reach 15-16 digits after the decimal point

<2> When calculating the volume, it cannot be written as 4/3, so that the results of the calculation are 1, then the real result of this question will be very different. The solution is to write it as 4.0 /3 or 4/3.0 , or write /3 Write it after the whole expression eg:4*pi*n*n*n/3

#include <stdio.h>
int main() 
{
    double r=0.0;
    double v=0.0;
    double pi=3.1415926;
    //输入
    scanf("%lf",&r);
    //计算
    v=4/3.0*pi*r*r*r;
    printf("%.3lf",v);
    return 0;
}

3. Score input and output

Topic link: grade input and output

topic:

 Solution 1:

#include <stdio.h>

int main()
 {
    int s1=0;
    int s2=0;
    int s3=0;

   scanf("%d %d %d",&s1,&s2,&s3);

   printf("score1=%d,score2=%d,score3=%d\n",s1,s2,s3);
   
    return 0;
}

Solution 2: If there is a lot of input data, use array input

#include <stdio.h>

int main()
 {
    int score[3]={0};
    int i=0;

   for(i=0;i<3;i++)
   {
    scanf("%d",&score[i]);
   }

   printf("score1=%d,score2=%d,score3=%d\n",score[0],score[1],score[2]);

    return 0;
}

4. Mutant Daffodil

Topic Link: Variant Narcissus

topic:

answer:

Take 12345 as an example, how to take out each number

1*2345       -     (12345/10000 )*(12345%10000)

12*346       -      (12345/1000)  *(12345%1000)

123*45       -        (12345/100)  * (12345%100)

1234*5       -         (12345/10)  *  (12345%10)

So it is a number i, the product of (i divided by the power of 10) and (i takes the modulo of the power of 10)

                                                    (i/j)*(i%j)

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

int main()
{
    int i=0;
    for (i = 10000; i <= 99999; i++) //取出五位数
    {
        //判断i是否为Lily Number
        int j=0;
        int sum=0;
        for(j=10;j<=10000;j*=10)
        {
            sum+=(i/j)*(i%j);
        }
        if (sum == i)
            printf("%d ", i);
    }
    return 0;
}

 5. Kiki counts

Topic Link: Kiki Counts

topic:

 Solution to the question: This question is mainly about taking the remainder

#include <stdio.h>

int main() 
{
   int a=0;
   int b=0;
   //输入
   scanf("%d %d",&a,&b);
   //计算
   int sum =(a+b)%100;//这里也可以写成a%100+b%100
   //输出
   printf("%d",sum);

    return 0;
}

6. Single digit of floating point number

Topic link: single digit of floating point number

topic:

 Solution 1: This question mainly learns (int) (double) forced conversion type , what type to convert is directly written in front of the variable or constant (the type you want to convert)

#include <stdio.h>

int main() 
{
    double a=0;
    scanf("%lf",&a);
    int  n=(int)a;
    printf("%d\n",n%10);
    
    return 0;
}

Solution 2: 13.141 when inputting directly, but the program input only takes the integer part

#include <stdio.h>

int main() 
{
    int a=0;
    scanf("%d",&a);
    printf("%d\n",a%10);
    
    return 0;
}

7. How many seconds can you live

Topic link: How many seconds can you live

topic:

Solution: How to write 10 to the 7th power for this question?

<1>3.156*pow(10,7)

<2>3.156 e7 (e represents the representation of scientific notation with base 10)

Also note that 631200000 is too large to use int type, it must be stored in long long type, and it is %lld when printing

#include <stdio.h>
#include<math.h>
int main() 
{
   int age=0;
   //输入
   scanf("%d",&age);
   //计算
   long long time=3.156*pow(10,7)*age;
   //或者这样写long long time=3.156e7*age;
   //输出
   printf("%lld",time);
    return 0;
}

 8. Time conversion

Topic Link: Time Conversion

topic:

Solution: This question is a question about net worth conversion. 3600s is equal to 1h, and seconds%3600 is the remainder after dividing 3600. Divide this remainder by 60 to get minutes, and take the remainder to get seconds

#include <stdio.h>

int main()
{
	//输入
	int seconds=0;
	int h=0;
	int m=0;
	int s=0;
	scanf("%d",&seconds);
	//计算
	h=seconds/3600;
	m=seconds%3600/60;
	s=seconds%3600%60;
	//输出
	printf("%d %d %d\n",h,m,s);

	return 0;
}

 9. Calculation of total score and average score

Topic Link: Total Grade and Average Score Calculation

topic:

Solution 1:

#include <stdio.h>

int main()
{
	double arr[3] = {0};//三科成绩
	//输入
	scanf("%lf %lf %lf", &arr[0], &arr[1], &arr[2]);
    //计算
    double sum=arr[0] + arr[1] + arr[2];
	double avg=sum/3.0;
	//输出
	printf("%.2lf %.2lf",sum , avg);
	return 0;
}

 Solution 2: Calculate while inputting

#include <stdio.h>

int main()
{
	double score=0.0;
	double sum=0.0;
	//输入并计算
	int i=0;
	for(i=0;i<3;i++)
	{
       scanf("%lf",&score);
	   sum+=score;
	}
	//输出
	printf("%.2lf %.2lf",sum ,sum/3);
	return 0;
}

10. KiKi and Yogurt

Topic Link: KiKi and Yogurt

topic:

Solution 1: When the total time is exactly divisible by the time spent drinking a bottle, just subtract it directly. If it is not divisible, you need to subtract another bottle

#include <stdio.h>
int main()
{
    int m = 0;//给的酸奶瓶数
    int n = 0;//喝一瓶所用的时间
    int h = 0;//总共时间

    while ((scanf("%d %d %d", &n, &h, &m) != EOF))
    {
        if (m % h)
            printf("%d", n - m / h - 1);
        else
            printf("%d", n - m / h);
    }
    return 0;
}​​​​

 Solution 2: Simplify (int) cast

#include <stdio.h>

int main()
{
	float m, n, h = 0;
	scanf("%f %f %f", &n, &h, &m);
	printf("%d", (int)(n - m / h) );
	return 0;
}

Summarize:

(1) The training ideas of these programming beginners are generally: input, calculation, output

(2) The function and return value of sqrt and pow are double type, and the header file must be #include<math.h>

  (3) The float type can only reach 6-7 digits after the decimal point. Remember to bring the decimal point when calculating the fraction, otherwise it will be directly rounded eg: 4/3.0

  (4) Narcissus variant: (i/j)*(i%j) j is pow(10,n)

  (5) There are two methods of power calculation: <1>pow(x,y) <2>en (scientific notation with base 10)

  (6) Learn to use mandatory conversion (type) flexibly

  (7) Learn to use the remainder flexibly (modulo)

Conclusion: There is still glory in the other side, and the youth is not afraid of the long years

 

I feel that the author's writing is not bad, or when I have gained a little, I would like to trouble you to move your little hands and give me a one-click three-link, thank you very much

Guess you like

Origin blog.csdn.net/qq_73017178/article/details/131703066