Jiangsu Province Computer Second -level C Operation Questions Compilation

2023-03-18  

Test data
17 24 18 8 15
23 5 7 14 16
4 16 13 20 22
10 12 19 21 3
11 18 25 2 9
screen output
17 24 18 8 15
23 5 7 14 16
4 16 13 20 22
10 12 19 21 3
11 18 25 2 9
yes

// I wrote
#include<stdio.h>
#define N 5

int judge(int a[N][N])
{
    
	int i,sum1=0,sum2=0,sum3=0,sum4=0,sum5=0,sum6=0,sum7=0;
	for(i=0;i<N;i++)
	{
    
		sum1+=a[0][i];
		sum2+=a[1][i];
		sum3+=a[2][i];
		sum4+=a[3][i];
		sum5+=a[4][i];
		sum6+=a[i][i];
		sum7+=a[i][4-i];  
	}
/*	printf("%d\n%d\n%d\n%d\n%d\n%d\n%d\n",sum1,sum2,sum3,sum4,sum5,sum6,sum7);*/
	if(sum1==sum2&&sum2==sum3&&sum3==sum4&&sum4==sum5&&sum5==sum6&&sum6==sum7)
	{
    
		return 1;
	}else{
    
		return 0;
	}
}

int main()
{
    
	int a[N][N]={
    {
    17,24,1,8,15},{
    23,5,7,14,16},{
    4,6,13,20,22},{
    10,12,19,21,3},{
    11,18,25,2,9}};
	int i,j;
	
	FILE *fp;
	fp=fopen("myf2.out","w");
	for(i=0;i<N;i++)
	{
    
		for(j=0;j<N;j++)
		{
    
			printf("%d\t",a[i][j]);
			fprintf(fp,"%d\t",a[i][j]);
			if(j==N-1)
			{
    
				printf("\n");
				fprintf(fp,"\n");
			}
		}
	}
	if(judge(a))
	{
    
		printf("yes\n");
		fprintf(fp,"yes\n");
	}else{
    
		printf("no\n");
		fprintf(fp,"no\n");
	}
	fprintf(fp,"\nMy exam number is B3010124");
	fclose(fp);
	return 0;
}

// Refer to the answer to improvement, which is more intelligent
#include<stdio.h>
#define N 5

int judge(int a[N][N])
{
    
	int i,j,*p,b[N+N+2]={
    0};
	for(i=0;i<N;i++)
		for(j=0;j<N;j++)
			b[i]+=a[i][j];  

	for(i=0;i<N;i++)
		for(j=0;j<N;j++)
			b[i+N]+=a[j][i];  

	for(i=0;i<N;i++)
		b[N+N]+=a[i][i];
	
	for(i=0;i<N;i++)
		b[1+N+N]+=a[i][4-i];
	
	for(i=0;i<N+N+2;i++)
		printf("%d\n",b[i]);
/*	printf("%d\n%d\n%d\n%d\n%d\n%d\n%d\n",sum1,sum2,sum3,sum4,sum5,sum6,sum7);*/

	p=b;
	while(p<(b+N+N+2))
	{
    
		if(*p==*(p+1))
		{
    
			p++;
		}else{
    
			break;
		}
	}

	if(p==b+N+N+1)
	{
    
		return 1;
	}else{
    
		return 0;
	}

}

int main()
{
    
	int a[N][N]={
    {
    17,24,1,8,15},{
    23,5,7,14,16},{
    4,6,13,20,22},{
    10,12,19,21,3},{
    11,18,25,2,9}};
	int i,j;
	FILE *fp;
	fp=fopen("myf2.out","w");
	for(i=0;i<N;i++)
	{
    
		for(j=0;j<N;j++)
		{
    
			printf("%d\t",a[i][j]);
			fprintf(fp,"%d\t",a[i][j]);
			if(j==N-1)
			{
    
				printf("\n");
				fprintf(fp,"\n");
			}
		}
	}
	if(judge(a))
	{
    
		printf("yes\n");
		fprintf(fp,"yes\n");
	}else{
    
		printf("no\n");
		fprintf(fp,"no\n");
	}
	fprintf(fp,"\nMy exam number is B3010124");
	fclose(fp);
	return 0;
}

// Programming Questions 2
// The function of writing function int
// In the two -dimensional array that is stored in A direction, the function returns the number of three digits that meet the above conditions
[Running Structure]
screen output:
i power
100 10000
250 62500
376 141376
500 250000
600 360000
625 390625
760 577600

#include<stdio.h>

int long find(long a[][2])
{
    
	int j=0;
	long i=0,m,n;
	for(i=100;i<1000;i++)
	{
    
		n=m=i*i;
		while(m>=i)   
		{
    
			if(m%1000==i)// The basic idea is 62500%1000 = 250,10000%1000 = 0,1000%1000 = 0,100%1000 = 100
			{
    
				break;
			}
			m/=10;
		}
		if(m>=i)
		{
    
			a[j][0]=i;
			a[j][1]=n;
			j++;
		}
	}
	return j;
}

int main()
{
    
	long a[10][2]={
    0};// cannot be defined here a [] [2], you must provide a complete line and column, otherwise you will report an error, although the results can come out
	int n,i;
	FILE *fp;
	fp=fopen("myf2.out","w");
	printf("i\tpower\n");
	fprintf(fp,"i\tpower\n");
	n=find(a);
	for(i=0;i<n;i++)
	{
    
		printf("%d\t%d\n",a[i][0],a[i][1]);
		fprintf(fp,"%d\t%d\n",a[i][0],a[i][1]);
	}
	fprintf(fp,"My Exam number is B16030124\n");
	fclose(fp);
	return 0;
}

// Bshand
// Programming Questions 3
// Writing a function int fun (int m, int n, int a [])
// The function of the function is to find out, M to all the number of conditions that meet the conditions,
// This condition is, 1. The number is the number of prime; 2. The number of digits that are expressed in the centers and the sum of the ten digits are exactly the 100 -digit number of the number, such as 293
// Stall these integers from the order of large to small to the array of A direction, and the function returns the number of integer in the A array
// (2) Write the main () function, and the function declaration contains a integer array of 100 yuan data A, input positive integer m, n m <n
// Output it in 5 formats per line to the screen and file myf2.out, finally save the candidate’s test number to the file

int judgeprime(int n)
{
    
	int i;
	for(i=2;i<n/2;i++)
	{
    
		if(n%i==0)
			break;
	}
	if(i>=n/2)

	{
    
		return 1;
	}
	else 
	{
    
		return 0;
	}
}

int judge2(int n)// What needs to be improved is to find positions, such as asking all int gw = n%10; int SW = N/10%10;
{
    
	int a,b,c,d;
	if(n>=100&&n<1000)
	{
    
		a=n/100;
		b=(n-a*100)/10;  
		c=n-(a*100+b*10);
	}else if(n>=1000&&n<10000)
	{
    
		d=n/1000;
		a=(n-d*1000)/100;
		b=(n-d*1000-a*100)/10;
		c=n-(d*1000+a*100+b*10);
	}

	if((b+c)%10==a)
	{
    
		return 1;
	}
	else{
    
		return 0;
	}
}

int fun(int m,int n,int a[])
{
    
	int i,j=0;
	for(i=m;i<=n;i++)
	{
    
		if(judgeprime(i)&&judge2(i))
		{
    
			a[j++]=i;
		}
	}
	return j;
}

int main()
{
    
	int a[100],m,n,k,i;
	FILE *fp;
	fp=fopen("mf2.out","w");
	do{
    
			scanf("%d %d",&m,&n);
	}while((m>n)||m<0||n<0);
	k=fun(m,n,a);
	for(i=k-1;i>=0;i--)
	{
    
		printf("%d\t",a[i]);
		fprintf(fp,"%d\t",a[i]);

		if(i%5==0)
		{
    
			printf("\n");
			fprintf(fp,"\n");
		}
	}
	fprintf(fp,"\nmy exam number is B1342423\n");
	fclose(fp);
	return  0;

}

// Programming Questions 4
struct stu
{

int num; //
char name[20]; //
int count; /Number of books/
}
// (1) Writing function int sortcount (Struct STU S [], int n) function function is n students before the array of S to the s
// Sort from large to small according to the number of books purchased, and count the total number of books for books in n students. The function returns the statistical results
// (2) Write the main function. The function function is an array S that declares a Struct STU type, and the test data is initialized, and the function is called
// [Test Data]
11,li,1
22,zhang,3
33,wang,1
44,zhou,2,
55,liang,4
screen output:
55 liang 4
22 zhang 3
44 zhao 2
11 li 1
33 wang 1
sum=11

#include<stdio.h>
#define N 5
struct stu
{
    
	int num;
	char name[20];
	int count;
};

int sortcount(struct stu s[],int n)
{
    
	int i,j,sum=0;
	struct stu temp;
	for(i=0;i<n-1;i++)
	{
    
		for(j=0;j<n-1-i;j++)
		{
    
			if(s[j].count<s[j+1].count)
			{
    
				temp=s[j];
				s[j]=s[j+1];
				s[j+1]=temp;
			}
		}
	}
	for(i=0;i<n;i++)
	{
    
		sum+=s[i].count;
	}
	return sum;
}

int main()
{
    
	struct stu s[N]={
    {
    11,"li",1},{
    22,"zhang",3},{
    33,"wang",1},{
    44,"zhou",2,},{
    55,"liang",4}};
	int totalcount,i;
	FILE *fp;
	fp=fopen("myf2.out","w");
	printf("\n");
	totalcount=sortcount(s,N);// I found an interesting phenomenon, and the data name cannot be paid to the function name
	for(i=0;i<N;i++)
	{
    
		printf("%d\t%s\t%d\n",s[i].num,s[i].name,s[i].count);
		fprintf(fp,"%d\t%s\t%d\n",s[i].num,s[i].name,s[i].count);
	}
	printf("totalcount=%d\n",totalcount);
	fprintf(fp,"totaltount=%d\n",totalcount);
	fclose(fp);
	return 0;

}

// Programming Questions 5 // Pay attention to find rules from horizontal and vertical, don’t be too stupid
// Generate a proof, the outermost layer is 1, the second layer is 2, and so on.
// Requirement (1) Define a symbol constant n represents 20
// (2) Writing function void matrix (int (*a) [n], int n) function function is to generate a N -order square array, and
// Save it to a two -dimensional array in A direction of A
//, for example n = 5
//1 1 1 1 1
//1 2 2 2 1
//1 2 3 2 1
//1 2 2 2 1
//1 1 1 1 1
// (3) Write the main function, declare n lines n column elements, and enter n (1 <n <= n)

#include<stdio.h>
#define N 20
void matrix(int (*a)[N],int n)
{
    
	int i,j,k,m;
	m=(n+1)/2;
	for(i=0;i<m;i++)
	{
    
		for(j=i;j<n-i;j++)
			a[i][j]=a[n-i-1][j]=i+1;   

		for(k=i;k<n-i;k++) 
			a[k][i]=a[k][n-i-1]=i+1;   
	}
}
void main()
{
    
	int i,j;
	int a[N][N]={
    0},n;
	FILE *fp;
	fp=fopen("myf2.out.","w");
	scanf("%d",&n);
	matrix(a,n);
	for(i=0;i<n;i++)
	{
    
		for(j=0;j<n;j++)
		{
    	
			printf("%3d",a[i][j]);
			fprintf(fp,"%3d",a[i][j]);
		}
		printf("\n");
		fprintf(fp,"\n");
	}
	fprintf(fp,"My exam number is B16030124\n");
	fclose(fp);
}

// Programming Questions 6

Jiangsu Province Computer Second -level Operation Questions#include<stdio.h>
int find(long a[][2])
{
    
	int j=0;
	long i,m,n;
	for(i=100;i<1000;i++)
	{
    
		n=m=i*i;
		while(m>=i)  
		{
    
			if(m%1000==i) 
				break;
			m/=10;
		}
		if(m>=i)
		{
    
			a[j][0]=i;
			a[j++][1]=n;
		}
	}
	return j;
}

void main()
{
    
	int i,n;
	long a[10][2];
	FILE* fp;
	fp=fopen("myf2.out","w");
	n=find(a);
	printf("i\tpower\n");
	fprintf(fp,"i\tpower\n");
	for(i=0;i<n;i++)
	{
    
		printf("%ld\t%ld\n",a[i][0],a[i][1]);
		fprintf(fp,"%ld\t%ld\n",a[i][0],a[i][1]);	
	}
	fprintf(fp,"My Exam Number IS: Admission Test Number");
	fclose(fp);	
}

// Programming Questions 7
/Remove a maximum score, remove a minimum score, remove the highest score, and then remove a minimum score
// Take the average score

#include<stdio.h>
#define N 10
double getscore(double score[])
{
    
	double *start=score,*end=score+N;
	double max1,max2,min1,min2,aver=0;
	max1=max2=min1=min2=*score;
	for(score++;score<end;score++)
	{
    
		if(max1<*score)
		{
    
			if(max2<*score)
			{
    
				max2=max1;
				max1=*score;
			}else
			{
    
				max2=*score;
			}
		}
		else if(min2>*score)
		{
    
			if(min1>*score)
			{
    
				min2=min1;
				min1=*score;
			}else
			{
    
				min2=*score;
			}
		}
	}

	for(score--;score>=start;score--)
		aver+=*score;
	return(aver-max1-max2-min1-min2)/(N-4);
}

int main()
{
    
	int i;
	double score[N]={
    9.34,9.19,9.33,8.43,8.89,7.84,8.23,8.99,9.80,9.33};
	double lastscore;
	FILE *fp;
	fp=fopen("myf2.out","w");
	for(i=0;i<N;i++)
	{
    
		printf("%4.2f\n",score[i]);
		fprintf(fp,"4.2%f\n",score[i]);
	}
	printf("\n");
	fprintf(fp,"\n");
	lastscore=getscore(score);
	printf("last score=%4.2f\n",lastscore);
	fprintf(fp,"last score=%4.2f\n",lastscore);
	fprintf(fp,"My exam number is B16030124");
	fclose(fp);
	return 0;
}
// [Comment] After comparing the reference answer, I found that I did not calculate the sum of each column. What needs to be improved is to store and compare it with array.

source

Random Posts

Git experience for the first time

Primary principle of ArrayList that primary school students can also understand

Back to Bosch, Last year, the car losing nearly 100 million yuan last year received 150 million yuan of investment

YOLOV3 MAP calculation tutorial

[2629] Identity Card 【Compare Pit / Water Questions】 The