基礎プログラミング復習用課題 解答例 —― 第 12 回 6 月 26 日 —― <問題 1> #include<iostream> #include<cmath> using namespace std; int main(){ double x; cout << "Input an integer : "; cin >> x; cout << "e^" << x << " = " << exp(x) << endl; return 0; } <問題 2> #include<iostream> #include<cmath> using namespace std; int main(){ double x, y; cout << "Input two integers : "; cin >> x >> y; cout << x << "^" << y << " = " << pow(x, y) << endl; return 0; } <問題 3> #include<iostream> #include<cmath> using namespace std; int main(){ double ax, ay, bx, by; cout << "Input A(ax, ay) : "; cin >> ax >> ay; cout << "Input B(bx, by) : "; cin >> bx >> by; cout << "The distance from A to B : " << sqrt(pow(bx-ax,2)+pow(by-ay,2)) << endl; return 0; } <問題 4> #include<iostream> #include<cmath> using namespace std; int main(){ int n; bool prime = true; cout << "Input an integer : "; cin >> n; if(n!=2){ for(int i=2; i<=sqrt(n); i++){ if(n%i==0){ prime = false; break; } } } if(prime == true){ cout << n << " is prime." << endl; } else{ cout << n << " is not prime." << endl; } return 0; } <問題 5> #include<iostream> #include<cmath> using namespace std; int main(){ double x, y; cout << "Input two real numbers : "; cin >> x >> y; cout << "|" << x << "| + |" << y << "| = " << fabs(x)+fabs(y) << endl; return 0; } <問題 6> #include<iostream> #include<cmath> using namespace std; int main(){ double x, y; cout << "Input two real numbers : "; cin >> x >> y; cout << fmax(x,y) << " >= " << fmin(x,y) << endl; return 0; } <問題 7> #include<iostream> #include<cmath> using namespace std; int main(){ double x, y, z; cout << "Input three real numbers : "; cin >> x >> y >> z; cout << "Max is " << fmax(fmax(x,y),z) << endl; cout << "Min is " << fmin(fmin(x,y),z) << endl; return 0; } <問題 8> #include<iostream> #include<cmath> using namespace std; int main(){ double x; cout << "Input a real number : "; cin >> x; cout << "Round " << x << " upward : " << ceil(x) << endl; cout << "Round " << x << " downward : " << floor(x) << endl; return 0; } <問題 9> #include<iostream> #include<cmath> using namespace std; int main(){ double x; cout << "Input a degree : "; cin >> x; cout << "sin = " << sin(x*M_PI/180) << endl; cout << "cos = " << cos(x*M_PI/180) << endl; cout << "tan = " << tan(x*M_PI/180) << endl; return 0; } <問題 10> #include<iostream> #include<cstdlib> using namespace std; int main(){ srand(1); //1を他の数字にすると発生する乱数が変わる. for(int i =0; i<10; i++){ cout << rand()%100+1 << " "; } cout << endl; return 0; } <問題 11> #include<iostream> #include<cstdlib> using namespace std; int main(){ srand(1); //1を他の数字にすると発生する乱数が変わる. for(int i =0; i<10; i++){ cout << (char)(rand()%26+'A') << " "; } cout << endl; return 0; }
© Copyright 2024 ExpyDoc