第三章 C++簡單題目
阿新 • • 發佈:2019-01-14
實驗練習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; }