C language experiment report (4)

C programming experiment report

Name: Yang Jing Experiment Location: Home Experiment Time: 2020.04.10

 

 experimental project

1. Use a for statement to implement a loop

2. Use the while loop statement to realize the loop

3. Use the do-while statement to implement the loop

4. Use while statement and for statement to realize the loop

5. Use a for statement to nest the loop

1. The purpose and requirements of the experiment

1. Proficiently master the method of while, do_while and for statement to realize the 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 content

1. Use a for statement to implement a loop

(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 terms. 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++)
    {
     fac=1.0;
        for(j=1;j<=2*i-1;j++)
        {
         fac=fac*j;
  }
        sum=sum+fac*sign;
        sign=-sign;
    }
    printf("sun=%.0f\n",sum);
}

(3) Problem analysis: n is the number entered by the keyboard, i controls the total number of cycles, j controls the result of 2n-1, fac and sum control the output of factorial operation, and sign controls the sign of the result. I didn't notice that the symbol is a minus sign, and I started thinking after reading the flowchart, which is more difficult to understand.

(4) Flow chart:

 

 2. Use the while loop statement to realize the loop

(1) A brief description of the problem: Find all the daffodils (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, such as 153 is the daffodil number 1 * 1 + 5 * 5 + 3 * 3 = 153)

(2) Experimental code:

#include<stdio.h>
main()
{
 int x,y,z;
 int k=100;
 while(100<=k&&k<=999)
 {x=k/100;
 y=(k/10)%10;
 z=k%10;
 if(k==x*x*x+y*y*y+z*z*z)
    printf("%d\n", k);
    k++;
 }
}

(3) Problem analysis: Determine the condition of 3 digits, pay attention to the writing method judged in the logic of c language. This question is the simplest among several questions.

(4) Flow chart:

 3. Use the while statement and the for statement to implement the loop

(1) A brief description of the problem: Enter 4 characters and convert them to 4 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')*1;
    }
    printf("Data=%d",data);

(3) Problem analysis: use for loop to input characters, if statement to judge, and after jumping out of the loop, convert to decimal integer to form 4 digits.

(4) Flow chart:

 

4. Use the for statement to nest the loop

A

(1) A brief description of the problem: there are 100 horses. To carry 100 loads, of which 1 horse can carry 3 loads, 1 medium horse can carry 2 loads, and two ponies can carry 1 load, how many combinations can be made in Malaysia, medium horses and ponies .

(2) Experimental code:

#include <stdio.h>
main ()
{
 int m, n, k;
 int sum = 0;
 printf ("Various pack methods are as follows: \ n");
 for (m = 1; m <= 100; m ++)
 for (n = 1; n <= 100-m; n ++)
 {
  k = 100-mn;
        if ((k% 2 == 0) && (3 * m + 2 * n + 0.5 * k == 100))
        {
            printf ("Malaysia% 3d horses; Middle Horse% 3d horses; Pony% 3d horses. \ n", m, n, k);
            sum ++;
        }
 }
 printf ("A total of% d piggyback methods. \ n" , sum); 
}

(3) Problem analysis: Use mathematical thinking to find the relationship between big, medium and small ponies, first define the variables of Malaysia and Chinese ponies, then exclude the ponies, judge by if judgment sentences, and pay attention to logical writing.

B

(1) A brief description of the problem: write a program to find the sum of the first 6 terms of a positive integer arithmetic sequence. The sum of the first 4 terms of the sequence is 26, and the product of the first 4 terms is 880.

(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 ("The initial value of the sequence is a =% d, the difference is d =% d \ n", a, d);
         printf ("\ nThe value of the first 6 items of the sequence:");
   for (i = 0; i <6; i ++)
   {
    printf ("% d", a + i * d);
    sum = sum + (a + i * d);
   }
   printf ("\ n");
  }
       
 }
 printf ("\ n 6 items:% d \ n ", sum);
}

 (3) Problem analysis: It is difficult to understand the question according to the mathematical formula. I have n’t understood it for a long time. The first 6 items can only be obtained by asking for the first item a and the difference d, and finally get 6 items. with.

C

(1) A brief description of the problem: 30 students buy snacks together and spend a total of 50 yuan. Among them, each university student spends 3 yuan, each middle school student spends 2 yuan, and each elementary student spends 1 yuan. Ask the university, middle and primary school students How many different solutions are there for the number of people (remove the solution where the number of students in a certain class 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;
              }
  }

 }
 printf ("There are% d different combinations. \ n", sum);
}

(3) Problem analysis: It is also a classification problem. It feels a bit like the horse problem. It is easier to understand and quick to do.

5. Nine Nine Multiplication Table

Experiment 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;
}

Problem analysis: various triangles in the reference book.

Guess you like

Origin www.cnblogs.com/Yee123/p/12682842.html