The most basic exercises in C++ programming (1-10), a must for beginners

C++ programming exercises (1-10)
1. Enter 3 numbers and find the maximum value
2. Program and find the roots of the equation ax2+bx+c=0
3. Enter a score and print the corresponding grade
4. Enter 3 Double type value, determine whether these three values ​​​​can represent the three sides of a triangle
5. Enter 20 numbers, find the maximum, minimum and average value
6. Enter several numbers, let the first number entered be the number to be followed. The number of input numbers, find the average and maximum value
7. Enter a number of numbers, enter -999 to indicate the end, find the average and maximum value
8. Sum s=1X1 + 2X2 + 3X3 +...+ 100X100
9 . The reward of the Indian king, the sum of 2 raised to the power of 0 is added to the sum of 2 raised to the power of 63
10. The sum of s=1! + 2! + 3! +...+ 10!

 Xiaobai records the process of learning C++~
 

1. Enter 3 numbers and find the maximum value

#include<iostream>
using namespace std;

int main()
{
    int a, b, c, m;
    cout << "请输入三个数,求出最大值" << endl;
    cin >> a >> b >> c;
    //首先定义一个中间变量m,并将a赋值给m
    m = a;
    if (b > m) //若b大于被赋值的m(就是a),就将b(目前最大)再次赋值给m
    {
        m = b;
    }
    if (c > m) //若c大于被赋值的m(就是b),就将c(目前最大)再次赋值给m,就得到了最大值
    {
        m = c;
    }
    cout << "最大值为:" << m << endl;

    system("pause");
    return 0;

}

 2. Write a program to find the roots of the equation ax2+bx+c=0

#include <iostream>
using namespace std;
#include<cmath>  //使用了根号,必须调一下函数库


//二次项系数等于0就为1次方程,不为0才为二次方程
//达尔塔 有三种情况,分别是大于0,小于0,等于0,需要分开讨论


int main()
{
	double a, b, c,  x1, x2;
	cout << "请输入二元一次方程组二次项系数a" << endl;
	cin >> a;
	cout << "请输入二元一次方程组一次项系数b" << endl;
	cin >> b;
	cout << "请输入二元一次方程组常数项系数c" << endl;
	cin >> c;
	cout << "您输入的方程组为:" << a << "x^2+" << b << "x+" << c << "=0" << endl;

	double d = b * b - 4 * a * c;
	cout << "d=" << d << endl;

	if (a == 0 & b == 0)
	{
		cout << "方程无解" << endl;
	}
	if (a == 0) //变成一次方程,解法不同

	{
		cout << "方程的解为:" << -c / b << endl;
	}
	if (d == 0) //a不为0才是二次方程
	{
		cout << "该方程有一个解" << endl;
		x1 = (-b) / (2 * a);
		cout << "该解为" << x1 << endl;
	}
	if (d < 0)
	{
		cout << "该方程组无解" << endl;
	}
	if (d > 0)
	{
		cout << "该方程有两个解" << endl;
		x1 = (-b + sqrt(d)) / (2 * a);
		x2 = (-b - sqrt(d)) / (2 * a);
		cout << "x1=" << x1 << endl;
		cout << "x2=" << x2 << endl;
	}
	
	system("pause");
	return 0;
}

3. Enter a score and print the corresponding grade.

#include <iostream>
using namespace std;

int main()
{
	double score;
	cout << "请输入您的成绩" << endl;
	cin >> score;
	cout << "您输入的成绩为" << score << endl;

	if (score < 60)
	{
		cout << "不合格" << endl;
	}
	if (60 < score & score < 80)
	{
		cout << "良好" << endl;
	}
	if (score > 80 & score<=100)
	{
		cout << "优秀" << endl;
	}
	if (score > 100)
	{
		cout << "输入错误,请重新输入!" << endl;
	}




	system("pause");
	return 0;
}

 4. Enter three double values ​​and determine whether these three values ​​can represent the three sides of a triangle.

#include<iostream>
using namespace std;

int main()
{
	double a, b, c;
	cout << "请输入三角形的三条边" << endl;
	cin >> a >> b >> c;

	if (a+b>c || a+c>b || b+c>a)
	{
		cout << "可以构成三角形" << endl;
	}

	else 
	{
		cout << "不可以构成三角形" << endl;
	}

	system("pause");
	return 0;
}

5. Enter 20 numbers and find their maximum, minimum and average values.

#include<iostream>
using namespace std;


int main()
{
	double a, minnumber, maxnumber,total;
	cout << "请输入20个数,按回车键结束" << endl;
	cin >> a;//将输入的a值赋给下一行作为初始值
	total = maxnumber = minnumber = a;//初始化的值放在循环外,也是将这三个数初始化
	for (int i = 1; i < 20; i++)
	{
		cin >> a;
		if (maxnumber < a)
		{
			maxnumber = a;
		}
		if (minnumber > a)
		{
			minnumber = a;
		}
		total = total + a;

	}

	cout << "最大数为:" << maxnumber << endl;
	cout << "最小数为:" << minnumber << endl;
	cout << "平均数为:" << total/20 << endl;

	system("pause");
	return 0;
}

 6. Enter a number of numbers. Let the first number entered be the number of numbers to be entered later. Find the average and maximum value.

#include<iostream>
using namespace std;


int main()
{
	int a,max_number,total,number; 

	cout << "请输入要计算数字的个数" << endl;
	cin >> number;
	cin >> a;
	total = max_number = a;//一定要初始化,不然后面没法比较
	for (int i = 1; i < number; i++)
	{
		cin >> a;
		if ( a > max_number)
		{
			max_number = a;
		}
		total = total + a;


	}



	cout << "最大值为:" << max_number << endl; 
	cout << "平均值为:" << total / number << endl;

	system("pause");
	return 0;
}


7. Enter several numbers, enter -999 to indicate the end, and find the average and maximum values.

#include <iostream>
using namespace std;

int main()
{
	int a, max_number, total;
	int count = 0;//计数器设置为0

	cout << "输入若干个数,输入-999代表结束" << endl;
	cin >> a;
	max_number = a;
	total = 0;
	while (a != -999)//当输入的值为-999时,表示结束,不会计算-999。if满足一次条件就会结束
	{

		if (max_number < a)
		{
			max_number = a;
		}
		total = total + a;
		cout << "total=" << total << endl;
		count = count + 1;
		cin >> a;
	}

	double n = total / count;
	cout << "最大值为:" << max_number << endl;
	cout << "平均值为:" << n << endl;




	system("pause");
	return 0;

}

8. Sum s=1X1 + 2X2 + 3X3 +…+ 100X100

#include <iostream>
using namespace std;

int main()
{
	int total=0;
	int temp;

	for (int i = 1; i < 101; i++)
	{
		temp = i * i;
		total += temp;
	}
	cout << "total=" << total << endl;

	system("pause");
	return 0;
}

9. The reward of the Indian king is the sum of 2 raised to the 0th power and added to 2 raised to the 63rd power.

#include <iostream>
#include<cmath>
using namespace std;

int main()
{
	double temp;//一定要定义成double类型,不然数字太大超过范围
	double total=0;
	for (int i = 0; i < 64; i++)
	{
		temp = pow(2,i);//表示2的i次方
		total = total+temp;
	}
	cout << "和为:" << total << endl;
	system("pause");
	return 0;
} 

10. Sum s=1! + 2! + 3! +…+ 10!

#include<iostream>
using namespace std;

int main()
{
	int temp=1;
	int total = 0;

	for (int i = 1; i <= 10; i++)
	{
		temp = temp * i;
		total = total + temp;
	}
	cout << "和为:" << total << endl;

	system("pause");
	return 0;
}

Guess you like

Origin blog.csdn.net/weixin_46324814/article/details/128061661
Recommended