第一题
#include<iostream>
#include<string>
#include<array>
using namespace std;
int main()
{
int a, b;
cout << "请输入较小的整数:";
cin >> a ;
cout << "请输入较大的整数:";
cin >> b ;
int sum = 0;
while (a <= b)
{
sum = sum + a;
a++;
}
cout <<"所有整数和为:" << sum<<endl;
system("pause");
return 0;
}
#include<iostream>
#include<array>
using namespace std;
const int ArSize = 16;
int main()
{
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;
}
#include<iostream>
using namespace std;
int main()
{
int number,sum = 0;
cout << "请输入数字:" ;
cin >> number;
while (number != 0)
{
sum = sum+number;
cout << "累计和为:" << sum<<endl;
cout << "请输入数字:" ;
cin >> number;
}
system("pause");
return 0;
}
#include<iostream>
using namespace std;
int main()
{
double Daphne, Cleo;
Daphne = Cleo = 100.0;
int year=1;
while (Cleo <= Daphne) {
Cleo += (Cleo) * 0.05;
Daphne += 10;
year++;
}
cout << "it will take " << year-1 << " years that Cleo's investment value exceeds Daphne's investment value.\n";
cout << "Now the Cleo's investment is " << Cleo << endl;
cout << "And the Daphne's investment is " << Daphne << endl;
return 0;
}
#include<iostream>
using namespace std;
int main()
{
string arr[] = { "一月份","二月份" ,"三月份" ,"四月份" ,"五月份" ,"六月份" ,"七月份" ,"八月份" ,"九月份" ,"十月份" ,"十一月份" ,"十二月份" };
int amount[12];
int sum = 0;
for (int i = 1;i <= 12;i++) {
cout << "请输入" << arr[i - 1] << "的图书数量:";
cin >> amount[i - 1];
sum += amount[i - 1];
}
cout << "图书总数为:" << sum << endl;
system("pause");
return 0;
}
输出结果为:
#include<iostream>
using namespace std;
int main()
{
string arr[] = { "一月份","二月份" ,"三月份" ,"四月份" ,"五月份" ,"六月份" ,"七月份" ,"八月份" ,"九月份" ,"十月份" ,"十一月份" ,"十二月份" };
int amount[3][12];
int sum_year = 0;
int sum = 0;
for (int year = 1;year <= 3;year++) {
string arr_year[] = { "第一年","第二年","第三年" };
cout << "请输入" << arr_year[year-1] << "的数量" << endl;
for (int i = 1;i <= 12;i++) {
cout << "请输入" << arr[i - 1] << "的图书数量:";
cin >> amount[year-1][i-1];
sum_year += amount[year - 1][i - 1];
}
cout << "本年的图书总数为:"<<sum_year<<endl;
sum += sum_year;
sum_year = 0;
}
cout << "图书总数为:" << sum << endl;
system("pause");
return 0;
}
#include<cstring>
using namespace std;
struct collection {
char name[100];
int year;
};
int main()
{
int amount;
cout << "How many cars do you wish to catalog?";
cin >> amount;
cin.get();
collection car[100];
for (int i = 1;i <= amount;i++)
{
cout << "Car #" << i << endl;
cout << "Please enter the make:";
cin.get(car[i].name, 100).get();
cout << "Please enter the year made:" << endl;
cin >> car[i].year;
cin.get();
}
cout << "Here is your collection:" << endl;
for (int i = 1;i <= amount;i++) {
cout << car[i].year << " " << car[i].name << endl;
}
system("pause");
return 0;
}
#include<iostream>
#include<string>
#include<cstring>
using namespace std;
int main()
{
char word[100];
int i = 0;
cout << "Enter words(to stop,type the word done):" << endl;
while (cin >> word && strcmp(word, "done"))
i++;
cout << "you entered a total of" << i << "words." << endl;
system("pause");
return 0;
}
#include<iostream>
#include<string>
#include<cstring>
using namespace std;
int main()
{
string word;
int i = 0;
cout << "Enter words(to stop,type the word done):" << endl;
while (cin>>word && word != "done")
i++;
cout << "you entered a total of " << i << " words." << endl;
system("pause");
return 0;
}
比如:
while(!(cin>>golf[i])){
...,
}
如果用户输入88,cin将为true,!(cin>>golf[i])将为false
#include<iostream>
#include<string>
#include<cstring>
using namespace std;
int main()
{
int column;
cout << "请输入行数:";
cin >> column;
for (int i = 0;i <column;i++) {
cin.get();
for (int j = (column - i);j != 1;j--) {
cout << ". ";
}
for (int k = (i+1);k!=0;k--) {
cout << "* ";
}
}
cin.get();
system("pause");
return 0;
}