您好,登錄后才能下訂單哦!
//C++語言的if/else判斷語句 #include<iostream> #include<string> using namespace std; int main() { char user_ans; cout << "現在正在下雨嗎Y/N?" << endl; cin >> user_ans; if ('Y' == user_ans || 'y' == user_ans) { cout << "現在正在下雨" << endl; } else { cout << "現在沒有下雨" << endl; cout << "今天會有太陽嗎?" << endl; cin >> user_ans; if ('Y' == user_ans || 'y' == user_ans) { cout << "今天是晴天" << endl; } else cout << "今天是陰天" << endl; } system("pause"); }
#include<iostream> #include<vector> #include<string> #include<iomanip> using namespace std; int main() { vector<int>numb;//使用vector bool u_agg = true; int cnt = 1; int buffer; float avg; char u_ans; while (u_agg) { cout << "第" << cnt << "個數字:\n"; cin >> buffer; numb.push_back(buffer);//vector的push_back() cout << "完成沒有?Y/N\n"; cin >> u_ans; if ('N' == u_ans || 'n' == u_ans) { cnt++; continue; } else { u_agg = false; } } for (int i = 0; i < numb.size() - 2; i++)//size() //這段目的可看出來,輸入相鄰的兩個數相加除以3 { int sum = 0; for (int j = i; j < i + 2; j++) sum = sum + numb[j]; avg = float(sum) / 3; cout << setiosflags(ios::fixed) << setprecision(2) << avg << " \n"; } system("pause"); }
#include<iostream> using namespace std; unsigned long int cnt(int n)//計算n的從0和n的每個數的 階乘之和 { unsigned long int sum = 0; for (int i = 1; i<=n; i++) { unsigned long int mul = 1; for (int j = 1; j <= i; j++) mul *= j; sum += mul; } return sum; } bool pass(int n)//判斷輸入的n是否在0和25之間 { if (n >= 0 && n <= 25) return true; else { cout << "n must between 0 and 25!\n"; return false; } } int main() { int n; unsigned long int sum; cout << "enter the number n:"; cin >> n; while (pass(n)) { sum = cnt(n); cout << sum; break; } system("pause"); }
#include<iostream>//第一種圖形 using namespace std; int main() { int j = 0; for (int i = 1; i <= 9; i++) { for (int k = 1; k <= i; k++) if ((k + j) % 9) cout << (k + j) % 9 << " "; else cout << 9 << " "; cout << endl; j = j + i; } system("pause"); } #include<iostream>//第二種圖形 using namespace std; int main() { int j = 0; for (int i = 1; i <= 9; i++) { for (int m = 0; m < 9 - i; m++) cout << " "; for (int k = 0; k <= i; k++) { if ((k + j) % 9) cout << (k + j) % 9 << " "; else cout << 9 << " "; } cout << endl; j = j + i; } system("pause"); } #include<iostream>//第三種圖 using namespace std; int main() { int j = 0; for (int i = 1; i <= 9; i++) { for (int k = 1; k <= 8; k++) if ((k + j) % 9) cout << (k + j) % 9 << " "; else cout << 9 << " "; cout << endl; j = j + 8; } system("pause"); }
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。