例3.10
#include <iostream>
#include <iomanip>
using namespace std;
int main()
{
double x=66,y=-8.246;
cout<<"x = "<<x<<"\t\t"
<<"y = "<<y<<endl;
cout <<setiosflags(ios::showpoint);
cout <<"x = "<<x<<'\t\t'
<<"y = "<<y<<endl;
return 0;
}
3.11
#include <iostream>
#include <iomanip>
using namespace std;
int main()
{
double x=66, y=-8.246;
cout<<"x = "<<x<<"\t\t"
<<"y = "<<y<<endl;
cout <<setiosflags(ios::showpos);
cout <<"x = "<<x<<'\t\t'
<<"y = "<<y<<endl;
return 0;
}
3.13
#include <iostream>
using namespace std;
int main()
{
int n,i,j,k,result;
cout<<"please input a number of three:";
cin>>n;
int temp=n;
i=temp/100;
temp-=i*100,j=temp/10;
k=temp-j*10;
result=k*100+j*10+i;
cout<<n<<"的逆转数是:"<<result<<endl;
return 0;
}
3.15
#include <iostream>
using namespace std;
int main()
{
int x= 10;
cout<<"x = "<<x<<endl;
{
int x = 20;
cout<<"x = "<<x<<endl;
}
cout <<"x = "<<x<<endl;
return 0;
}
3.19
#include <iostream>
using namespace std;
int main()
{
int score;
char grade;
cout<<"请输入学生百分制成绩:";
cin>>score;
if(score>=90) grade = 'A';
else if (score>=80) grade = 'B';
else if (score>=60) grade = 'C';
else grade = 'D';
cout <<"grade = "<<grade<<endl;
return 0 ;
}
3.20
#include <iostream>
using namespace std;
int main()
{
int x,y;
cout<<"请输入x的值:";
cin>>x;
if(x)
{
if(x>0)
y = 1;
else
y = -1;
}
else
y=0;
cout <<"x= "<<x<<"\t\t"<<"y = "<<y<<endl;
return 0 ;
}
3.24
#include <iostream>
using namespace std;
int main()
{
int x,cout1=0,cout2=0;
cout<<"请输入正整数,当输入0或负数时结束输入:";
cin>>x;
while (x>0)
{
if (x<60)
cout1++;
else
cout2++;
cin>>x;
}
cout<<"小于60的正整数的个数 :"<<cout1<<endl;
cout <<"大于等于60的正整数的个数 :"<<cout2<<endl;
return 0 ;
}
3.33
#include <iomanip>
#include <iostream>
using namespace std;
int main()
{
int f1 = 1,f2 = 1;
for (int i= 1;i<=20;i++)
{
cout<<setw(12)<<f1<<setw(12)<<f2;
if(i%2 ==0)
cout<<endl;
f1 = f1+f2;
f2 = f2+f1;
}
return 0 ;
}
3.34
#include <iomanip>
#include <iostream>
using namespace std;
int main()
{
int max_item,number;
cout<<"请输入一个自然数:";
cin>>number;
if (number<1)
{
cout<<"输入有误"<<endl;
exit(0);
}
max_item = number*(number +1)-1;
int sum = max_item;
cout<<number<<"*"<<number<<"*"<<number<<" = "<<max_item;
for(int i= 1;i<number;i++)
{
max_item-=2;
sum +=max_item;
cout<<" + "<<max_item;
}
if(number ==1)
cout<<endl;
else
cout<<" = "<<sum<<endl;
return 0 ;
}