3 回答
TA贡献1796条经验 获得超4个赞
给,已经编译运行确认:
1.
#include <iostream>
using namespace std;
void print_pyramid(int height);
int main()
{
int pyramid_height;
char ch;
do
{
cout << "how high would you like the pyramid?: ";
cin >> pyramid_height;
while (pyramid_height > 30 || pyramid_height < 1)
{
cout << "Pick another height (must be between 1 and 30): ";
cin >> pyramid_height;
}
print_pyramid(pyramid_height);
cout << "Do you want to print another pyramid (Y/N)? ";
cin>>ch;
}while(ch=='Y');
return 0;
}
void print_pyramid(int height)
{
int a,i;
cout << "\n";
for (a=0;a<height;a++)
{
for (i=0;i<a;i++) cout<<'*';
cout<<endl;
}
for (a=0;a<height;a++)
{
for (i=height-1-a;i>=0;i--) cout<<'*';
cout<<endl;
}
cout << "\n";
}
2.
#include <iostream>
#include <stdlib.h>
#include <time.h>
using namespace std;
int main()
{
int a[10]={NULL},total=1000;
int i;
int max,maxList;
srand((unsigned)time(NULL));
for(i=0;i<10;i++)
{
if(i!=9)
{
a[i]=(rand()%1001)%(total+1);
total-=a[i];
}
else a[9]=total;
}
max=a[0];maxList=0;
for(i=1;i<=10;i++)
{
cout<<i<<" "<<a[i-1]<<endl;
if(a[i-1]>max) {max=a[i-1];maxList=i;}
}
cout<<"The winner is candidate number "<<maxList<<" with "<<max<<" votes!";
system("pause");
return 0;
}
TA贡献1820条经验 获得超2个赞
#include <iostream.h>
#include <conio.h>
main()
{
int i,n,sum=0;
int vote[10]={0};
srand(time(NULL));//设置随机种子
while(1)//循环,直到获得合适的票数
{
sum=0;//将总和归零
for(i=0;i<10;i++)//循环10次,生成10个随机数
{
vote[i]=rand()%1000;//随机数在0-1000之间
sum=sum+vote[i];//将生成的随机数累加,存在sum变量中
}
if(sum==1000)//判断是否等于确定的随机数总和
break;
}
cout<<"candidate number number of votes"<<endl;
int max=0;int maxi;
for(i=0;i<10;i++)//输出票数,并比大小
{
if(max<vote[i])
{max=vote[i];maxi=i;}
cout<<i<<" "<<vote[i]<<endl;
}
cout<<"The winner is candidate number"<<maxi<<"with"<<max<<"votes!"<<endl;
system("pause");//使程序在DOS窗口下暂停,可注释掉
TA贡献1876条经验 获得超6个赞
#include <iostream>
using namespace std;
int main()
{
int n,i,j;
char ch='y';
while(ch=='y'||ch=='Y')
{
cout<<"How high would you like the pyramid to be(1~15)?";
cin>>n;
while(n>15)
{
cout<<"How high would you like the pyramid to be(1~15)?";
cin>>n;
}
for(i=0;i<n;i++)
{
for(j=0;j<i+1;j++)
cout<<"*";
cout<<endl;
}
for(i=0;i<n-1;i++)
{
for(j=0;j<n-1-i;j++)
cout<<"*";
cout<<endl;
}
cout<<"Do you want to print another pyramid (Y/N)?";
cin>>ch;
}
return 0;
}
添加回答
举报