话不多说直接上代码:
1.
#include<iostream>
using namespace std;
int main()
{
int a,b, sum=0;
cout << "Please enter two numbers:" << endl;
cin >> a >> b;
if(a>b)
{
int t = a;
a = b;
b = t;
}
for(int i=a; i<=b; i++)
{
sum +=i;
}
cout << "All integers between them add up to:" << sum << endl;
return 0;
}
#include<iostream>
#include<array>
using namespace std;
const int ArSize = 16;
int main()
{
array<long long, ArSize> factorials;
factorials[0] = factorials[1] = 1LL;
for(int i=2; i<ArSize; i++)
{
factorials[i] = i*factorials[i-1];
}
for(int x:factorials)//基于范围的for循环
{
cout << x << endl;
}
}
#include<iostream>
using namespace std;
int main()
{
int n;
int sum=0;
cin >> n;
while(n != 0)
{
sum += n;
cout << sum << endl;
cin >> n;
}
return 0;
}
#include<iostream>
using namespace std;
const double Daphne = 0.1;
const double Cleo = 0.05;
int main()
{
int year = 1;
double d_Interest = 100*Daphne;//利息
double c_Interest = 100*Cleo;
double d_Amount = d_Interest + 100;
double c_Amount = c_Interest + 100;
while(d_Amount > c_Amount)
{
++year;
d_Amount += d_Interest;
c_Interest = c_Amount*Cleo;
c_Amount += c_Amount*Cleo + c_Interest;
}
cout << "It would take " << year <<
" years for Cleo's investment value to surpass Daphne's." << endl
<< "Cleo's investment is: " << c_Amount << endl
<< "Daphne's investment is: " << d_Amount << endl;
return 0;
}
#include<iostream>
#include<string>
using namespace std;
int main()
{
string month[] =
{"January","February","March","April","May","June","July","August","September","October","November","December"};
int sales[13];
int sum=0;
for(int i=0; i<12; i++)
{
cout << "Please enter the sales for " << month[i] << endl;
cin >> sales[i];
sum += sales[i];
}
for(int i=0; i<12; i++)
{
cout << month[i] << " sales: " << sales[i] << endl;
}
cout << "Total sales this year are: " << sum;
}
#include<iostream>
#include<string>
using namespace std;
int main()
{
string month[] =
{"January","February","March","April","May","June","July","August","September","October","November","December"};
string years[] = {"First", "Second", "Third"};
int sales[3][13];
int sum[3];
int total=0, year=0;
for(int j=0; j<3; j++)
{
for(int i=0; i<12; i++)
{
cout << "Please enter the sales for "
<< month[i] << " of the " << years[j] << "year."<< endl;
cin >> sales[j][i];
sum[j] += sales[j][i];
}
++year;
}
for(int i=0; i<3; i++)
{
cout << years[i] << " year sales :" << sum[i] << endl;
total += sum[i];
}
cout << "Total sales for the three years are: " << total;
}
#include<iostream>
#include<string>
using namespace std;
struct Car
{
string producer;
int year;
};
int main()
{
int n;
cout << "How many cars do you wish to catalog?";
cin >> n;
cin.get();
Car *car = new Car[n];
for(int i=0; i<n; i++)
{
cout << "Car #" << (i+1) << " :" << endl
<< "Please enter the make: " << endl;
getline(cin, car[i].producer);
cout << "Please enter the year made: " << endl;
cin >> car[i].year;
cin.get();
}
cout << "Here is your collection:" << endl;
for(int i=0; i<n; i++)
{
cout << car[i].year << " " << car[i].producer << endl;
}
return 0;
}
#include<iostream>
#include<cstring>
using namespace std;
int main()
{
char ch[20];
int letters=0;
cout << "Enter words(to stop, type the word done)" << endl;
while(cin >> ch)
{
// 比较参数1,2(1>2:返回正数 1<2:返回负数 1==2:返回零)
if(strcmp(ch, "done")!=0)//if(strcmp(ch, "done"))
{
++letters;
}
else
{
break;
}
}
cout << "You entered a total of " << letters << " words.";
}
#include<iostream>
#include<string>
using namespace std;
int main()
{
string word;
int n_words;
cout << "Enter words(to stop, type the word done):" << endl;
while(cin >> word)
{
if(word == "done")
{
break;
}
++n_words;
}
cout << "You entered a total of " << n_words << " words." << endl;
return 0;
}
#include<iostream>
using namespace std;
int main()
{
int n;
cout << "Enter number of row: " ;
cin >> n;
for(int i=0; i<n; i++)
{
for(int j=1; j<n-i; j++)
{
cout << "." ;
}
for(int k=0; k<=i; k++)
{
cout << "*" ;
}
cout << endl;
}
return 0;
}
以上都是自己琢磨着写的,非标准答案,欢迎指正。有不懂的地方也欢迎来问噢~