【1】 编写程序求三个数的平均值;A
要求输出格式,求5,7,11的平均值时,输出
Enter the first number: 5
Enter the second number: 7
Enter the third number: 11
Average is : 7.66667
代码:
#include <iostream>
using namespace std;
int main()
{float a,b,c,d,e;
cin>>a>>b>>c;
cout<<"Enter the first number:"<<a<<endl;
cout<<"Enter the second number:"<<b<<endl;
cout<<"Enter the third number:"<<c<<endl;
d=a+b+c;
e=d/3;
cout<<"Average is:"<<e<<endl;
return 0;
}
挺顺利的但是还有疑问….就是endl是否等于’/n’?
【2】 请写程序根据输入的n,画出图形。比如n=5时,图形如下:C
*
***
*****
*******
*********
简化版…先弄出最后一层:
#include <iostream>
using namespace std;
int main()
{ int a,b;
cin>>a;
b=2*a-1;
while(b>=1)
{
cout<<' 42';
b=b-1;
}
return 0;
}
【5】编写上一题程序,使得可以拆分任意位的整数,输入输出为:C
Enter number:456
[1]:6
[2]:5
[3]:4
#include <iostream>
using namespace std;
int main()
{char c;
int n=0;
cout<<"Enter number: ";
while((c=getchar())!=' ')
{ n=n+1;
cout<<"["<<n<<"]:"<<c<<endl;
}
cout<<endl;
return 0;
}
【6】从10个数中寻找最大的数和最小的数。并解决歌星大奖赛问题。在歌星大奖赛中,有10个评委为参赛的选手打分,分数为1~100分。选手最后得分为:去掉一个最高分和一个最低分后其余8个分数的平均值。请编写一个程序实现。 B
#include <iostream>
using namespace std;
int main()
{ float a,b,c,d,e,f,g,h,i,j,k,l,m;
cin>>a>>b>>c>>d>>e>>f>>g>>h>>i>>j;
k=a+b+c+d+f+g+h+i+j;
if(a-b>=0)l=a;
else l=b;
if(c-l>=0)l=c;
else ;
if(d-l>=0)l=d;
else;
if(e-l>=0)l=e;
else;
if(f-l>=0)l=f;
else;
if(g-l>=0)l=g;
else;
if(h-l>=0)l=h;
else;
if(i-l>=0)l=i;
else;
if(j-l>=0)l=j;
else;
if(a-b<=0)m=a;
else m=b;
if(c-m<=0)m=c;
else ;
if(d-m<=0)m=d;
else;
if(e-m<=0)m=e;
else;
if(f-m<=0)m=f;
else;
if(g-m<=0)m=g;
else;
if(h-m<=0)m=h;
else;
if(i-m<=0)m=i;
else;
if(j-m<=0)m=j;
else;
k=k-m-l;
k=k/8;
cout<<k<<endl;
return 0;
}
老是平均数不对,排查
#include <iostream>
using namespace std;
int main()
{ float a,b,c,d,e,f,g,h,i,j,k,l,m;
cin>>a>>b>>c>>d>>e>>f>>g>>h>>i>>j;
k=a+b+c+d+f+g+h+i+j;
if(a-b>=0)l=a;
else l=b;
if(c-l>=0)l=c;
else ;
if(d-l>=0)l=d;
else;
if(e-l>=0)l=e;
else;
if(f-l>=0)l=f;
else;
if(g-l>=0)l=g;
else;
if(h-l>=0)l=h;
else;
if(i-l>=0)l=i;
else;
if(j-l>=0)l=j;
else;
if(a-b<=0)m=a;
else m=b;
if(c-m<=0)m=c;
else ;
if(d-m<=0)m=d;
else;
if(e-m<=0)m=e;
else;
if(f-m<=0)m=f;
else;
if(g-m<=0)m=g;
else;
if(h-m<=0)m=h;
else;
if(i-m<=0)m=i;
else;
if(j-m<=0)m=j;
else;
k=k-m-l;
cout<<k<<endl;
k=k/8;
cout<<k<<endl;
return 0;
}
把每一次的k输出来,原来是加少个e....
#include <iostream>
using namespace std;
int main()
{ float a,b,c,d,e,f,g,h,i,j,k,l,m;
cin>>a>>b>>c>>d>>e>>f>>g>>h>>i>>j;
k=a+b+c+d+e+f+g+h+i+j;
if(a-b>=0)l=a;
else l=b;
if(c-l>=0)l=c;
else ;
if(d-l>=0)l=d;
else;
if(e-l>=0)l=e;
else;
if(f-l>=0)l=f;
else;
if(g-l>=0)l=g;
else;
if(h-l>=0)l=h;
else;
if(i-l>=0)l=i;
else;
if(j-l>=0)l=j;
else;
if(a-b<=0)m=a;
else m=b;
if(c-m<=0)m=c;
else ;
if(d-m<=0)m=d;
else;
if(e-m<=0)m=e;
else;
if(f-m<=0)m=f;
else;
if(g-m<=0)m=g;
else;
if(h-m<=0)m=h;
else;
if(i-m<=0)m=i;
else;
if(j-m<=0)m=j;
else;
k=k-m-l;
k=k/8;
cout<<k<<endl;
return 0;
}
超级麻烦的程序...待改进
优化:#include <iostream>
using namespace std;
int max(int x,int y)
{int z;
z=x>y? x:y;
return(z);
}
int min(int x,int y)
{int z;
z=x<y? x:y;
return(z);
}
int main()
{ float a,b,c,d,e,f,g,h,i,j,k,l,m;
cin>>a>>b>>c>>d>>e>>f>>g>>h>>i>>j;
k=a+b+c+d+e+f+g+h+i+j;
l=max(max(max(max(max(max(max(max(max(a,b),c),d),e),f),g),h),i),j);
m=min(min(min(min(min(min(min(min(min(a,b),c),d),e),f),g),h),i),j);
k=k-m-l;
k=k/8;
cout<<k<<endl;
return 0;
}
【3】 请写程序根据输入的n,画出图形。比如n=5时,图形如下:C
*
***
*****
*******
*********
最后一行;
#include <iostream>
#include <iomanip>
using namespace std;
int main()
{ int a,b;
cin>>a;
b=2*a-1;
cout<<setw(b)<<setfill('*')<<endl;
return 0;
}
这句 cout<<setw(b)<<setfill('*')<<endl;无法实现...于是...
#include <iostream>
#include <iomanip>
using namespace std;
int main()
{ int a,b;
cin>>a;
b=2*a-1;
cout<<setw(b+1)<<setfill('*')<<' ';
return 0;
}
可以啦
好像是控制符里面一定要有实质“内容”?
反正最后一行是先解决了
然后开始循环
#include <iostream>
#include <iomanip>
using namespace std;
int main()
{ int a,b;
cin>>a;
b=2*a-1;
while(b>=1)
{
cout<<setw(b+1)<<setfill('*')<<' ';
b=b-2;
}
return 0;
}
又是倒过来的问题!!!!!
#include <iostream>
#include <iomanip>
using namespace std;
int main()
{ int a,b,c;
cin>>a;
b=2*a-1;
c=1;
while(b>=1)
{
cout<<setw(c+1)<<setfill('*')<<' ';
c=c+2;
b=b-2;
}
return 0;
}
Ok了尼玛~~~
绝对没有人这样写 哈哈哈~
剩下居中的问题...
可以在前面补空格
注意到输出流控制符setfill只对其后的输出有效,于是:
#include <iostream>
#include <iomanip>
using namespace std;
int main()
{ int a,b,c;
cin>>a;
b=2*a-1;
c=1;
while(b>=1)
{ cout<<setfill(' ');
cout<<setw(a)<<" ";
cout<<setw(c+1)<<setfill('*')<<' ';
c=c+2;
b=b-2;
a=a-1;
}
return 0;
}
关于get、putchar
#include <iostream>
using namespace std;
int main()
{char c;
while((c=getchar())!=' ')
{if((c>='a'&&c<='z')||(c>='A'&&c<='Z'))
{c=c+4;
if(c>'Z'&&c<='Z'+4||c>'z')c=c-26;
}
cout<<c;
}
cout<<endl;
return 0;
这是一个加密程序
输入一个char c,再使用c=getchar()能够判别每一个字符是否为换行符??
再:
#include <iostream>
using namespace std;
int main()
{char c;
while((c=getchar())!=' ')
{ cout<<c<<endl;
}
cout<<endl;
return 0;
}
则:
于是再改进了一下:
#include <iostream>
using namespace std;
int main()
{char c;
int n=0;
cout<<"Enter number: ";
while((c=getchar())!=' ')
{ n=n+1;
cout<<"["<<n<<"]:"<<c<<endl;
}
cout<<endl;
return 0;
}
可得到:
变成了可以拆分数据的程序啦…和那道题目的要求还是不太一样
使得可以拆分任意位的整数,输入输出为:C
Enter number:456
[1]:6
[2]:5
[3]:4
就是顺序倒过来了,应该是个位在最前面的。
终于成功了..