Huazhong University of Science and Technology SPOC Programming Questions Chapter 5

1 Enter ten unequal positive integers, find the average value, and find the integer closest to the average value. If there are two, both output (20 points)
.

Enter ten unequal positive integers, find the average, and find the integer closest to the average. If there are two, both output

Input format: 10 positive integers

Output format: all positive integers closest to the average value, separated by carriage return between numbers

Input example: 1 2 3 4 5 6 7 8 9 10

Sample output:

5

6

#include<iostream>
#include<cmath>
using namespace std;
int main(){
    
    
double  a[10],b[10];
for (int i=0;i<10;i++)
{
    
    
cin>>a[i];
}
double s=0;
for(int i=0;i<10;i++)
{
    
    
s=s+a[i];
}
s/=10;
for(int i=0;i<10;i++)
{
    
    
b[i]=fabs(a[i]-s);
}
double min=b[0];
for(int i=0;i<10;i++)
{
    
    
if(b[i]<min)
min=b[i];
}
for(int i=0;i<10;i++)
{
    
    
if(fabs(a[i]-s)==min)
cout<<a[i]<<endl;
}
return 0;
}

Time limit: 500ms Memory limit: 128000kb
2 Enter the value of a 5*5 array element, and enter an integer m (m<10 m> (10 points)
Question content:

Input the value of a 5*5 array element, and input an integer m (m<10), multiply the value of the upper right half corner element of the two-dimensional array by m, and then output the multiplied two-dimensional array element. Each array element occupies 5 characters width, and is right-aligned, and wraps after each output line

Input format: first input the two-dimensional array elements by line, and then input the integer m

Output format:

Output the multiplied array elements, each array element occupies 5 characters width, and is right-aligned, wrap after each output line

Input sample:

1 2 3 4 5
2 3 4 5 6
3 4 5 6 7
4 5 6 7 8
5 6 7 8 9
3

Sample output:

 3    6    9   12   15

 2    9   12   15   18
 3    4   15   18   21
 4    5    6   21   24
 5    6    7    8   27
#include <iostream>
#include<iomanip>
using namespace std;
int main() 
{
    
    
	int matrix[5][5] = {
    
     0 };
	for(int i=0;i<=4;i++)
		for (int j = 0; j <= 4; j++)
		{
    
    
			cin >> matrix[i][j];
		}
	int x = 0;
	cin >> x;
	for(int i=0;i<=4;i++)
		for (int j = i; j <= 4; j++)
		{
    
    
			matrix[i][j] *= x;
		}
	for (int i = 0; i <= 4; i++)
	{
    
    
		for (int j = 0; j <= 4; j++)
		{
    
    
			cout << setw(5) << matrix[i][j];
		}
		cout << endl;
	}

	return 0;
}

3 Input 6 strings of unequal length, sort the 6 strings according to the string length, and output the strings in order from longest to shortest. (20 points)
Question content: Input 6 strings of unequal length, sort the 6 strings according to the length of the strings, and output the strings in order from longest to shortest.

Input format: input six unequal-length strings with a length not exceeding 45

Output format:

Output strings sorted by length, separated by carriage returns

Input sample:

United States
Pakistan
Uzbekistan
Kazakhstan
Russia
People's Republic of China

Sample output:

People's Republic of China
Uzbekistan
Kazakhstan
Pakistan
Russia
United States

Time limit: 500ms Memory limit: 128000kb

#include<iostream>
#include<cstring>
using namespace std;
int main(){
    
    
char a[6][45];
int b[6];
for(int i=0;i<6;i++)
{
    
    
cin>>a[i];
}
for(int i=0;i<6;i++)
b[i]=strlen(a[i]);
for(int i=0;i<6;i++)
{
    
    
for(int j=0;j<5;j++)
{
    
    
if(b[j]>b[j+1]) 
{
    
    
int t=b[j];
b[j]=b[j+1];
b[j+1]=t;
}
}
}
for(int i=5;i>=0;i--)
{
    
    
for(int j=0;j<6;j++)
if(b[i]==strlen(a[j]))
cout<<a[j]<<endl;
}
}

Guess you like

Origin blog.csdn.net/weixin_51236357/article/details/112077242