C language programming experiment report 4

C programming experiment report

Name: Zeng Fubo

Experiment location: home

Experiment time: 2020.4.9

Experimental items: 5.3.1 Exercise 2. Find the sum of the first n items in the sequence; 5.3.2 Exercise 2. Daffodil number; 5.3.4 Decimal conversion; 5.3.5 Exercise 1. Hundred horses Baidan problem; 5.3.5 Exercise 2 . Find the arithmetic sequence; 5.3.5 Exercise 3. Food distribution problem;

1. The purpose and requirements of the experiment

1. Proficiency in the method of while, do. While and for statement to achieve loop.

2. Understand the difference and conversion of the three kinds of loop statements, their respective adaptability, and the use of loop nesting.

3. Master how to use break, .continue statements in loop statements to change the program flow.

4. Master various algorithms in the loop of programming.

2. Experimental exercises

5.3.1 Exercise 2. Find the sum of the first n terms in the sequence

1. A brief description of the problem: by writing 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>
#include<conio.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<=2*n-1;i=i+2)
    {
        fac=1.0;
        for(j=1;j<=i;j++)
        fac=fac*j;
        fac=fac*sign;
        sum+=fac;
        sign=-sign;
    }
    printf("sum=%.0f\n",sum);
}

3. Problem analysis: At the beginning of the textbook, only the sum of the first n terms of the arithmetic sequence was written, without factorial consideration. Later, under the teacher's explanation, the textbook program was modified to complete this experimental project.

5.3.2 Exercise 2. Number of daffodils

1. A simple analysis of the problem: find all the daffodils (the number of daffodils is-a 3-digit natural number, and the cubic sum of the digits of the number is equal to the number itself, such as 153 is the number of daffodils 1 3 + 5 3 + 3 ^ 3 = 153).

2. Experimental code:

#include<stdio.h>
main()
{
    int x,y,z;
    int k=100;
    while(k<=999)
    {
        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. Problem analysis: The program does not need to receive keyboard input. The daffodil number is a 3-digit natural number, which should be within 100-999. Since the cube of each number needs to be equal to the number itself, each digit of the number needs to be requested. digital

5.3.4 Decimal conversion

1. A brief description of the problem: Enter 4 character numbers and convert them to decimal integers and display them.

2. Experimental code:

#include<stdio.h>
main()
{
    char c;
    int k,data;
    data=0;
    for(k=0;k<4;k++)
    {
        while(1)
        {
            c=getchar();
            if(c>='0'&&c<='9')
            break;
        }
        if(k==0)
            data+=(c-'0')*1000;
        if(k==1)    
            data+=(c-'0')*100;
        if(k==2)    
            data+=(c-'0')*10;
        if(k==3)
            data+=(c-'0');
    }
    printf("Data=%d",data);
}

3. Problem analysis: None

5.3.5 Exercise 1. The problem

1. A brief description of the problem: there are 100 horses and 100 camel cargo. One large horse can carry three camels, one medium horse can carry two camels, and two ponies can carry one camel. Find out how many combinations of horses and ponies are possible for the curse you need.

2. Experimental code:

#include<stdio.h>
main()
{
    int 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 ( " Malaysia% 3d horses; Medium horse% 3d horses; Pony% 3d horses. \ n " , m, n, k); 
                sum ++ ; 
            } 
        } 
    } 
        printf ( " There are% d piggyback methods. \ n " , sum); 
}

3. Problem analysis: It must be written as k = 100-mn, but not 100 = k + m + n.

5.3.5 Exercise 2. Find the arithmetic sequence

1. A brief description of the problem: there is a sequence of positive integer arithmetic differences. It is known that the sum of the first 4 terms of the sequence is equal to 26, and the product of the first 4 terms is equal to 880. Find the value of the first 6 items of the difference series and the sum of the first 6 items.

2. Experimental code:

#include <stdio.h>
main()
{
int a,b,c,d,i,sum=0;
for(a=1;a<=26;a++)
for(d=1;d<=26;d++)
{
b=4*a+6*d;
c=a*(a+d)*(a+2*d)*(a+3*d);
if((b==26)&&(c==880))
{
printf("数列的初值为a=%d,差值为d=%d\n",a,d);
printf("\ n The value of the first 6 items in the sequence: " );
 for (i = 0 ; i < 6 ; i ++ ) 
{ 
printf ( " % d " , a + i * d); 
sum = sum + (a + i * d); 
} 
printf ( " \ n " ); 
} 
} 
printf ( " \ n Sum of the first 6 items of the sequence:% d \ n " , sum); 
}

3. Problem analysis: b is summation and c is quadrature. It is necessary to understand how the sum and product are obtained, and nest the if statement in the for statement for branch selection.

5.3.5 Exercise 3. Food distribution issues

1. A brief description of the problem; there are 30 students buying snacks together for a total of 50 yuan. Among them, each university student spends 3 yuan, each middle school student spends 2 yuan, each elementary school student spends 1 yuan. Find out how many different combinations there are for the distribution of the number of students in large, middle, and elementary schools (do not calculate the combination of 0 for a certain class of students).

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 ( " There are% d different combinations. \ n " , sum); 
}

3. Problem analysis: The main problem in writing and modifying the program is that the relationship between x and y has not been figured out before, resulting in an infinite number of results.

Three, nine and nine multiplication table

2. Experimental code:

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

        printf("\n");
    }

    return 0;
}

3. Problem analysis: At the beginning, I had no clue about the code of the Jiujiu multiplication table. I did n’t know where to start. I had to search the relevant code on the Internet. I chose this relatively simple completion. The knowledge about C language is still not very familiar. It is impossible to effectively select the learned knowledge to solve the problem, and the foundation is weak.

Fourth, the experimental summary

After this experiment, I understood where I was lacking, such as the judgment of conditions, etc. At the same time, there were errors in the use of some symbols.

Guess you like

Origin www.cnblogs.com/aa842659/p/12697288.html