PTA programming ladder competition (161 ~ 180 questions)

Original is not easy, useful, please point a praise, thank you!

161. Word length (15 points)

Your program reads a line of text separated by spaces into several words end. You have to output the length of each word. The words here have nothing to do with language and can include various symbols. For example, it's a word with a length of 4. Note that consecutive spaces may appear in the line; final. Not included.

Input format:
Input gives one line of text in one line to end

Tip: use scanf ('% C',...); To read in a character until you read it until.

Output format:
Output the length of the word corresponding to this line of text in one line. Each length is separated by a space, and there is no last space at the end of the line.

Input example:
It's great to see you here.
Output example:
4 5 2 3 3 4

#include<stdio.h>
int main()
{
  /* This question has four most difficult points
     1. The first is that when there is only one word, there can be no extra space at the beginning and one space at the end
     2. The second is not displayed when the number of times is 0
     3. Third, we must pay attention to the space can not be redundant, whether the beginning, end, or middle
     */
	char c;
	int flag=1;   //Control space printing cannot be redundant
	int k=0;
	scanf("%c",&c);
	while(c!='.')   //If there is only one point, it doesn't need to be displayed
	{
		if(c ==' ')
		{
			if(k!=0)
			{
				if(flag)
				{
					printf("%d",k);
					flag=0; //After the first word is printed, it can be printed in the form of% d so that there is no space at the end
				}
				else      
				{
					printf(" %d",k);
				}
				k=0;
			}
		}
		else
		{	k++;	}
		scanf("%c",&c);
	}
	if(k!=0)        //If the last word is a space, don't print it
	{
		if(flag)      //Be responsible for preventing redundant spaces at the beginning and end
		{
			printf("%d",k);
		}
		else
		{
			printf(" %d",k);
		}
	}
}

162. Table output (5 points)

This topic requires the preparation of procedures and the output of tables in accordance with the specified format.

Input format:
This topic is not entered.

Output format:
The following tables are required to be output in strict accordance with the format given:

Province Area(km2) Pop.(10K)

Anhui 139600.00 6461.00
Beijing 16410.54 1180.70
Chongqing 82400.00 3144.23
Shanghai 6340.50 1360.26
Zhejiang 101800.00 4894.00

#include<stdio.h>
int main()
{
printf("------------------------------------\n");
printf("Province      Area(km2)   Pop.(10K)\n");
printf("------------------------------------\n");
printf("Anhui         139600.00   6461.00\n");
printf("Beijing        16410.54   1180.70\n");
printf("Chongqing      82400.00   3144.23\n");
printf("Shanghai        6340.50   1360.26\n");
printf("Zhejiang      101800.00   4894.00\n");
printf("------------------------------------\n");
}

163. Packing problem (20 points)

Input format:
Enter the number of items N (≤ 1000) given in the first line; N positive integers s given in the second line
​i
​​ (1≤s
​i
≤ 100, indicating the size of item i).

Output format:
Output the size of each item and its box serial number according to the input order. Each item accounts for 1 line, and the last line outputs the required number of boxes.

Input example:
8
60 70 80 90 30 40 10 20
Output example:
60 1
70 2
80 3
90 4
30 1
40 5
10 1
20 2
5

#include<stdio.h>
#include<string.h>
struct Love{
	int xiang;
	int ji;
}love[1001];
int main()
{
	int b[1000]={100};
	int max=0;
	int i,j;
	int first;
	int n;
	int num=0;
	int k=0;
	int a[100],one=0;
	scanf("%d",&n);
	for(i=0;i<n;i++)
	{
		scanf("%d",&love[i].xiang);
	}
	for(i=0;i<n;i++)
		b[i]=100;
	for(i=0;i<n;i++)
	{
		for(j=0;j<n;j++)
		{
			if(love[i].xiang<=b[j])
			{
				love[i].ji = j;	
				b[j]-=love[i].xiang;
				if(max<j)
					max=j;
				break;
			}
		}
		
	}
	
	
	for(i=0;i<n;i++)
	{
		printf("%d %d\n",love[i].xiang,love[i].ji+1);
	}
	printf("%d\n",max+1);
}

164. Sum the first N items of the interleaved sequence (15 points)

This problem requires writing a program to calculate the sum of the first N items of the staggered sequence 1-2 / 3 + 3 / 5-4 / 7 + 5 / 9-6 / 11 +.

Input format:
The input gives a positive integer N in one line.

Output format:
Output the value of partial sum in one line, and keep the result to three decimal places.

Input example:
5
Output example:
0.917
Author: Chen Jianhai
Setting: Zhejiang University
Time limit: 400 ms
Memory limit: 64 MB

#include<stdio.h>
int main()
{
	int n;
	int i;
	int zi = 1,mu = 1;
	int flag=1;
	double sum=0;
	scanf("%d",&n);
	for(i=0;i<n;i++)
	{
		sum+=zi*1.0/mu*flag;
		flag=-flag;
		zi+=1;
		mu+=2;
	}
	printf("%.3lf\n",sum);
}

165. Calculate the mean square deviation of the set data (15 points)


Input format:
The input first gives a positive integer N (≤ 10) on the first line
​4
), and the next line gives N positive integers. All numbers do not exceed 1000, and the numbers in the same row are separated by spaces.

Output format:
Output the mean square deviation of these N numbers. It is required to output 5 digits after the decimal point with fixed accuracy.

Input example 1:
10
6 3 7 1 4 8 2 9 11 5
Output example 1:
3.03974
Input example 2:
1
2
Output example 2:
0.00000

#include<stdio.h>
#include<math.h>
int main()
{
	int n;
	int i,j;
	double x=0;
	int a[10005];
	double result;
	int sum=0;
	double avg; 
	scanf("%d",&n);
	for(i=0;i<n;i++)
	{ 
		scanf("%d",&a[i]);
		sum+=a[i];
		//printf("1");
	}
	avg = sum*1.0/n;
	for(i=0;i<n;i++)
	{
		x += (a[i]-avg)*(a[i]-avg);
		//printf("2");
	}
	result = sqrt(x/n);
	printf("%.5lf\n",result);	
}

166. Currency conversion (20 points)

Enter an integer (no more than 9 digits) to represent a RMB value (unit: yuan). Please convert it to the capital Chinese format required by finance. For example, 23108 yuan will become "twenty-three thousand one hundred and eight" after conversion Yuan. In order to simplify the output, the lowercase English letters a-j are used to represent the upper case numbers 0-9, and S, B, Q, W and Y are used to represent ten, one hundred, ten thousand, ten thousand and one hundred million respectively. Thus, 23108 yuan should be converted to "cWdQbBai" yuan.

Input format:
The input gives a nonnegative integer of no more than 9 bits in one line.

Output format:
Output the converted result in one line. Note that the usage of "zero" must conform to Chinese habits.

Input example 1:
813227345
Output example 1:
iYbQdBcScWhQdBeSf
Input example 2:
6900
Output example 2:
gQjB

I didn't write this question correctly and didn't get a full score. It seems that it's 15 points. I want to commemorate my code

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
int main()
{
	int num;
	int i,j,n,xing=0;
	char s[100];
	char str1[100],a=-1;
	int k=0;		//Calculated digits
	int flag=1;
	gets(s);
	int len = strlen(s);
	for(i=0;i<len;i++)
	{
		num = s[i]-'0';
		if(num!=0)
		{
			xing++;
			flag=0;
		}
		if(i==len-1 && num!=0)
			flag=1;
	}
	for(i=0;s[i]!='\0';i++)
	{
		num = s[i]-'0';
		if(s[i]+=2)
		switch(num)
		{
		case 0:str1[++a]='a';break;
		case 1:str1[++a]='b';break;
		case 2:str1[++a]='c';break;
		case 3:str1[++a]='d';break;
		case 4:str1[++a]='e';break;
		case 5:str1[++a]='f';break;
		case 6:str1[++a]='g';break;
		case 7:str1[++a]='h';break;
		case 8:str1[++a]='i';break;
		case 9:str1[++a]='j';break;
		}
		a++;
		k++;
	}
	/*for(i=0;i<k;i++)
		printf("%c",str1[i]);*/		//debugging
	n=0;
	for(i=k*2-1;i>=0;)
	{
		if(n==1 || n==5)
		{
			str1[i]='S';
		}
		else if(n==2 || n==6)
		{
			str1[i]='B';
		}
		else if(n==3 || n==7)
		{
			str1[i]='Q';
		}
		else if(n==4)
		{
			str1[i]='W';
		}
		else
		{
			str1[i]='Y';
		}
		
		n++;
		i-=2;
	}
	if(flag)
		for(i=0;i<k+k-1;i++)
			printf("%c",str1[i]);
	else
			for(i=0;i<k+k-1-xing*2+1;i++)
		printf("%c",str1[i]);
}

167. Find the local maximum of the matrix (15 points)

Given an integer matrix A with M rows and N columns, if the non boundary element A[i][j] of a is greater than the adjacent upper, lower, left and right elements, then element A[i][j] is said to be the local maximum of the matrix. This problem requires all local maxima of a given matrix and their positions.

Input format:
Enter the row number M and column number N (3 ≤ M,N ≤ 20) of matrix A given in the first row; in the last M row, each row gives the values of N elements of A in this row. The numbers are separated by spaces.

Output format:
Each row outputs a local maximum in the format of "element value row number column number", where the row and column numbers start from 1. It is required to output incrementally according to the line number; If there is more than one local maximum in the same row, the row is output incrementally by column number. If there is no local maximum, then "None total rows total columns" will be output.

Input example 1:
4 5
1 1 1 1 1
1 3 9 3 1
1 5 3 5 1
1 1 1 1 1
Output example 1:
9 2 3
5 3 2
5 3 4
Input example 2:
3 5
1 1 1 1 1
9 3 9 9 1
1 5 3 5 1
Output example 2:
None 3 5

#include<stdio.h>
struct Love{
	int hang;
	int lie;
	int max;
}Max[200];
int main()
{
	int m,n;
	int i,j;
	int a[300][300];
	//int max[20],k=0;
	int k=0;
	int flag=0;
	scanf("%d%d",&m,&n);
	for(i=0;i<m;i++)
	{
		for(j=0;j<n;j++)
		{
			scanf("%d",&a[i][j]);
		}
	}
	for(i=0;i<m;i++)
	{
		if(i==0)
			continue;
		if(i==m-1)
			break;
		for(j=0;j<n;j++)
		{
			if(j==0||j==n-1)
				continue;
			if(a[i][j]>a[i-1][j] && a[i][j]>a[i+1][j] && a[i][j]>a[i][j+1] && a[i][j]>a[i][j-1])
			{
									Max[k].max=a[i][j];
									Max[k].hang=i+1;
									Max[k].lie=j+1;
									k++;
									flag=1;
			}
		}
	}
	if(flag!=0)
	{
		for(i=0;i<k;i++)
		{
			printf("%d %d %d\n",Max[i].max,Max[i].hang,Max[i].lie);
		}
	}
	else
	{
		printf("None %d %d\n",m,n);
	}
}

168. Query fruit price (15 points)

Given four kinds of fruits, namely apple, pear, orange and grape, the unit prices correspond to 3.00 yuan / kg, 2.50 yuan / kg, 4.10 yuan / kg and 10.20 yuan / kg respectively.

First, the following menu is displayed on the screen:

[1] apple
[2] pear
[3] orange
[4] grape
[0] exit
You can enter No. 1 ~ 4 to query the unit price of the corresponding fruit. When the number of consecutive queries exceeds 5, the program shall automatically exit the query; Exit when the user enters 0 less than 5 times; Enter another number to display the price as 0.

Input format:
Input gives several numbers entered continuously by the user in one line.

Output format:
First, display the menu on the screen. Then, corresponding to each input of the user, the query result is output in one line in the format of "price = price", in which the price retains two decimal places. When the user queries more than 5 times continuously, or actively enters 0, the program ends.

Input example 1:
3 -1 0 2
Output example 1:
[1] apple
[2] pear
[3] orange
[4] grape
[0] exit
price = 4.10
price = 0.00
Input example 2:
1 2 3 3 4 4 5 6 7 8
Output example 2:
[1] apple
[2] pear
[3] orange
[4] grape
[0] exit
price = 3.00
price = 2.50
price = 4.10
price = 4.10
price = 10.20

#include<stdio.h>
int main()
{
	int i,j;
	int n;
	int x;
	printf("[1] apple\n");
	printf("[2] pear\n");
	printf("[3] orange\n");
	printf("[4] grape\n");
	printf("[0] exit\n");
	
	for(i=0;i<5;i++)
	{
		scanf("%d",&x);
		if(x==0)
		break;
		if(x<0 || x>4)
		printf("price = 0.00\n");
		if(x==1)
		printf("price = 3.00\n");
		else if(x==2)
		printf("price = 2.50\n");
		else if(x==3)
		printf("price = 4.10\n");
		else if(x==4)
		printf("price = 10.20\n");
		
	}
}

169. Mixed type data format input (5 points)

This problem requires writing a program to read floating point number 1, integer, character and floating point number 2 in sequence, and then output them in the order of character, integer, floating point number 1 and floating point number 2.

Input format:
Enter floating point number 1, integer, character and floating point number 2 in one line, separated by 1 space.

Output format:
In one line, it is output in the order of character, integer, floating point number 1 and floating point number 2, in which the floating point number retains 2 digits after the decimal point.

Input example:
2.12 88 c 4.7
Output example:
c 88 2.12 4.70

#include<stdio.h>
int main()
{
	double a;
	int b;
	char c;
	double d;
	scanf("%lf%d %c%lf",&a,&b,&c,&d);
	printf("%c %d %.2lf %.2lf\n",c,b,a,d);
}

170. BCD decryption (10 points)

BCD number is a two digit decimal number expressed in one byte, and every four bits represent one bit. So if the hexadecimal of a BCD number is 0x12, it expresses 12 in decimal. But Xiao Ming didn't learn BCD. He converted all BCD numbers into decimal output as binary numbers. So 0x12 of BCD is output as decimal 18!

Now, your program will read the wrong decimal number and output the correct decimal number. Tip: you can convert 18 back to 0x12 and then back to 12.

Input format:
Input a positive integer in the range of [0, 153] in one line to ensure that it can be converted back to a valid BCD number, that is, when this integer is converted to hexadecimal, A-F numbers will not appear.

Output format:
Output the corresponding decimal number.

Input example:
18
Output example:
12

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

171. Pattern matching of strings (25 points)

Given two strings consisting of English letters, String and Pattern, it is required to find the position where the Pattern first appears in the String and output the substring of the String after this position. If Not Found, output "Not Found".

This topic aims to test the performance of different matching algorithms in various data situations. The characteristics of test data of each group are as follows:

Data 0: small-scale string, test the basic correctness;
Data 1: random data. The String length is 10
​5
, Pattern length is 10;
Data 2: random data. The String length is 10
​5
, Pattern length is 10
​2
​​ ;
Data 3: random data. The String length is 10
​5
, Pattern length is 10
​3
​​ ;
Data 4: random data. The String length is 10
​5
, Pattern length is 10
​4
​​ ;
Data 5: String length is 10
​6
, Pattern length is 10
​5
​​ ; Test for tail character mismatch;
Data 6: String length is 10
​6
, Pattern length is 10
​5
​​ ; Test for first character mismatch.
Input format:
The first line of input gives a String, which is composed of English letters and is no longer than 10
​6
String of. The second line gives a positive integer N (≤ 10), which is the number of Pattern strings to be matched. Then N lines, each line gives a Pattern, which is composed of English letters with a length of no more than 10
​5
String of. Each string is non empty and ends with a carriage return.

Output format:
For each Pattern, output the matching results according to the requirements of the problem surface.

Input example:
abcabcabcabcacabxy
3
abcabcacab
cabcabcd
abcabcabcabcacabxyz
Output example:
abcabcacabxy
Not Found
Not Found

#include<stdio.h>
#include<string.h>
#include<stdlib.h>
int main()
{
	char str1[1000001],str2[1000001];
	int i,n;
	gets(str1);
	scanf("%d",&n);
	getchar();
	for(i=0;i<n;i++)
	{
		gets(str2);
		char *p=strstr(str1,str2);
		if(p!=NULL)
		{
			for(;*p!='\0';p++)
				printf("%c",*p);
			printf("\n");
		}
		else
			printf("Not Found\n");
	}
	return 0;
}

172. Say Hello to Integers (5 points)

Say hello to integers? Yes! You're right! Now let's say "Hello ~" to integers. Read in two integers, and then output greetings to them.

Input format:
In one line, give two integers A and B with an absolute value of no more than 32767, with A space between them

Output format:
Output "Hello, A and B!" on one line (where A and B are replaced by actual input integers)

Input example:
1949 2015
Output example:
Hello, 1949 and 2015!

#include<stdio.h>
int main(void)
{
	int n,m;
	scanf("%d%d",&n,&m);
	printf("Hello, %d and %d!\n",n,m);
}

173. PTA refreshed me (5 points)

PTA refreshed me jpg

The above is a masterpiece of students from Hubei Institute of economics. Please output this sentence in Chinese pinyin.

Input format:
No input for this question.

Output format:
Follow the sample output in one line, ending with an exclamation mark.

Input example:
nothing
Output example:
PTA shi3 wo3 jing1 shen2 huan4 fa1 !

#include<stdio.h>
int main()
{
  printf("PTA shi3 wo3 jing1 shen2 huan4 fa1 !\n");
  return 0;
}

174. Temperature conversion (5 points)

This problem requires writing a program to calculate the Celsius temperature corresponding to the Fahrenheit temperature of 150 ° F. Calculation formula: C=5 × (f − 32) / 9, where: C represents Celsius temperature, f represents Fahrenheit temperature, and the output data shall be integer.

Input format:
This topic is not entered.

Output format:
Output in the following format

fahr = 150, celsius = integer value of calculated temperature in Celsius

#include<stdio.h>
int main()
{
  int f=150;
  printf("fahr = 150, celsius = %d\n",5*(f-32)/9);
  return 0;
}

175. Output diamond pattern (5 points)

This problem requires writing A program to output the specified diamond pattern composed of "A".

Input format:
No input for this question

Output format:
Output the diamond pattern composed of "A" in the following format.

A
A A
A

#include<stdio.h>
int main()
{
	printf("  A\n");
	printf("A   A\n");
	printf("  A\n");
}

176. Fish or meat (10 points)



The national standard height of 8-year-old boy is 130cm and the standard weight is 27kg; The standard height of an 8-year-old girl is 129 cm and the standard weight is 25 kg.

Now you should give nutritional suggestions according to the baby's height and weight.

Input format:
The input gives a positive integer N not exceeding 10 in the first line, and then N lines, each line gives the body data of a baby:

Gender height weight
Where gender is 1 for boys and 0 for girls. Height and weight are positive integers no more than 200.

Output format:
For each baby, give your advice in one line:

If it is too short, the output is: duo chi yu! (eat more fish);
If it is too thin, output: duo chi rou! (eat more meat);
If the standard is positive, output: wan mei! (perfect);
If it is too high, output: ni li hai! You're great;
If it is too fat, output: shao chi rou! (eat less meat).
Evaluate height first, then weight. There should be a space between two sentences.

Input example:
4
0 130 23
1 129 27
1 130 30
0 128 27
Output example:
ni li hai! duo chi rou!
duo chi yu! wan mei!
wan mei! shao chi rou!
duo chi yu! shao chi rou!

#include<stdio.h> 
int main()
{
	int a,s,h,w;
	scanf("%d",&a);
	while(a--)
	{
		scanf("%d %d %d",&s,&h,&w);
	    switch(s)
	    {
			case 1: 
			if(h==130) printf("wan mei! ");
			else if(h<130) printf("duo chi yu! ");
			else if(h>130) printf("ni li hai! ");
			if(w==27) printf("wan mei!\n");
			else if(w<27) printf("duo chi rou!\n");
			else if(w>27) printf("shao chi rou!\n");
			break;
			case 0:
			if(h==129) printf("wan mei! ");
			else if(h<129) printf("duo chi yu! ");
			else if(h>129) printf("ni li hai! ");
			if(w==25) printf("wan mei!\n");
			else if(w<25) printf("duo chi rou!\n");
			else if(w>25) printf("shao chi rou!\n");
			break;
		}
	}
	return 0;
}

Original is not easy, useful, please point a praise, thank you!

Keywords: pta

Added by tolli on Sun, 02 Jan 2022 20:24:06 +0200