2018 NPU computer test questions (C language)

1. Product: Given n sets of numbers, each set of two integers, output the product of these two integers.

Input example:
2
1 1
2 3
Output:
1
6

Reference Code:

#include<stdio.h>
int main()
{
    
    
	int n,a,b,c;
	scanf("%d",&n);
	while(n--)
	{
    
    
		scanf("%d%d",&a,&b);
		c=a*b;
		printf("%d\n",c);
	}
}

2. Find the factorial: Given n groups of numbers, each group has an integer, output the factorial of the group of numbers.

Input example:
2
3
5
Output:
6
120

Reference Code:

#include<stdio.h>
int function(int n)
{
    
    
	int k=1,i;
	for(i=1;i<=n;i++)
		k*=i;
	return k;
}
int main()
{
    
    
	int n,a,x;
	scanf("%d",&n);
	while(n--)
	{
    
    
		scanf("%d",&x);
		a=function(x);
		printf("%d\n",a);
	} 
}

3.C(n,m): Find the number of different ways to select m numbers from n numbers.

Input sample:
10 3
Output:
120

Reference Code:

#include<stdio.h>
int main()
{
    
    
	int n,m;
	scanf("%d%d",&n,&m);
	int p=1,q=1,i,x;
	for(i=n;i>n-m;i--)
		p=p*i;
	for(i=m;i>0;i--)
		q=q*i;
	x=p/q;
	printf("%d",x);
}

4. Given n groups of numbers, each group has m numbers, sort each group of numbers from small to large and output.

Input example:
2 4
3 5 2 8
2 7 9 8
Output:
2 3 5 8
2 7 8 9

Reference Code:

#include<stdio.h>
int sort(int a[],int n)
{
    
    
	int i,j,k;
	for(i=0;i<n-1;i++)
	{
    
    
		for(j=0;j<n-i-1;j++)
		{
    
    
			if(a[j]>a[j+1])
			{
    
    
				k=a[j];
				a[j]=a[j+1];
				a[j+1]=k;
			}
		}
	}
}
int main()
{
    
    
	int i,n,m;
	scanf("%d%d",&n,&m);
	int b[m];
	while(n--)
	{
    
    
		for(i=0;i<m;i++)
			scanf("%d",&b[i]);
		sort(b,m);
		for(i=0;i<m;i++)
			printf("%d ",b[i]);
	}
}

5. String reversal: Given n groups of strings, the number of strings in each group does not exceed 20, and the reverse string of each group of strings is output.

Input example:
3
nwpu
china
xi an
Output:
upwn
anihc
na ix

Reference code:
Method 1:

#include<stdio.h>
#include<string.h>
int main()
{
    
    
	int n,i,j,len;
	char a[20],b[20];
	scanf("%d%*c",&n);
	while(n--)
	{
    
    
		gets(a);
		len=strlen(a);
		for(i=0,j=len-1;i<len;i++,j--)
			b[i]=a[j];
		for(i=0;i<len;i++)
			printf("%c",b[i]);
		printf("\n");
	}
}

Method Two:

#include<stdio.h>
#include<string.h>
int main()
{
    
    
	int n,i,len;
	scanf("%d%*c",&n);
	char a[20];
	while(n--)
	{
    
    
		gets(a);
		len=strlen(a);
		for(i=len-1;i>=0;i--)
			printf("%c",a[i]);
		printf("\n");
	}
}

6. Determine whether it is a "palindrome": Given n sets of strings, determine whether each set of strings is a palindrome (it is the same when looking from left to right and from right to left, including spaces, English, numbers, and symbols) , if so, output yes, otherwise output no.

Input example:
4
nwpu
madam
1001
xi ix
Output:
no
yes
yes
yes

Reference Code:

#include<stdio.h>
#include<string.h>
int main()
{
    
    
	int n,i,j,len;
	char a[100],b[100];
	scanf("%d%*c",&n);
	int flag;
	while(n--)
	{
    
    
		flag=1;
		gets(a);
		len=strlen(a);
		for(i=0,j=len-1;i<len;i++,j--)
			b[i]=a[j];
		b[len]='\0';
		for(i=0;i<len;i++)
		{
    
    
			if(b[i]!=a[i])
			{
    
    
				flag=0;
				break;
			}
		}
		if(flag==1)
			printf("yes\n");
		else
			printf("no\n");
	}
	return 0;
}

7. Determine whether the brackets match: given n groups of numbers, each group is a string, test three types of brackets: {}, [], (), and the order can only be the first left bracket, the last right bracket, and the spaces between brackets can Nested. If there is a match, output yes, otherwise output no.
For example: {@}[a](4) and {[0]} are matched;
{[[9]} and {}{ are not matched.

Input example:
2
{a}[b](d)
{[(]}
Output:
yes
no

Reference Code:

#include<stdio.h>
#include<string.h>
int main()
{
    
    
	int n,i,len;
	int big,mid,small;
	int flag;
	char a[100];
	scanf("%d%*c",&n);
	while(n--)
	{
    
    
		flag=1;
		big=0;
		mid=0;
		small=0;
		gets(a);
		len=strlen(a);
		for(i=0;i<len;i++)
		{
    
    
			switch(a[i])
			{
    
    
				case'{':
					big++;
					break;
				case'[':
					mid++;
					break;
				case'(':
					small;
					break;
				case'}':
					if(big>0) big--;
					else flag=0;
					break;
				case']':
					if(mid>0) mid--;
					else flag=0;
					break;
				case')':
					if(small>0) small--;
					else flag=0;
					break;
			}
		}
		if(big!=0 || mid!=0 || small!=0)
			flag=0;
		if(flag==1)
			printf("yes\n");
		else
			printf("no\n");
	}
}

Guess you like

Origin blog.csdn.net/Wxy971122/article/details/105219418