第三章 C++简单题目

实验练习1.直角三角形

#include <iostream>
using namespace std;
bool rightangle(int x,int y,int z){
	if (x*x + y*y == z*z || x*x + z*z == y*y || y*y + z*z == x*x)
		return 1;
	else return 0;
}
int main() {
	int x, y, z,t;
	
	while (cin >> x >> y >> z) {
		t = rightangle(x, y, z);
		if (x > 1000 || x < 1 || y>1000 || y < 1 || z>1000 || z < 1)
			cout << "The edges is out of boundry!" << endl;
		else if (t == 1) {
			cout << "True" << endl;
		}
		else cout << "False" << endl;
	}
	return 0;
}

实验练习2判丑数

只包含因子2,3,5的正整数被称作丑数,比如4,10,12都是丑数,而7,23,111则不是丑数,另外1也不是丑数。请编写一个函数,输入一个整数n,能够判断该整数是否为丑数,如果是,则输出True,否则输出False。

#include<iostream>
using namespace std;
bool ugly(int num) {
	if (num == 1)
		return false;
	else {
		while (num % 2 == 0)
			num /= 2;
		while (num % 3 == 0)
			num /= 3;
		while (num % 5 == 0)
			num /= 5;
		return num == 1 ? true : false;
	}
}
	int main() {
		int n, t;
		
		while (cin >> n) {
			t = ugly(n);
			if (n > 1000000 || n < 1)
				cout << "Error" << endl;
			else if (t == 1) {
				cout << "True" << endl;
			}
			else
				cout << "False" << endl;
		}
		return 0;
	}

猜你喜欢

转载自blog.csdn.net/lyc44813418/article/details/85936387