1. 第1题
#include <iostream>
using namespace std;
int main()
{
int n1 = 0;//第一个数
int n2 = 0;//第二个数
int total = 0;//和
cout << "请输入2个整数:";
cin >> n1 >> n2;
for (int i = n1; i <= n2; i++)
{
total = total + i;
}
cout << n1 << "~" << n2 << "之间的所有整数和为:" << total << endl;
return 0;
}
这里要注意的是for循环的边界,因为是包含这两个整数的,所以从n1开始,到n2结束,因此要用i<=n2
结果图:
2. 第2题
#include<iostream>
#include<array>
using namespace std;
const int ArSize = 101;//0~100
int main()
{
array<long double, ArSize> a1;
a1[1] = a1[0] = 1;
for (int i = 2; i < ArSize; i++)
{
a1[i] = a1[i - 1] * i;
}
for (int i = 0; i < ArSize; i++)
{
cout << i << "! = " << a1[i] << endl;//逐个显示
}
return 0;
}
这里要注意的:
- 使用了array模板,所以要添加相应的头文件。
- 要计算100!,因为0!也要算进去,所以ArSize应该设置为101。
结果图:
3. 第3题
#include<iostream>
using namespace std;
int main()
{
int num = 0;//输入的数字
int total = 0;//和
cout << "请输入数字:";
cin >> num;
while (num != 0)
{
total = total + num;
cout << "当前累计和为:" << total << endl;
cout << "请输入数字:";
cin >> num;
}
cout << "程序结束!" << endl;
return 0;
}
注意点:
- while循环的测试条件,当输入0的时候跳出循环,还可用用别的循环。
结果图:
4. 第4题
#include <iostream>
using namespace std;
int main()
{
//原始存款100美元
double Daphne = 100;
double Cleo = 100;
int year = 0;//年数
double RateofDaphne = 0.10;//Daphne的年利率
double RateofCleo = 0.05;//Cleo的年利率
while (Cleo <= Daphne)
{
//第year年的投资价值
Daphne = Daphne + 100 * RateofDaphne;
Cleo = Cleo + Cleo * RateofCleo;
year++;
}
cout << year << "年后:" << endl;
cout << "Cleo的投资价值为:" << Cleo << "美元" << endl;
cout << "Daphne的投资价值为:" << Daphne << "美元" << endl;
cout << "Cleo的投资价值超过了Daphne的投资价值!" << endl;
return 0;
}
注意的点:
- 选择哪一种循环方式,我这里选择的是while循环,因为我不知道要循环多少次,当然也可以用for循环。
- 测试条件应该是Cleo的投资价值>Daphne的投资价值时,跳出循环。
结果图:
5. 第5题
#include <iostream>
#include<string>
using namespace std;
int main()
{
const string Month[12] = { "Jan","Feb","Mar","Apr","May","June","July","Aug","Sept","Nov","Oct","Dec" };//12个月
int number[12];//每个月的销售额
int total = 0;//总销售额
cout << "请输入每个月的销售额:" << endl;
for (int i = 0; i < 12; i++)
{
cout << Month[i] << ":";
cin >> number[i];
}
for (int i = 0; i < 12; i++)
{
total = total + number[i];
}
cout << "这一年的销售总额为:" << total << endl;
return 0;
}
注意的点:
- 月份字符串我采用的string,因此应该添加相应的头文件,之所以采用string而没有采用char*,是因为我觉得string比char更方便,不容易出错。
结果图:
6. 第6题
#include <iostream>
#include<string>
using namespace std;
int main()
{
const string Month[12] = { "Jan","Feb","Mar","Apr","May","June","July","Aug","Sept","Nov","Oct","Dec" };//12个月
int number[3][12];//每年每个月的销售额
int TotalofYear[3] = { 0 };//每年的销售额
int total = 0;//3年的总销售额
for (int i = 0; i < 3; i++)
{
cout << "请输入第" << i + 1 << "年各个月的销售额:" << endl;
for (int j = 0; j < 12; j++)
{
cout << Month[j] << ":";
cin >> number[i][j];
}
}
for (int i = 0; i < 3; i++)
{
for (int j = 0; j < 12; j++)
{
TotalofYear[i] = number[i][j] + TotalofYear[i];
}
cout << "第" << i + 1 << "年的销售额为:" << TotalofYear[i] << endl;
}
total = TotalofYear[0] + TotalofYear[1] + TotalofYear[2];
cout << "这3年的销售总额为:" << total << endl;
system("pause");
return 0;
}
这题相比于上一题代码要多一点,因为是三年的销售量,使用到二维数组,单单输入就要输3次12个月。其实二维数组就是多个一维数组组成的,很简单。
结果图:
7. 第7题
#include <iostream>
#include<string>
using namespace std;
struct car
{
string make;//生产商
int yearmade;//生产年份
};
int main()
{
int num;//车辆数
cout << "How many cars do you wish to catalog?";
cin >> num;
cin.get();//上一行输入的是数字,下一行输入的是字符串,为了防止混合输入数字和面向行的字符串出现的问题
car *arr = new car[num];//动态创建相应数量的car结构数组;
for (int i = 0; i < num; i++)
{
cout << "Car#" << i + 1 << ":" << endl;
cout << "Please enter the make: ";
getline(cin, (arr + i)->make);
cout << "Please enter the year made: ";
cin >> (arr + i)->yearmade;
cin.get();//上一行输入的是数字,下一行输入的是字符串,为了防止混合输入数字和面向行的字符串出现的问题
}
cout << "Here is your collection:" << endl;
for (int i = 0; i < num; i++)
{
cout << (arr + i)->make << " " << (arr + i)->yearmade << endl;
}
delete[] arr;//释放内存
return 0;
}
这一题看似简单,实质上到处都是坑!我就掉坑里了,尽管看过书,也知道容易犯错,还是栽了>-<
- 混合输入数字和面向行的字符串时,千万千万要注意!!!在二者之间加上
cin.get();
!!!这是因为当cin读取数字后会将回车键生成的换行符留在输入队列中,导致后面读取字符串时看到的不是你输入的字符串,而是换行符!为了防止这种错误的出现,请同学们一定要加上cin.get();
,我也求求我自己长长心吧,栽了好几次了。。。 - 使用new创建动态数组时,最后记得delete数组释放内存。
- 这里访问结构成员时,我使用的时=是指针,所以用箭头运算符访问。
结果图:
8. 第8题
#include <iostream>
#include<cstring>
using namespace std;
int main()
{
char words[100];
int count = 0;//计数
cout << "Enter words (to stop, type the word done): " << endl;
cin >> words;
while (strcmp(words, "done") != 0)
{
cin >> words;//不是done继续输入
count++;
}
cout << "You entered a total of " << count << " words." << endl;
return 0;
}
注意点
- 因为不知道要循环多少次,所以选择while循环
- 跳出循环的测试条件是用户输入done时。
- 当用户输入的不是done时,继续输入,并计数加一
- 这题和书上152页的5.16一个性质。
结果图:
9. 第9题
#include <iostream>
#include<string>
using namespace std;
int main()
{
string words;
int count = 0;//计数
cout << "Enter words (to stop, type the word done): " << endl;
cin >> words;
while (words != "done")
{
cin >> words;//不是done继续输入
count++;
}
cout << "You entered a total of " << count << " words." << endl;
return 0;
}
这题和上一题不同之处是用string类,因此比较两个字符串是否一样时,直接用!=即可。
结果图:
10. 第10题
#include <iostream>
#include<string>
using namespace std;
int main()
{
int num;
cout << "Enter number of rows: ";
cin >> num;
for (int i = 1; i <= num; i++)
{
//打印.
for (int j = i; j < num; j++)
{
cout << '.';
}
//打印*
for (int k = i; k > 0; k--)
{
cout << '*';
}
cout << endl;//每行打印完换行
}
return 0;
}
注意点:
- 第一层循环是用来控制行,1~5行
- 第二层循环中用来控制每行中打印的字符:其中有两个循环,第一个循环是打印字符.,第二循环是打印*,主要是控制打印两种字符的个数。
结果图: