1、
代码
#include<iostream> int main() { using namespace std; int num1; int num2; int total=0; cout << "请输入开始数字\n"; cin >> num1; cout << "请输入结束数字\n"; cin >> num2; for (num1; num1<=num2; num1++) total = num1 + total; cout << num1 << " 和 " << num2 << "之间的整数和为 " << total <<endl; return 0; }
运行结果
2、
代码
#include<iostream> int main() { using namespace std; double total = 0.0; double in; cout << "请输入数字:"; cin >> in; while (in != 0) { total += in; cout << "所有输入数的和为:" << total << "\n"; cout << "请输入下一个数字:"; cin >> in; } cout << "运行结束"; return 0; }
运行结果
3、
代码
#include<iostream> int main() { using namespace std; double daphne=100; double cleo=100; int year=1; while (daphne >= cleo) { daphne += 100*0.1; cleo += cleo*0.05; cout << "第" << year << "年,daphne投资价值为 " << daphne << "cleo投资价值为 " << cleo <<endl; year++; } cout << "第 " << year << "年,时cleo超过daphne的投资价值"<< endl; return 0; }
运行结果
4、
代码
#include<iostream> const char *months[12] = {"一月","二月","三月","四月","五月","六月","七月","八月","九月","十月","十一月","十二月"}; int main() { using namespace std; int sales[12]; int total; for (int i=1; i<=12; i++) { cout << "请输入" << months[i-1] << "销售数量:"; cin >> sales[i-1]; } for (int j=0; j<12; j++) { total = total+sales[j]; } cout << "总销售为:" << total <<endl; return 0; }
运行结果
5、
代码
#include<iostream> const char *months[12] = {"一月","二月","三月","四月","五月","六月","七月","八月","九月","十月","十一月","十二月"}; int main() { using namespace std; int sales[3][12]; int total[3] = {0}; //一定要初始化。不然初始值不为0 int sum; for (int a=1;a<=3;a++) { for (int i=1; i<=12; i++) { cout << "请输入第"<< a << "年" << months[i-1] << "销售数量:"; cin >> sales[a-1][i-1]; } } for (int b=0; b<3; b++) { for (int j=0; j<12; j++) { total[b] = total[b]+sales[b][j]; } sum = sum + total[b]; cout << "第" << b+1 << "年总销量为" << total[b] <<endl; } cout << "总销售为:" << sum <<endl; return 0; }
运行结果
6、
代码
<pre name="code" class="cpp">//一定要加while (cin.get() != '\n'); #include<iostream> using namespace std; const int LEN = 60; struct Car { char brand[LEN]; int year; }; int main() { int num; cout << "How many cars do you wish to catalog?"; cin >> num; while (cin.get() != '\n'); Car *ps = new Car[num]; for (int i=0;i<num;i++) { cout << "Car #" << (i+1) << ":\n"; cout << "Please enter the make:"; cin.getline(ps[i].brand, LEN); cout << "Please enter the year made:"; cin >> ps[i].year; while(cin.get() != '\n'); } cout << "Here is your collection:\n"; for (int i=0; i<num; i++) { cout << ps[i].year << " " << ps[i].brand <<endl; } delete [] ps; return 0; }
运行结果
7、
代码
#include<iostream> #include<cstring> const int STR_LEN=60; int main() { using namespace std; char words[STR_LEN]; int count=0; cout << "Enter words(to stop,type the word done):\n"; while (cin >> words && strcmp("done", words)) ++count; cout << "You entered a total of " << count << " words .\n" <<endl; return 0; }
运行结果
8、
代码
#include<iostream> #include<string> int main() { using namespace std; string words; int count=0; cout << "Enter words {to stop,type the word done}:\n"; while (cin >> words && words != "done") ++count; cout << "You entered a total of " << count << " words .\n"; return 0; }
运行结果
9、
代码
#include<iostream> int main() { using namespace std; int row; cout << "Enter number of row:"; cin >> row; for (int i=0; i<row; i++) { for (int j=row-i; j>1; j--) { cout << "."; } for (int k=0; k<=i; k++) { cout << "*"; } cout << "\n"; } return 0; }
运行结果
时间: 2024-11-10 01:23:03