//C++语言的if/else判断语句 #include#include 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#include #include #include using namespace std; int main() { vector 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"); }
#includeusing 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//第一种图形 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 //第二种图形 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 //第三种图 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"); }