C++ primer第一章习题
#include <iostream>
using std::cout;
using std::endl;
using std::cin;
int main()
{
//1.3
cout<<"Hello,Word!"<<endl;
//1.4
int num1,num2;
cin>>num1>>num2;
cout<<"num1 乘以num2 的结果为:"<<num1 * num2<<endl;
//1.9
int sum=0;
for(int i=-100;i<=100;++i)
{
sum+=i;
}
cout<<sum;//结果为
//1.10(1)
int sumF(0);
for(int i=50;i<=100;++i)
{
sumF+=i;
}
cout<<sumF<<endl;
//1.10(2)
int sumW(0);
int j=100;
while(j>=50)
{
sumW+=j;
j--;
}
cout<<sumW<<endl;
//1.11(1)
int i=10;
while(i>=0)
{
cout<<i<<endl;
i--;
}
//1.11(2)
for(int j=10;j>=0;--j)
{
cout<<j;
}
//1.16
int a(0),b(0);
cin>>a>>b;
if(a>b)
{
cout<<a;
}
else
{
cout<<b;
}
//1.17
int num(0);
int result(0);
while(cin>>num)
{
if(num<0)
{
result++;
}
}
cout<<result<<endl;
//1.18+1.19
int num1(0),num2(0);
int count(0);
cin>>num1>>num2;
if(num1==num2)
{
cout<<"不能相等!";
return 0;
}
else if(num1>num2)
{
for(num1;num1>=num2;num1--)
{
cout<<num1<<" ";
count++;
if(count%10==0)
{
cout<<endl;
}
}
return 0;
}
else {
while(num1<=num2)
{
cout<<num1<<" ";
num1++;
count++;
if(count%10==0)
{
cout<<endl;
}
}
return 0;
}
system("pause");
}