上課教材 試著列印出”Hello, C++”! 1 2 3 4 5 6 7 8 9 10 #include <iostream> using namespace std; int main () { cout << "Hello, C++." << endl; return 0 ; }
輸入一個半徑,請問圓面積等於多少? 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 #include <iostream> using namespace std; int main () { int r; cin >> r; double A A = r * r * 3.14 ; cout << A << endl; return 0 ; }
假設年初投入金額123元,年末賺到4元,問年報酬率是多少%? 1 2 3 4 5 6 7 8 9 10 11 #include <iostream> using namespace std; int main () { int amount = 123 , profit = 4 ; double return_rate = (double ) profit / amount; cout << "Return rate(%)=" << return_rate * 100 <<endl; return 0 ; }
Body Mass Index(BMI) 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 using namespace std; int main () { int name ; double BMI, weight, height ; cout << "輸入姓名" << endl ; cin >> name >> endl ; cout << "輸入身高(公分)" << endl ; cin >> height >> endl ; cout << "輸入體重(公斤)" << endl ; cin >> weight >> endl ; height=height/100 ; BMI=weight/(height*height); cout << name << BMI << endl ; return 0 ; }
輸入一個半徑,先判斷半徑是否大於0,在執行圓面積等於多少,若小於、等於0則貼出”Not a circle. “ 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 #include <iostream> using namespace std; int main () { int r ; cin >> r ; if (r>0 ){ double A = r*r*3.14 ; cout << A << endl ; } else { cout << "Not a circle. " << endl; } return 0 ; }
飲料販賣機** 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 #include <iostream> using namespace std; int main () { int choice; cin >> choice; int coin; cin >> coin; int price; switch (choice){ case 1 : price = 25 ; break ; case 2 : price = 30 ; break ; case 3 : price = 50 ; break ; } if (coin>=price){ cout << "You got what you want with the changes $" << coin-price <<". " <<endl ; }else { cout <<"Insent more coin. " << endl; } return 0 ; }
隨機亂數 1 2 3 4 5 6 7 8 9 10 #include <iostream> #include <cstdlib> #include <ctime> using namespace std; int main () { srand (time (0 )); cout << rand ()%6 +1 <<" " ; return 0 ; }
輾轉相除法 1 2 3 4 5 6 7 8 9 10 11 12 13 14 #include <iostream> using namespace std; int main () { int a =54 , b=32 ; int r; do { r=a%b; a=b; b=r; }while (r>0 ); cout<<a<<endl; return 0 ; }
1+2+3+…+100=? 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 #include <iostream> using namespace std; int main () { int s=0 ; for (int i=1 ;i<=100 ;i++){ s=s+i; } cout<<s<<endl; return 0 ; }
版權聲明: 此文章版權歸che哲所有,可轉載,禁止營利