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 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204
| 5.9.1 //计算两个整数之间数的和 #include <iostream> using namespace std;
int main() { int a, b,sum; int c = 0; cin >> a >> b; // a<b; if (a > b) { int t; t = a; a = b; b = t; } while (a != b) { c = c + a; a = a++; } sum = c + b; cout << sum << endl; return 0; } 5.9.2 #include <iostream> using namespace std;
int main() { int a; int sum = 0; do{ cin >> a; sum = sum + a; cout << sum << endl; } while (a != 0); cout << "完成!\n"; return 0; } 5.9.3 #include <iostream> #include <cmath> using namespace std;
const float inst = 0.1; const int money = 100;
int main() { double Dh, Ce; int year=1; do { Dh = inst * money * year; Ce = pow(1.05, year)*money-money; year++; } while (Ce < Dh); cout << Dh << " " << Ce << endl; cout << year << endl; return 0; } 5.9.4 #include <iostream> using namespace std;
int main() { char* month[] = { "一月","二月","三月","四月","五月","六月","七月","八月","九月","十月","十一月","十二月" }; int sale[12]; int i; int sum = 0; for (i=0;i<12;i++) { cout << month[i] << ": "; cin >> sale[i]; } for (i=0;i<12;i++) { sum += sale[i]; } cout << sum << endl; return 0; } 5.9.5 #include <iostream> using namespace std;
int main() { char* time[3][12] = { "一月","二月","三月","四月","五月","六月","七月","八月","九月","十月","十一月","十二月", "一月","二月","三月","四月","五月","六月","七月","八月","九月","十月","十一月","十二月" , "一月","二月","三月","四月","五月","六月","七月","八月","九月","十月","十一月","十二月" }; int sale[3][12]; int i,j; int sum = 0; for (i = 0; i < 3; i++) for(j=0;j<12;j++) { cout << time[i][j] << ": "; cin >> sale[i][j]; } for (i = 0; i < 3; i++) for(j=0;j<12;j++) { sum += sale[i][j]; } cout << sum << endl; return 0; } 5.9.6 #include<iostream> #include <string> using namespace std;
struct car { string producer; int time; };
int main() { int n,i; cout << "How...? "; cin >> n; car * name = new car[n]; // delete [] neme for (i=0;i<n;i++) { cout << "Car #:" << i + 1 << endl; cout << "Make: "; cin.get(); getline(cin, name[i].producer); cout << "Time: "; cin >> name[i].time; } cout << "Here...: \n"; for (int j = 0; j < n; j++) { cout << name[j].time << " " << name[j].producer << '\n'; } delete[] name; return 0; } 5.9.7 #include <iostream> #include <cstring> using namespace std;
int main() { int i=0; char words[10]="0"; cout << "请输入单词: \n"; cin >> words; while (strcmp(words, "done") != 0) { cin >> words; i++; } cout << "\n共输入了" << i << "个单词\n"; return 0; } 5.9.8 #include <iostream> #include <string> using namespace std;
int main() { int i = 0; string words; cout << "请输入单词: \n"; cin >> words; while (!("done"==words)) { cin >> words; i++; } cout << "\n共输入了" << i << "个单词\n"; return 0; } 5.9.9 #include<iostream> using namespace std;
int main() { int n,i,j; cout << "Enter number of rows: "; cin >> n; for (i = 1; i <= n; i++) { for (j = 1; j <=n-i; j++) cout << "."; for (j=1;j<=i;j++) { cout <<"*"; } cout << endl; } return 0; }
|