892 programming of Inner Mongolia University

Keywords: C++ Programming

2019

1. Students' scores can correspond to different grades according to different score segments, for example, 90-100 is A; 80-89 points are B; 70-79 is divided into C; 60-69 points are D; Below 60 points is E. It is required to input the student score of the hundred mark system, and output the score grade corresponding to the student score.

For example, input 95, output A

#include <iostream>

using namespace std;

int main(){
	int score;
	while(1){
		cin>>score;
		if(score>100 ||score<0){
			cout<<"ERROR!"<<endl;
		}
		else{
			score = score/10;
			switch(score){
			case 10:
			case 9:cout<<"A"<<endl;break;
			case 8:cout<<"B"<<endl;break;
			case 7:cout<<"C"<<endl;break;
			case 6:cout<<"D"<<endl;break;
			default:cout<<"E"<<endl;break;
			}
		}
	}
}

Operation results:

2. Use a recursive algorithm to sum 1 to 100 and output.

#include <iostream>

using namespace std;

int res(int n){
	if(n==1)
		return 1;
	else 
		return res(n-1) + n;
}

int main(){
	int n = 100;
	int sum;
	sum = res(n);
	cout<<sum<<endl;;
}

Operation results:

3. Enter four numbers arbitrarily. It is required to sort the four numbers and output them from large to small.

#include <iostream>

using namespace std;

int main()
{
	int i,a[4],max,temp;
	cout<<"Enter four integers:"<<endl;
	for(i=0;i<=3;i++)
		cin>>a[i]; 
	for(i=0;i<=3;i++)
	{
		max = a[i];
		for(int j=i;j<=3;j++)
		{
			if(a[j]>max){
				temp = max;
				max = a[j];
				a[j] = temp;
			}
		}
		a[i] = max;
	}
	cout<<"Output these four numbers from large to small:"<<endl;
	for(i=0;i<=3;i++)
		cout<<a[i]<<" ";
	cout<<endl;
} 

Operation results:

4. Input a string of characters and find out the continuous numbers and output them.

For example, input ab123cd4f56 and output 123 4 56

#include <iostream>
#include <string>

using namespace std;

int main(){
	string str;
	int i,len,sign = 0;
	cin>>str;
	len = str.length();
	cout<<"The integer in the string has:";
	for(i=0;i<len;i++){
		if(str[i]>='0' && str[i]<='9'){
			if(sign==0){
				cout<<" ";
			}
			cout<<str[i];
			sign = 1;
		}
		else{
			sign = 0;
		}
	}
	cout << endl;
	return 0;
}

Operation results:

5. Input student scores, number them according to the input sequence, and then sort them in descending order to output the scores of the top ten students. If the number of students is less than ten, only the only student scores will be output.

For example, output: x No. xx min

#include <iostream>

using namespace std;

struct student{
	int score;
	int num;
};

int main(){
	student stu[101],temp;
	int i,j,n;
	cout<<"Please enter the number of students:"<<endl;
	cin>>n;

	for(i=1;i<=n;i++){
		stu[i].num = i;
		cin>>stu[i].score;
	}

	for(i=1;i<n;i++){
		for(j=1;j<=n;j++){
			if(stu[i].score<stu[i+1].score){
				temp = stu[i];
				stu[i] = stu[i+1];
				stu[i+1] = temp;
			}
		}
	}

	if(n>10){n=10;}
	for(i=1;i<=n;i++){
		cout<<stu[i].num<<"number:"<<stu[i].score<<"branch"<<endl;
	}

}

Operation results:

6. Input a positive number less than 1000 from the keyboard and output its square root (if the square root is not a positive number, output other parts). It is required to check whether the data is a positive number less than 1000 after entering it. If not, re-enter is required.

#include <iostream>
#include <cmath>

using namespace std;

int main(){
	
	while(1){
		int n;
		double sum;
		cout<<"Please enter a positive number less than 1000"<<endl;
		cin>>n;
		if(n<0||n>1000){
			cout<<"ERROR!"<<endl;
			cout<<endl;
		}
		else{
			sum = sqrt((double)n);
			cout<<"Its square root is:"<<sum<<endl;
			cout<<endl;
		}
	}
}

Operation results:

7. Input two numbers and output the maximum common divisor and the minimum common multiple.

#include <iostream>

using namespace std;

int main(){
	int m,n,i,j,max,min;
	cin>>m>>n;
	i = m;
	j = n;
	while(1){
		if(i%m==0 && i%n==0){
			min = i;
			break;
		}
		i += m;
	}
	while(1){
		if(m%j==0 && n%j==0){
			max = j;
			break;
		}
		j--;
	}
	cout<<"greatest common divisor:"<<max<<endl;
	cout<<"Minimum common multiple:"<<min<<endl;
	return 0;
}

Operation results:

8. If a number is exactly equal to the sum of its factors, it is called "perfect number". For example, the factor of 6 is 1,2,3 and 6 = 1 + 2 + 3. Write a program to find all perfect numbers within 1000.

Output in the following format: 6 its factors are 1 2 3

#include <iostream>

using namespace std;

int main(){
	int i, j, sum;      

	for(i=2;i<=1000;i++){
		sum = 1;
		for(j=2;j<=i/2;j++)
			if(i%j==0)
				sum += j;
		if(sum == i){
			cout<<sum<<" its factors are 1 ";
			for(j=2;j<=i/2;j++)
				if(i%j==0)
					cout<<j<<" ";
			cout<<endl;
		}
	}
	return 0;
}

Operation results:

9. Write a simple calculator with the functions of addition, subtraction, multiplication, division and exit.

#include<iostream>
using namespace std;
int main()
{
	int num1,num2,n,sign=0;
	float sum=0;
	while(1)
	{
		cout<<"Please enter the command, 1-Plus, 2-Minus, 3-Multiply, 4-Except, 5-sign out"<<endl;
		cin>>n;
		sum=0;
		switch(n) 
		{
		case 1:cin>>num1>>num2;sum=num1+num2;break;
		case 2:cin>>num1>>num2;sum=num1-num2;break;
		case 3:cin>>num1>>num2;sum=num1*num2;break;
		case 4:cin>>num1>>num2;sum=num1/num2;break;
		case 5:sign=1;break;
		}
		if(sign)
		{
			break;
		}
		cout<<sum<<endl;
	}		
}

Operation results:

Posted by Mantis_61 on Sun, 28 Nov 2021 05:31:50 -0800