首届程序设计大赛题目及答案.docx
《首届程序设计大赛题目及答案.docx》由会员分享,可在线阅读,更多相关《首届程序设计大赛题目及答案.docx(9页珍藏版)》请在冰豆网上搜索。
首届程序设计大赛题目及答案
首届程序设计大赛题目及答案
程序设计大赛题目
1.硬币兑换:
用一元人民币兑换一分、二分和五分的硬币,编程求出所有兑换方法,并打印每种情况下各面值硬币需几枚?
(10分)
2.旅馆里有一百个房间,从1到100编了号。
第一个服务员把所有的房间门都打开了,第二个服务员把所有编号是2的倍数的房间“相反处理”,第三个服务员把所有编号是3的倍数的房间作“相反处理”,……,以后每个服务员都是如此。
问第100个服务员来过后,哪几扇门是打开的。
(所谓“相反处理”是:
原来开着的门关上,原来关上的门打开。
)(15分)
3.整型数组,长度为10,输入数组各元后,求数组各元的平均值,并找出最接近平均值的那个元素。
打印输出上述两个结果,用逗号隔开,不要有其它字符。
(20分)
4.编程求两个很长的整数之和,比如两个20位十进制整数。
(很长的整数指无法用long型存储的数)(25分)
例如:
12345678900987654321+12345678900987654321=24691357801975308642
5.编写布雷程序。
(30分)
在一个10*10的方格中,随机分布10个地雷,并在其它没有地雷的格中显示该方格周围相邻的方格中共有几枚地雷。
样例输出:
(图中-1的位置表示地雷,其它值表示该位置相邻的八个格子中的地雷数)
答案:
第一题:
#include
usingnamespacestd;
intmain()
{
inti,j,k;
for(i=1;i<=20;i++)
for(j=1;j<=50;j++)
{
k=100-5*i-2*j;
if(k>0)
cout<<"五分硬币"<
}
system("pause");
return0;
}
第二题:
#include
usingnamespacestd;
intmain()
{
inti,j; //i为服务员编号,j为房间编号
inta[101];
for(intt=1;t<101;t++)
a[t]=1;
for(i=2;i<101;i++)
for(j=i;j<101;j++)
if(j%i==0)a[j]*=-1;
cout<<"打开的房间为:
";
for(i=1;i<101;i++)
if(a==1)
cout<
system("pause");
return0;
}
第三题:
#include
#include
usingnamespacestd;
intmain()
{
inta[10],i,sum=0,n=0;
floatave,ca;
for(i=0;i<10;i++)
{
cin>>a;
sum+=a;
}
ave=sum/10.0;
cout<<"数组平均值为:
"< ca=fabs(a[0]-ave);
for(i=1;i<10;i++)
{
if(fabs(a-ave) {ca=fabs(a-ave);
n=i;
}
}
cout<<"最接近平均值的元素为第"< system("pause");
return0;
}
第四题:
//长整数相加
#include
#include
#include
#defineLENGTH10
usingnamespacestd;
classClargeInt
{
staticconstintSIZE=LENGTH;//整数最长100位
intelement[SIZE];
int_len; // 0<_len int_OverFlow;//溢出标记,0无溢出,1溢出
public:
int*GetPosition()const
{
return(int*)element;
}
ClargeInt(void):
_OverFlow(0)
{
inti;
for(i=0;i {
element=0;
_len=SIZE;
}
}
ClargeInt(int*data,intlen):
_OverFlow(0)
{
inti;
for(i=0;i {
element=*(data+i);
}
_len=len;
}
ClargeInt(constClargeInt&a)
{
inti;
for(i=0;i {
element=a.element;
}
_len=a._len;
}
voidprint(void)
{
inti;
for(i=_len-1;i>=0;i--)
printf("%1d",element);
printf("\n");
}
intGetLen()const
{
return_len;
}
intSetOverFlow(intOverFlow)
{
_OverFlow=OverFlow;
}
intGetOverFlow()
{
return _OverFlow;
}
constClargeInt&operator+(constClargeInt&RValue)
{
ClargeInt temp;
intLlen,Rlen,ActualLen;
intvalue;
intCarryFlag; //进位标记,产生进位为1,没有进位为0
CarryFlag=0;
if(_len>=RValue._len)
{
ActualLen=_len;
for(inti=0;i {
if(i {
value=element+RValue.element+CarryFlag;
}
else
{
value=element+CarryFlag;
}
temp.element=value%10;
CarryFlag=value/10;
}
}
else
{
ActualLen=RValue._len;
for(inti=0;i {
if(i<_len)
{
value=element+RValue.element+CarryFlag;
}
else
{
value=RValue.element+CarryFlag;
}
temp.element=value%10;
CarryFlag=value/10;
}
} //theendofif
temp._len=ActualLen;
if(CarryFlag==1)
{
if(ActualLen {
temp.element[ActualLen]=CarryFlag;
temp._len=ActualLen+1;
}
else
{
temp._OverFlow=1;
temp._len=ActualLen;
}
}
returntemp;
} //theendofoperator+
};
intmain()
{
intdata1[LENGTH];
intdata2[LENGTH];
intlen1,len2;
inti;
//Seedtherandom-numbergeneratorwithcurrenttimesothat
//thenumberswillbedifferenteverytimewerun.
//
srand((unsigned)time(NULL));
for(i=0;i {
data1=rand()%10;
}
for(i=0;i {
data2=rand()%10;
}
//InitArray(data1,len1);
//InitArray(data2,len2);
ClargeInt Value1(data1,LENGTH);
ClargeInt Value2(data2,LENGTH-5);
ClargeInt SumValue;
SumValue=Value1+ Value2;
Value1.print();
Value2.print();
if(SumValue.GetOverFlow()==1)
{
cout<<"相加之和,超出整数长度表示范围!
"< }
SumValue.print();
SumValue=Value2+Value1;
if(SumValue.GetOverFlow()==1)
{
cout<<"相加之和,超出整数长度表示范围!
"< }
SumValue.print();
system("pause");
return0;
}
第五题:
#include"stdio.h"
#include"stdlib.h"
#include"time.h"
intmain()
{int b[100];
int a[10][10];
int i=0,r,k,n,m,l,x,y,bomb;
for(i=0;i<100;i++)
{b=i;
a[i/10][i%10]=20;
};
i=0;
k=100;
while(i<10)
{srand(time(NULL));
r=rand()%k;
a[b[r]/10][b[r]%10]=-1;
while(r<=k-2)
{b[r]=b[r+1];
r++;
};
k--;
i++;
};
for(i=0;i<10;i++)
for(r=0;r<10;r++)
{if(a[r]!
=-1)
{k=(((i-1)>0)?
(i-1):
0);
n=(((i+1)<9)?
(i+1):
9);
m=(((r-1)>0)?
(r-1):
0);
l=(((r+1)<9)?
(r+1):
9);
x=k;
bomb=0;
while(x<=n)
{y=m;
while(y<=l)
{if(a[x][y]==-1)
bomb++;
y++;
};
x++;
};
a[r]=bomb;
};
};
i=0;
while(i<10)
{printf("\n");
r=0;
while(r<10)
{printf("%4d",a[r]);
r++;
};
i++;
};
getch();
return0;
}
希望对大家有用!