C language programming experiment (4)

C programming experiment report
Experiment items: Experiment 5.3.1 Exercise 2, Experiment 5.3.2 Exercise 2, Experiment 5.3.4, Experiment 5.3.5 Exercise 1 & Exercise 2 & Exercise 3, Jiujiu Multiplication Table
Name: Long Jiacheng Experiment Location: Home experiment time: April 9, 2020

1. The purpose and requirements of the experiment

1. Master the method of while, do_while and for statements to realize the loop
2. Understand the difference and conversion of the three kinds of loop statements, their respective adaptability, the use of loop nesting
3. Master how to use break, continue statements in loop statements Change the program flow
4. Master the method of using loops in program design to implement various algorithms

2. Experimental content

1. Experimental exercises: Experiment 5.3.1 Exercise 2

1 A brief description of the problem: write a program to find the sequence 1, -3!, 5!,-7!, ..., (-1) ^ n-1 (2n-1)! The sum of the first n items. The value of n is entered by the keyboard.
2 Experimental code:

#include<stdio.h>
main()
{
    int n,i,j,sign=1;
    float fac,sum;
    printf("Please input value of n:");
    scanf("%d",&n);
    sum=0.0;
    for(i=1;i<=n;i=i+1)
    {
	    fac=1.0;
	    for(j=1;j<=2*i-1;j=j+1)
	    {
		    fac=fac*j; 
	    }
	    fac=fac*sign;//计算第n项 
	    sum=sum+fac;//求前n项和 
	    sign=-sign;
    }
    printf("sum=%.0f\n",sum);
}

3. Flow chart:

3 Problem analysis: This code uses more nesting of for loops, what needs to be understood is the role of each loop and the relationship between loops.

2. Experimental exercises: Experiment 5.3.2 Exercise 2

1. A brief description of the problem: Find all the daffodil numbers (the daffodil number is a 3-digit natural number, and the cubic sum of the digits of the number is equal to the number itself)
2. Experimental code:

#include<stdio.h>
main()
{
    int x,y,z;
    int k=100;
    while(k<=999)//while循环条件,水仙花数是一个三位数
    {
	    x=k/100;
	    y=(k/10)%10;
	    z=k%10;
	    if(x*x*x+y*y*y+z*z*z==k)//水仙花数应当满足的条件
	    printf("%d\n",k);
	    k++; 
    }
}

3. Flow chart:

4. Problem analysis: This question was explained before the class, and there were not many problems in the process of entering the code.

3. Experimental exercises: Experiment 5.3.4

1. Brief description of the problem: Enter 4 character numbers and convert them to decimal integers.
2. Experimental code:

3. Flow chart:

4. Problem analysis: Due to carelessness when entering the code, the ";" was added after the for statement, which caused it to fail to run.

4. Experimental exercises: Experiment 5.3.5 Exercise 1

1. A brief description of the problem: there are 100 horses and you have to carry 100 loads. Wherein one pack 3 can be supported Malaysian goods, a pack 2 can be supported in the cargo horse, pony two pack 1 can carry goods, seeking desired Malaysia, Malaysia, the number of combinations can pony
2, Experiment Code:

#include<stdio.h>
main()
{
    int m,n,k;//m,n,k分别代表大马、中马和小马的匹数
    int sum=0; 
    printf("各种驮法如下:\n");
    for (m=1;m<=100;m++)
        for(n=1;n<=100-m;n++)
        {
    	    k=100-m-n;
    	    if((k%2==0)&&(3*m+2*n+0.5*k==100))
    	    {
    		    printf("大马%3d匹;中马%3d匹;小马%3d匹.\n",m,n,k);
    	        sum++;
		    }
	    }
    printf("共有%d种驮法.\n",sum);
 } 

3. Flow chart:

4. Problem analysis: When entering the code, enter "==" as "="

5. Experimental exercises: Experiment 5.3.5 Exercise 2

1. A brief description of the problem: In a positive integer arithmetic sequence, the sum of the first 4 terms of the sequence is known to be equal to 26, and the product of the first 4 terms is equal to 880. Please write a program to find the value of the first 6 terms of the differential sequence and The sum of the first 6 items in the sequence.
2. Experimental code:

3. Flow chart:

4. Problem analysis: The position of b and d was wrong when entering the code, resulting in an error in the calculation result

6. Experimental exercises: Experiment 5.3.5 Exercise 3

1. A brief description of the problem: 30 students went to buy snacks together and spent a total of 50 yuan. Among them, each university student spent 3 yuan, each middle school student spent 2 yuan, and each elementary student spent 1 yuan. How many different combinations are there in the number of people allocated (does not calculate the combination of a certain number of students is 0)
2. Experimental code:

#include<stdio.h>
main()
{
    int x,y,z,sum;
    sum=0;
    for(x=1;x<30;x++)
    {
	    for(y=1;y<30;y++)
	    {
		    z=30-x-y;
		    if((z!=0)&&(3*x+2*y+z==50))
		    {
			    printf("大学生%3d\t中学生%3d\t小学生%3d\n",x,y,z);
			    sum=sum+1;
		    }
		    else
		       continue;
	    }
    }
    printf("sum=%d\n",sum);
}

3. Flow chart:

4. Problem analysis: Pay attention to each nested pairing problem in the process of entering the code, which is easy to cause confusion.

Experiment 3: Project training: Nine-nine-nine multiplication table

1. Flow chart:

2. Code:

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

Fourth, the experimental summary (analysis of personal gains and losses)

This experiment found that sometimes it will naturally add ";" after the for statement, resulting in an error in the running result; sometimes it will cause some letters to be input incorrectly due to carelessness; also found some in the experiment that I don't understand The problem was solved by discussing with classmates or checking information on the Internet.

Guess you like

Origin www.cnblogs.com/ljc-0819/p/12682878.html