第一题
#include <iostream>
using namespace std;
int main()
{
cout<<"请输入两个整数,将计算两个整数之间(包括这两个整数)所有整数的和\n";
int first,twice,last=0;
cin>>first>>twice;
for (int i=first;i<=twice;i++)
{
last=last+i;
}
cout<<"和为:"<<last<<endl;
return 0;
}
第二题
#include <iostream>
#include <array>
using namespace std;
const int ArSize = 100;
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];
}
cout.setf(ios_base::fixed);
for (int i=0;i<ArSize;i++)
{
cout<<i<<"!="<<factorials[i]<<endl;
}
return 0;
}
第三题
#include <iostream>
using namespace std;
int main()
{
cout<<"请输入数字,将输出累计和,输入0停止\n";
int number,sum=0;
do
{
cin>>number;
sum=sum+number;
}while(number!=0);
cout<<"累计和为:"<<sum<<endl;
}
第四题
#include <iostream>
using namespace std;
const double simple_interest = 0.10;
const double compound_interest = 0.05;
int main()
{
double Daphne,Cleo;
int number=0;
Daphne=Cleo=100;
while(Daphne>=Cleo)
{
Daphne = Daphne+10;
Cleo = Cleo+Cleo*compound_interest;
number++;
}
cout<<"Daphne: "<<Daphne<<"美元"<<endl;
cout<<"Cleo: "<<Cleo<<"美元"<<endl;
cout<<number<<"年之后Cleo比Daphne多"<<endl;
return 0;
}
第五题
#include <iostream>
using namespace std;
const int month = 12;
int main()
{
const char * Month[month]=
{
"January",
"February",
"March",
"April",
"May",
"June",
"July",
"Aguest",
"September",
"October",
"November",
"December"
};
int Sales[month];
int sum=0;
cout<<"输入每个月的销量"<<endl;
for (int i=0;i<12;i++)
{
cout<<Month[i]<<":";
cin>>Sales[i];
sum=sum+Sales[i];
}
cout<<"今年的销量为:"<<sum<<endl
return 0;
}
第六题
#include <iostream>
using namespace std;
const int month = 12;
const int years = 3;
int main()
{
const char * Month[month]=
{
"January",
"February",
"March",
"April",
"May",
"June",
"July",
"Aguest",
"September",
"October",
"November",
"December"
};
int Sales[years][month];
int annual[years]={0};
int sum=0;
for (int i=0;i<years;i++)
{
cout<<"第"<<i+1<<"年\n";
cout<<"输入每个月的销量"<<endl;
for (int j=0;j<month;j++)
{
cout<<Month[j]<<":";
cin>>Sales[i][j];
annual[i]=annual[i]+Sales[i][j];
}
cout<<"第"<<i+1<<"年的销量是:"<<annual[i]<<endl;
sum=sum+annual[i];
}
cout<<"3年的总销售量为:"<<sum<<endl;
return 0;
}
第七题
#include <iostream>
#include <cstring>
#include <string>
using namespace std;
struct Car{
string producer;
int year;
};
int main()
{
cout<<"How many cars do you wish to catalog?";
int num;
cin>>num;
Car *car=new Car [num];
for (int i=0;i<num;i++)
{
cout<<"Car #"<<i+1<<":"<<endl;
cout<<"Please enter the made: ";
cin.get();
getline(cin,car[i].producer);
cout<<"Please enter the year made: ";
cin>>car[i].year;
}
cout<<"Here is your collection: \n";
for (int i=0;i<num;i++)
{
cout<<car[i].year<<" "<<car[i].producer<<endl;
}
delete [] car;
return 0;
}
第八题
#include <iostream>
#include <cstring>
using namespace std;
const int ArSize = 10;
int main()
{
cout<<"Enter words (to stop, type the word down)\n";
char words[ArSize];
int num=0;
cin>>words;
while (strcmp(words,"done"))
{
cin>>words;
num++;
}
cout<<"\nYou entered a total of "<<num<<"words\n";
return 0;
}
第九题
#include <iostream>
#include <cstring>
using namespace std;
int main()
{
cout<<"Enter words (to stop, type the word down)\n";
string words;
int num=0;
cin>>words;
while (words!="done")
{
cin>>words;
num++;
}
cout<<"\nYou entered a total of "<<num<<" words\n";
return 0;
}
第十题
#include <iostream>
using namespace std;
int main()
{
cout << "enter number of rows: ";
int num;
cin >> num;
int some = 1;
while (some <= num)
{
for (int j = 0; j < num - some; j++)
{
cout << ".";
}
for (int i = 0; i < some; i++)
{
cout << "*";
}
some++;
cout << endl;
}
return 0;
}