#include "stdafx.h"
#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:
#include "stdafx.h"
#include<iostream>
using namespace std;
int main()
{
int i,j;
for(i=0;i<5;i++)
{
for(j=i;j<5;j++)
cout<<"*";
cout<<endl;
}
return 0;
}
#include "stdafx.h"
#include<iostream>
using namespace std;
int main()
{
int sum,n;
n=1;
sum=0;
do
{
sum+=n;
n++;
}while(n<=100);
cout<<"sum="<<sum<<endl;
return 0;
}
#include "stdafx.h"
#include<iostream>
using namespace std;
int main(int argc, char* argv[])
{
int a,b,c;
for(a=1;a<=9;a++)
{
for(b=1;b<=a;b++)
{
c=a*b;
cout<<a<<"*"<<b<<"="<<c<<endl;
}
cout<<endl;
}
return 0;
}
#include "stdafx.h"
#include<iostream>
using namespace std;
int main()
{
int a,b,c,d=0;
cout<<"please input a number"<<endl;
cin>>a;
for(b=10;b<=a;b*=10)
{
if(a/b>=1)
c=1;
d+=c;
}
cout<<"这是一个"<<d+1<<"位数"<<endl;
d=0;
for(b=b/10;b>=1;b=b/10)
{
c=a/b;
d+=c;
a%=b;
}
cout<<"各位数之和等于:"<<d<<endl;
return 0;
}
#include "stdafx.h"
#include<iostream>
using namespace std;
int main()
{
int a,b,c;
for(a=0;a*5<=100;a++)
for(b=0;(a*5+b*3)<=100;b++)
{
c=100-a-b; //百鸡
if((a*5+b*3+c/3==100)&&(c%3==0)) //百钱
cout<<"公鸡有"<<a<<"只"
<<"母鸡有"<<b<<"只"
<<"小鸡有"<<c<<"只"<<endl;
}
return 0;
}
习题3整数:
#include "stdafx.h"
#include<iostream>
using namespace std;
int main()
{
int a,b,c,d=0;
cout<<"please input a number"<<endl;
cin>>a;
for(b=10;b<=a;b*=10)
{
if(a/b>1)
c=1;
d+=c;
}
cout<<"这是一个"<<d+1<<"位数"<<endl;
d=0;
for(b=b/10;b>=1;b=b/10)
{
c=a/b;
d+=c;
a%=b;
}
cout<<"各位数之和等于:"<<d<<endl;
return 0;
}
习题4塔内建筑:
#include "stdafx.h"
#include<iostream>
using namespace std;
int main()
{
float x,y;
cout<<"请输入点坐标,坐标值用空格键隔开"<<endl;
cin>>x>>y;
if(x<0) //由于每个区域是对称的,所以可以将所有坐标转换到第一象限
x=-x; //这样就可以简化判断条件
if(y<0) //
y=-y; //
if((x-2)*(x-2)+(y-2)*(y-2)<=1)
cout<<"该点建筑高度为10米"<<endl;
else
cout<<"该点建筑高度为0米"<<endl;
return 0;
}
习题5级数n项和方法一for循环:
#include "stdafx.h"
#include<iostream>
using namespace std;
int main()
{
int a,b,c,d,s=0;
cout<<"你想计算到第几项?"<<endl;
cin>>c;
for(a=c;a>0;a--)
{
d=1;
for(b=a;b>=1;b--)
d*=b;
s+=d;
}
cout<<"级数前"<<c<<"项之和为:"<<s<<endl;
return 0;
}
方法2while:#include "stdafx.h"
#include<iostream>
using namespace std;
int main()
{
int a,b,c,d,s=0;
cout<<"你想计算到第几项?"<<endl;
cin>>a;
b=a;
while(b>0)
{
c=b;
d=1;
while(c>0)
{
d*=c;
c--;
}
s+=d;
b--;
}
cout<<"级数前"<<a<<"项之和为:"<<s<<endl;
return 0;
}
习题6猴子:
#include "stdafx.h"
#include<iostream>
using namespace std;
int main()
{
int a=10,b=1,c=0;
for(a;a>1;a--)
{
c=2*(b+1);
b=c;
}
cout<<"猴子第一天共摘了"<<c<<"个苹果"<<endl;
return 0;
}
习题7求数列的和:
#include "stdafx.h"
#include<iostream>
using namespace std;
int main()
{
int a,n,c,s;
cout<<"请输入一个数(0~9):"<<endl;
cin>>a;
cout<<"请输入要计算的位数n"<<endl;
cin>>n;
c=s=a;
for(n;n>1;n--)
{
a*=10;
c+=a; //c=aa,c=aaa,c=aaaa...
s+=c;
}
cout<<"s[n]="<<s<<endl;
return 0;
}
习题9比赛名单:
#include "stdafx.h"
#include<iostream>
using namespace std;
struct yidui
{char name[20];};
int main()
{
yidui nam[4]={{"ChenLiu"},{"ZhaoQi"},{"SongBa"}};
int z3,l4,w5;
for(z3=2;z3>=0;z3--)
for(l4=2;l4>=0;l4--)
for(w5=0;w5<=2;w5++)
if(z3!=l4&&l4!=w5&&w5!=z3)//对手不能相同
if(z3!=0&&w5!=0&&w5!=2) //
cout<<"ZhangSan and "<<nam[z3].name<<endl
<<"LiSi and "<<nam[l4].name<<endl
<<"WangWu and "<<nam[w5].name<<endl;
return 0;
}
乘法表:
#include "stdafx.h"
#include<iostream>
using namespace std;
int main()
{
int a,b,c;
for(a=1;a<=9;a++)
{
for(b=1;b<=a;b++)
{
c=b*a;
cout<<b<<"x"<<a<<"="<<c<<"\t";
}
cout<<endl;
}
return 0;
}