第一次上机作业

第一道题:首先输入三个数字,然后用if语句,把所有的情况列出来并输出。

#include <iostream>
using namespace std;
int main()
{
	int a, b, c;
	cin >> a >> b >> c;
	if (a > b) {
		if (b > c) { cout << a << "   " << b << "   " << c << endl; }
		else if (a > c) { cout << a << "   " << c << "   " << b; }
		else { cout << c << "   " << a << "   " << b; }
	}
	if (a < b) {
		if (a > c) { cout << b << "   " << a << "   " << c << endl; }
		else if (b > c) { cout << b << "   " << c << "   " << a; }
		else { cout << c<< "   "<< b<<"  " <<  a; }
		
	}
	
	return 0;
}

第二道题:根据海伦公式,要用到开平方根,把值定义好,按顺序输出

#include<iostream>
using namespace std;
#include "math.h";
#include "cmath";
int main() {
	int a, b, c, p, s, q;
	cin >> a >> b >> c;
	p = (a + b + c) / 2;
	q = p * (p - a) * (p - b) * (p - c);
	s = sqrt(q);
	if (s <= 0)
	{
		cout << "三角形不存在" << endl;
	}
	else { cout << "三角形的面积是" << s; }
	
	return 0;
}

第三道题:水仙花。
关键在于知道如何表达个位数,十位数和百位数。

#include <iostream>
using namespace std;
int main() {
	int num = 100;
	do {
		int a = num % 10;
		int b = num / 10 % 10;
		int c = num / 100;
		if (a * a * a + b * b * b + c * c * c == num) {
			cout << num << endl;
			num++;

		}
		else { num++; }
	} while (num < 1000);
	return 0;
}

第四道题:do while 循环,“1”是真,可以无限循环。嵌入if,直到break 可以退出。

#include<iostream>
using namespace std;
int main() {
	int n;
	int sum = 0;
	do
	{
		cin >> n;
		if (n >= 0) {
			int i;
			for (i = 1; i <= n; ++i) {
				sum += i;
				
			}cout << sum << endl;

		}
		else {
			cout << "false";
			break;
		}
	} while (1);
	return 0;
}

第五道题:\t可以把数对齐。弄清楚每一列是哪些数,每一行是哪些数,数的关系

#include <iostream>
using namespace std;
int main() {
	int a, b, c;
	int i = 1;
	for (b = 1; b <= 4; b++) {
		for (a = 1; a <= 5; a++)
		{
			c = a * i;
			cout << c << "\t";
		}
		i++;
		cout << endl;
	}
	return 0;
}

第六道题:穷举法。

#include <iostream>
using namespace std;
int main() {
	int a = 0;
	int b = 0;
	int c = 0;
	int d = 0;
	int e = 0; 
	int f = 0;
	int g = 0;
	int h = 0;
	int i = 0;
	int j = 0;
	cin>> a>>b>>c>>d>>e>> f>> g>> h>>i>> j;
int sum = 0;
int m = 0;
if (a > 0)
sum += 1;
m+=a;
if (b > 0)
sum += 1;
m +=b;
if (c > 0)
sum += 1;
m +=c;
if (d > 0)
sum += 1;
m +=d;
if (e > 0)
sum += 1;
m +=e;
if (f > 0)
sum += 1;
m +=f;
if (g > 0)
sum += 1;
m +=g;
if (h > 0)
m +=h;
sum += 1;
if (i > 0)
sum += 1; 
m +=i;
if (j > 0)
sum += 1;
m +=j;
cout<< sum<<endl<<m/10;

	return 0;
}

第七道题:定义pi,

#include <iostream>

using namespace std;
int main() {
	int r;
	double pi = 3.142;
	float rad;

	for (r = 1; r <= 10; r += 1) {
		rad = pi*r*r ;
		if (rad > 100) break;
		cout << rad<<endl;
	}
	return 0;
}

第八题:

#include <iostream>
#include <cmath>
using namespace std;
int main()
{
	int a, b, i;
	int flag = 0;
	cin >> a;
	b = sqrt(a);
	for (i = 2; i <= b; ++i)
	{

		if (a % i == 0) {
			flag = 1;
			break;
		}
}
	if (flag == 1) {
		cout << "不是质数";
	}
	else cout << "是质数";


	return 0;
}

第九题:

#include<iostream>
#include<cmath>
using namespace std;
int main()
{
	int i, j, flag = 1, n = 0;
	for (i = 101; i < 200; i++)
	{
		flag = 1;
		for (j = 2; j <= sqrt(200); j++)
			if (i % j == 0)
			{
				flag = 0;
				break;
			}
		if (flag == 1)

		{
			n++;
			cout << i << " ";
			if (n == 5)
			{
				cout << endl;
				n = 0;
			}
		}
	}
	cout << endl;
	return 0;
}

第十题:

#include <iostream>
using namespace std;
int main()
{
	int n;
	cout << "请输入一个数字" << endl;
	cin >> n;

	for (int i = 1; i <= n; i++)
	{
		for (int j = 1; j <= 2 * i - 1; j++)

			cout << "*";
		cout << endl;
	}
	for (int m =  n ; m >= 1; --m)
	{
		for (int h = 1; h <=2*m-3; h++)
			cout << "*";
		cout << endl;
	}
		return 0;

}
  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值