50道基础编程题

本文列举了50道基础编程题目,涵盖数值计算、数组操作、字符串处理、逻辑判断等多个方面,旨在帮助初学者巩固编程基础。
摘要由CSDN通过智能技术生成

1、输入3个数,求最大值

int main() 
{
    int a,b,c,m; 
  cin>>a>>b>>c; 
  m=a; 
  if(b>m) m=b; 
  if(c>m) m=c; 
  cout<<m; 
} 

2、编程序,求方程ax2+bx+c=0的根

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


int main()
{
   
	double a, b, c, d, x1, x2;
	cin >> a >> b >> c;
	if (a == 0)
		if (b == 0) cout << "error\n";
		else cout << "x=" << -c / b << endl;
	else
	{
   
		d = b * b - 4 * a*c;
		if (fabs(d) <= 1e-6)
			cout << "x1=x2=" << -b / (2 * a) << endl;
		else if (d > 1e-6)
		{
   
			x1 = (-b + sqrt(d)) / (2 * a);
			x2 = (-b - sqrt(d)) / (2 * a);
			cout << "x1=" << x1 << ",x2=" << x2 << endl;
		}
		else cout << "方程无实根\n";
	}
	return 0;
}

3、输入一个成绩,打印相应的等级

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

int main()
{
   
	int score;
	cin >> score;
	switch (score / 10)
	{
   
	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;
	}
	return 0;
}

4、输入3个double类型的值,判断这3个值是否可以表示一个三角形的三条边。

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

int main()
{
   
	double a, b, c;
	cin >> a >> b >> c;
	if (a + b > c&&a + c > b&&b + c > a)
		cout << "可以构成三角形" << endl;
	else
		cout << "不能构成三角形" << endl;
	return 0;
}

5、输入20个数,求其最大、最小和平均值

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

int main()
{
   
	int a[20];
	double sum = 0;
	for (int i = 0; i < 20; i++)
	{
   
		cout << i + 1 << " ";
		cin >> a[i];
		sum += a[i];
	}
	sort(a, a + 20);
	cout << "max=" << a[19] << endl;
	cout << "min=" << a[0] << endl;
	cout << "average=" << sum / 20 << endl;
	return 0;
}

6、输入若干个数,设输入的第一个数为后面要输入的数的个数,求平均值及最大值。

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

int main()
{
   
	int n, max, temp;
	double sum = 0;
	cin >> n;
	cin >> temp;
	max = temp;
	sum = temp;
	for (int i = 1; i < n; i++)
	{
   
		cin >> temp;
		if (temp > max)
			max = temp;
		sum += temp;
	}
	cout << "平均值:" << double(sum) / n << endl;
	cout << "最大值:" << max << endl;
	return 0;
}

7、输入若干个数,输入-999表示结束,求平均值及最大值。

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

int main()
{
   
	int n, cnt = 0;
	double max, sum = 0;
	cin >> n;
	if (n != -999)
	{
   
		sum = n;
		max = n;
		cnt++;
	}
	while (cin >> n)
	{
   
		if (n == -999)
			break;
		if (n > max)
			max = n;
		sum += n;
		cnt++;
	}
	cout << "最大值:" << max << endl;
	cout << "平均值:" << sum /double(cnt) << endl;
	return 0;
}

8、求和 s=11 + 22 + 33 +…+ 100100

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

int main()
{
   
	double sum = 0;
	for (int i = 1; i <= 100; i++)
	{
   
		sum += i * i;
	}
	cout << sum << endl;
	return 0;
}

9、印度国王的奖励在这里插入图片描述

int main() 
{
    double t=1,s=0; 
  for(int i=0; i<=63; i++) 
  {
    s=s+t; 
    t=2*t; 
  } 
  cout<<s/1.4e8<<endl; 
}  

10、求和 s=1! + 2! + 3! +…+ 10!

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

int fun(int n)
{
   
	if (n == 1)
		return 1;
	else
		return n * fun(n - 1);
}
int main()
{
   
	long sum = 0;
	for (int i = 1; i <= 10; i++)
		sum += fun(i);
	cout << sum << endl;
	return 0;
}

11、求 e=1 + 1/1! + 1/2! + 1/3! + …(前20项)

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

int fun(int n)
{
   
	if (n == 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值