1. 求和输入的数的范围
#include <iostream>
using namespace std;
int main()
{
using namespace std;
int sum = 0;
int min_num = 0;
int max_num = 0;
cout << "Enter the min num: ";
cin >> min_num;
cout << "Enter the max num: ";
cin >> max_num;
for (int i = min_num; i <= max_num; i++)
sum += i;
cout << "The sum of the num in " << min_num
<< "-" << max_num << " is " << sum << endl;
return 0;
}
2. 使用long double 和 array 编写100!
#include <iostream>
#include <array>
const int ArSize = 101;
using namespace std;
int main()
{
array<long double, ArSize> factorials;
factorials[1] = factorials[0] = 1.0;
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;
return 0;
}
3. 输入数字,准时求和,输入0结束
#include <iostream>
using namespace std;
int main()
{
int sum = 0;
int num = 0;
cout << "Enter a num: ";
cin >> num;
while (num)
{
sum += num;
cout << "The sum of num is " << sum << endl;
cout << "Enter a num: ";
cin >> num;
}
return 0;
}
4. 投资利润
#include <iostream>
const int money = 100;
using namespace std;
int main()
{
double daphne_money = money;
double cleo_money = money;
int year = 1;
while (cleo_money <= daphne_money)
{
daphne_money += money * 0.1;
cleo_money += cleo_money * 0.05;
++year;
}
cout << year << " year, " << "cleo: " << cleo_money
<< " daphne: " << daphne_money << endl;
return 0;
}
5. 12个月的销售额,输入并打印出来
#include <iostream>
const int months = 12;
using namespace std;
int main()
{
string month_name[months] =
{
"January", "February", "March", "April", "May", "June",
"July", "August", "September", "October", "November", "December"
};
int sale[months];
for (int i = 0; i < months; i++)
{
cout << "Enter the sale for " << month_name[i] << ": ";
cin >> sale[i];
}
for (int i = 0; i < months; i++)
{
cout << month_name[i] << ": " << sale[i] << endl;
}
return 0;
}
6. 第五题变为完成三年的销售额
#include <iostream>
const int months = 12;
const int years = 3;
using namespace std;
int main()
{
string month_name[months] =
{
"January", "February", "March", "April", "May", "June",
"July", "August", "September", "October", "November", "December"
};
int sale[years][months];
for (int j = 0; j < years; j++)
{
for (int i = 0; i < months; i++)
{
cout << "Enter the " << j << " sale for " << month_name[i] << ": ";
cin >> sale[j][i];
}
}
for (int j = 0; j < years; j++)
{
for (int i = 0; i < months; i++)
{
cout << j << ": " << month_name[i] << ": " << sale[i] << endl;
}
}
return 0;
}
7. 结构体car,存储品牌和年份,输入记录多少种car
#include <iostream>
#include <string>
using namespace std;
struct car
{
string make;
int year;
};
int main()
{
int car_num;
cout << "How many cars do you wish to catalog? ";
(cin >> car_num).get();
car* cars = new car[car_num];
for (int i = 0; i < car_num; i++)
{
cout << "Car #" << i+1 << ":" << endl;
cout << "Please enter the make: ";
getline(cin, cars[i].make);
cout << "Please enter the year made: ";
(cin >> cars[i].year).get();
}
cout << "Here is your collection:" << endl;
for (int i = 0; i < car_num; i++)
{
cout << cars[i].year << " " << cars[i].make << endl;
}
delete[] cars;
return 0;
}
8. 输入多个单词以done结束,统计总共多少个
#include <iostream>
using namespace std;
const int word_num = 20;
int main()
{
char word[word_num];
int sum = 0;
cout << "Enter words (to stop, type the word done):" << endl;
(cin >> word).get();
while (strcmp(word, "done"))
{
sum++;
(cin >> word).get();
}
cout << "You entered a total of " << sum << " words.";
return 0;
}
9. 使用string处理第八题
#include <iostream>
#include <string>
using namespace std;
const int word_num = 20;
int main()
{
string word;
int sum = 0;
cout << "Enter words (to stop, type the word done):" << endl;
(cin >> word).get();
while (word != "done")
{
sum++;
(cin >> word).get();
}
cout << "You entered a total of " << sum << " words.";
return 0;
}
10. 编写程序打印标点和星号
#include <iostream>
using namespace std;
int main()
{
int rows;
cout << "Enter number of row: ";
cin >> rows;
for (int i = 0; i < rows; i++)
{
for (int j = 0; j < rows; j++)
{
if (j < rows - i - 1)
cout << ".";
else
cout << "*";
}
cout << endl;
}
return 0;
}