Experimental Report of "C Language Programming" (4)

C language programming report

Experimental items: Class practice 5-3-1, practice 2, 5-3-2, practice 2, 5-3-4, 5-3-5, practice 1, 2, 3

Name: Yu Yongxiang Experiment Location: Home Experiment Time: April 10, 2020

1. The purpose and requirements of the experiment

1. Skillfully 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. Experimental exercises:

Experiment 5-3-1 Exercise 2

Experiment code:

#include<stdio.h>
main()
{
	int i,j,n,sign=1;
	float fac,sum;
	printf("Please input value of n:\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=sum+fac;
		sign=-sign;
	}
	printf("sum=%.f\n",sum);
}

There is a problem : the meaning of the question is unclear

Solution : Find mathematical information

2. Experimental exercises:

Experiment 5-3-2 Exercise 2

Experiment 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(k==x*x*x+y*y*y+z*z*z)//控制条件,使其满足水仙花数 
        printf("%d\n",k);
        k++;
    }
}

There is a problem : none

Solution : None

3. Experimental exercises:

Experiment 5-3-4

Experiment 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;
		}
		switch(k)
		{
			case 0:Data+=(c-'0')*1000;break;
			case 1:Data+=(c-'0')*100;break;
			case 2:Data+=(c-'0')*10;break;
			case 3:Data+=(c-'0');break; 
			default:break; 
		}
	}
		printf("Date=%d ",Data); 
}

There is a problem : the quoted character is sometimes wrong

Solution : Confirm again that every character is added ''

4. Experimental exercises:

Experiment 5-3-5 Exercise 1

Experiment 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("大马%3d匹;中马%3d匹;小马%3d匹。\n",m,n,k);
		     sum++;
		  }
		} 
    }
	printf("共有%d种驮法\n",sum); 
}

There is a problem : the data is wrong

Solution : actually missed '=' in the condition

5. Experimental exercises:

5-3-5 Exercise 2

Experiment code:

#include<stdio.h>
main()
{
	int a,b,c,d,i,sum=0;//设a为首项,d为差值,b为前四项的和,c为前四项的积
	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数列前6项的值:");
			   for(i=0;i<6;i++)
			   {
                                printf("%d ",a+i*d);
			        sum=sum+(a+i*d);
				} 
				printf("\n");
		   }
		} 
		printf("\n数列的前6项和:%d\n",sum);
}

There is a problem : none

Solution : None

6. Experimental exercises:

5-3-5 Exercise 3

Experiment code:

#include<stdio.h>
main()
{
	int x,y,z,sum;//设x为大学生的,y为中学生的,z为小学生的 
	sum=0;
	for(x=1;x<30;x++)//外循环 
	{
		for(y=1;y<30;y++)
		{
			z=30-x-y;
			if(x*3+y*2+z==50)
			{
				printf("大学生%3d\t 中学生%3d\t 小学生%3d\n",x,y,z);
				sum=sum+1;
			}
		}
	}
	printf("共有%d种不同的组合。\n",sum);
}

There is a problem : none

Solution : None

3. Experimental summary

reward:

1. I understand the difference and conversion of the three kinds of loop statements, their respective adaptability, and the use of loop nesting;

2. Mastered a certain while, do_while and for statement to achieve the loop method, but not skilled;

3. There is a certain improvement in logic;

insufficient:

1. It is difficult to understand complex cycles, which is difficult to understand;

2. Often missing some details, leading to problems in the final result;

Guess you like

Origin www.cnblogs.com/GZ-1128-XZ/p/12697271.html