第一次上机作业

19c20c张定闻
第一题.列出所有的可能

#include<iostream>
using namespace std;
int main() {
	double a, b, c;
	cout << "请依次输入三个实数";
	cin >> a >> b >> c;
	if (a > b && b > c) {
		cout << a << b << c << endl;
	}
	if (c > b && b > a) {
		cout << c << b << a<< endl;
	}
	if (b > c && c > a) {
		cout << b << c << a << endl;
	}
	if (c> a && a > b) {
		cout << c << a << b << endl;
	}
	if (c> b && b > a) {
		cout << c << b << a << endl;
	}
	if (a > c && c> b) {
		cout << a << c<< b << endl;
	}
	if (b > a && a > c) {
		cout << b<< a << c << endl;
	}
	return  0;
}

2首先判定三边是否构成三角形

#include<iostream>
#include<cmath>
using namespace std;
int main() {
	double a, b, c, s,p;
    cout << "请分别输入三角形的三边的值";
	cin >> a >> b >> c;
	p = (a + b + c) / 2;
	if (a + b > c && a + c > b && b + c > a) {
		s = sqrt(p * (p - a) * (p - b) * (p - c));
		cout << s << endl;
	}
	else {
		cout << "输入的三边不构成三角形" << endl;
	}
	return 0;
}

3,水仙花数

#include<iostream>
using namespace std;
int main() {
	cout << "打印所有水仙花数" << endl;
	for (int a = 1; a <= 9; a++) {
		for (int b = 0; b <= 9; b++) {
			for (int c = 0; c <= 9; c++) {
				if (a * 100 + b * 10 + c == a * a * a + b * b * b + c * c * c) {
					cout << a * 100 + b * 10 + c << endl;
				}
			}
		}
	}
	return 0;
}

4运用数列求和公式

#include<iostream>
using namespace std;
int main() {
	cout << "请输入一个数" << endl;
	int N;
	cin >> N;
	while (N > 0) {
		cout << N * (N + 1) / 2;
		cin >> N;

	}
	return 0;

}

5用两个字母表示出行列数然后寻找规律

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

6正数及其平均数

#include<iostream>
using namespace std;
int main() {
	int a, b=0, s=0;

	for ( int x = 1; x <= 10; x++) {
		cin >> a;
		if (a >= 0) {
			b ++;
			s += a;
		}
	}
	cout << "b=" <<b<< endl;
	cout << "s=" <<s/10<< endl;
	return 0;
}

7用if语句限制area

#include<iostream>
using namespace std;
int main() {
	double area;
	
	for (int radius = 1; radius <= 10;radius++) {
		area = 3.14 * radius * radius;
		if (area <= 100) {
			cout << area << endl;
		}
	}
	return 0;
}

8运用for语句,if语句,如果一个数无法被2到二分之一次方之中的数整除那么他就是一个素数

#include<iostream>
#include<cmath>
using namespace std;
int main() {
	int m;
	int c = 1;
	cout << "输入一个数" << endl;
	cin >> m;
	for (int n = 2; n <= sqrt(m); n++) {
		if (m % n != 0) continue;
		else {
			c = 0;
			cout << "no" << endl;
		}break;
	}
	if (c)
		cout << "yes" << endl;
	return 0;

}

9通过8的原理得出所有素数最后制表使其每行输出5个

#include<iostream>
#include<cmath>
using namespace std;
int main() {
    int c = 1;
    for (int m= 100; m <= 200; ++m) {
        
            int n= 1;
            for (int i = 2; i <= sqrt(m); ++i) {
                if (m % i == 0) {
                    n= 0;
                    break;
                }
            }
            if (n) {
                if (c == 5) {
                    cout << m<< endl;
                    c = 1;
                }
                else {
                    cout << m << " ";
                    c++;
                }
            }
    }
    return 0;
}


10使其不满足条件的输出" "满足条件的输出*

#include<iostream>
using namespace std;
int main() {
	cout << "请输入一个正整数" << endl;
	int n;
	cin >> n;
        for (int i = 1; i <= n; i++) {
            for (int j = n - i - 1; j >= 0; j++) {
                cout << " ";
            }
            for (int j = 1; j <= 2 * i - 1; j++) {
                cout << "*";
            }
            cout << endl;
        }
        for (int i = n - 1; i >= 1; i--) {
            for (int j = 0; j <= n - i - 1; j++) {
                cout << " ";
            }
            for (int j = 2 * i - 1; j >= 1; j--) {
                cout << "*";
            }
            cout << endl;
        }
        return 0;
    }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值