1
#include <iostream> int main() { using namespace std; int num1, num2; cout << "请输入第一个整数: "; cin >> num1; cout << "请输入第二个整数: "; cin >> num2; int tot = 0; for (int i = num1; i <= num2; i++) { tot += i; } cout << num1 << " - " << num2 << " 之间所有整数的和为 " << tot << endl; system("pause"); return 0; }
2
#include <iostream> #include <array> const int ArSize = 101; int main() { using namespace std; array<long double, ArSize> factorials; factorials[1] = factorials[0] = 1; for (int i = 2; i < ArSize; i++) factorials[i] = i * factorials[i - 1]; for (int i = 0; i < ArSize; i++) cout << i << "! = " << factorials[i] << endl; system("pause"); return 0; }
3
#include <iostream> int main() { using namespace std; double num; double tot = 0; do { cout << "请输入数字, 程序会计算你所输入数字的累计和[0结束]: "; cin >> num; tot += num; cout << "总计: " << tot << endl; } while (num != 0); cout << "结束退出." << endl; system("pause"); return 0; }
4
#include <iostream> int main() { using namespace std; double Daphne = 100; double Cleo = 100; int year = 0; do { Daphne += 100 * 0.1; Cleo += Cleo * 0.05; year++; } while (Daphne >= Cleo); cout << year << " 年后 Cleo 的投资价值超过 Daphne.\n"; cout << "Daphne 现有投资总额: " << Daphne << endl; cout << "Cleo 现有投资总额: " << Cleo << endl; system("pause"); return 0; }
5
#include <iostream> #include <string> int main() { using namespace std; const int SIZE = 12; string Month[SIZE] = { "一月","二月","三月","四月","五月","六月","七月","八月","九月","十月","十一月","十二月" }; int sales[SIZE]; int tot = 0; cout << "请输入《C++ For Fools》书籍一年内每个月的销售量\n"; for (int i = 0; i < SIZE; i++) { cout << "请输入" << Month[i] << "的销售量: "; cin >> sales[i]; tot += sales[i]; } cout << "《C++ For Fools》书籍一年内每个月的销售情况: \n"; for (int i = 0; i < SIZE; i++) { cout << "第" << Month[i] << "的销售量: " << sales[i] << endl; } cout << "今年的销售总量: " << tot << endl; system("pause"); return 0; }
6
#include <iostream> #include <string> int main() { using namespace std; const int SIZE = 12; const int size = 3; string Month[SIZE] = { "一月","二月","三月","四月","五月","六月","七月","八月","九月","十月","十一月","十二月" }; string Year[size] = { "一年","二年","三年" }; int sales[size][SIZE]; int tot[size] = { 0,0,0 }; cout << "请输入《C++ For Fools》书籍三年内每个月的销售量\n"; for (int j = 0; j < 3; j++) { cout << "请输入第" << Year[j] << "的销售量: \n"; for (int i = 0; i < SIZE; i++) { cout << "请输入" << Month[i] << "的销售量: "; cin >> sales[j][i]; tot[j] += sales[j][i]; } } cout << "《C++ For Fools》书籍每年的销售情况: \n"; for (int j = 0; j < size; j++) { cout << "第" << Year[j] << "的销售量: " << tot[j] << endl; } cout << "三年的销售总量: " << tot[0] + tot[1] + tot[2] << endl; system("pause"); return 0; }
7
#include <iostream> #include <string> using namespace std; struct car { string make; int year; }; int main() { int num; cout << "你要输入多少辆汽车: "; cin >> num; car *Car = new car[num]; for (int i = 0; i < num; i++) { cin.get(); //丢弃输入回车符 cout << "汽车 " << i + 1 << ": " << endl; cout << "请输入汽车制造厂商: "; getline(cin, Car[i].make); cout << "请输入汽车制造年份: "; cin >> Car[i].year; } cout << "这是你的汽车: \n"; for (int i = 0; i < num; i++) { cout << Car[i].year << " " << Car[i].make << endl; } system("pause"); return 0; }
8
#include <iostream> #include <cstring> int main() { using namespace std; const int size = 20; char word[size]; int i; cout << "请输入单词[输入done结束]: \n"; cin >> word; for (i = 0; strcmp(word, "done"); i++) cin >> word; cout << "你总共输入了 " << i << " 个单词.\n"; system("pause"); return 0; }
9
#include <iostream> #include <string> int main() { using namespace std; string word; int i; cout << "请输入单词[输入done结束]: \n"; cin >> word; for (i = 0; word != "done"; i++) cin >> word; cout << "你总共输入了 " << i << " 个单词.\n"; system("pause"); return 0; }
10
#include <iostream> int main() { using namespace std; int num; cout << "请输入行数: "; cin >> num; for (int i = 1; i <= num; i++) { for (int j = 0; j < num - i; j++) cout << " "; for (int j = 0; j < i; j++) cout << "*"; cout.put('\n'); } system("pause"); return 0; }