当前位置:文档之家› 首届程序设计大赛题目及答案

首届程序设计大赛题目及答案

首届程序设计大赛题目及答案程序设计大赛题目1. 硬币兑换:用一元人民币兑换一分、二分和五分的硬币,编程求出所有兑换方法,并打印每种情况下各面值硬币需几枚?(10分)2. 旅馆里有一百个房间,从1到100编了号。

第一个服务员把所有的房间门都打开了,第二个服务员把所有编号是2的倍数的房间“相反处理”,第三个服务员把所有编号是3的倍数的房间作“相反处理”,……,以后每个服务员都是如此。

问第100个服务员来过后,哪几扇门是打开的。

(所谓“相反处理”是:原来开着的门关上,原来关上的门打开。

) (15分)3. 整型数组,长度为10,输入数组各元后,求数组各元的平均值,并找出最接近平均值的那个元素。

打印输出上述两个结果,用逗号隔开,不要有其它字符。

(20分)4. 编程求两个很长的整数之和,比如两个20位十进制整数。

(很长的整数指无法用long型存储的数)(25分)例如:12345678900987654321+12345678900987654321=246913578019753086425.编写布雷程序。

(30分)在一个10*10的方格中,随机分布10个地雷,并在其它没有地雷的格中显示该方格周围相邻的方格中共有几枚地雷。

样例输出:(图中-1的位置表示地雷,其它值表示该位置相邻的八个格子中的地雷数)答案:第一题:#include <iostream>using namespace std;int main(){int i,j,k;for(i=1;i<=20;i++)for(j=1;j<=50;j++){k=100-5*i-2*j;if(k>0)cout<<"五分硬币"<<i<<"枚,二分硬币"<<j<<"枚,一分硬币"<<k<<"枚"<<endl;}system("pause");return 0;}第二题:#include<iostream>using namespace std;int main(){int i,j; //i为服务员编号,j为房间编号int a[101];for(int t=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<<i<<",";system("pause");return 0;}第三题:#include <iostream>#include <math.h>using namespace std;int main(){int a[10],i,sum=0,n=0;float ave,ca;for(i=0;i<10;i++){cin>>a;sum+=a;}ave=sum/10.0;cout<<"数组平均值为:"<<ave<<endl;ca=fabs(a[0]-ave);for(i=1;i<10;i++){if( fabs(a-ave)<ca){ ca=fabs(a-ave);n=i;}}cout<<"最接近平均值的元素为第"<<n+1<<"个"<<a[n]<<endl; system("pause");return 0;}第四题://长整数相加#include <iostream>#include <stdlib.h>#include <time.h>#define LENGTH 10using namespace std;class ClargeInt{static const int SIZE = LENGTH; //整数最长100位int element[SIZE];int _len; // 0 < _len < SIZE 整数的实际位数int _OverFlow; //溢出标记,0 无溢出, 1 溢出public:int* GetPosition() const{return (int*)element;}ClargeInt(void):_OverFlow (0){int i;for (i = 0; i<SIZE; i++){element = 0;_len = SIZE;}}ClargeInt(int *data, int len):_OverFlow(0) {int i;for (i = 0; i < len; i++){element = *(data+i);}_len = len;}ClargeInt(const ClargeInt& a){int i;for (i = 0; i < a._len ; i++){element = a.element;}_len = a._len;}void print(void){int i;for (i = _len - 1 ; i >= 0 ; i--)printf("%1d",element);printf("\n");}int GetLen()const{return _len;}int SetOverFlow(int OverFlow){_OverFlow = OverFlow;}int GetOverFlow(){return _OverFlow ;}const ClargeInt& operator+(const ClargeInt& RValue){ClargeInt temp;int Llen,Rlen,ActualLen;int value;int CarryFlag; //进位标记,产生进位为 1,没有进位为0 CarryFlag = 0;if ( _len >= RValue._len ){ActualLen = _len;for (int i = 0; i < ActualLen; i++){if (i < RValue._len){value = element + RValue.element + CarryFlag; }else{value = element + CarryFlag;}temp.element = value % 10;CarryFlag = value / 10;}}else{ActualLen = RValue._len;for (int i = 0; i < ActualLen; i++){if (i < _len){value = element + RValue.element + CarryFlag; }else{value = RValue.element + CarryFlag;}temp.element = value % 10;CarryFlag = value / 10;}} //the end of iftemp._len = ActualLen ;if (CarryFlag == 1){if (ActualLen < SIZE){temp.element[ActualLen] = CarryFlag;temp._len =ActualLen + 1;}else{temp._OverFlow = 1;temp._len =ActualLen ;}}return temp;} // the end of operator +};int main(){int data1[LENGTH];int data2[LENGTH];int len1, len2;int i;// Seed the random-number generator with current time so that //the numbers will be different every time we run.//srand( (unsigned)time( NULL ) );for (i = 0; i< LENGTH; i++){data1 = rand()%10;}for (i = 0; i< LENGTH-5; 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<<"相加之和,超出整数长度表示范围!" << endl; }SumValue.print();SumValue = Value2 + Value1;if( SumValue.GetOverFlow()==1){cout<<"相加之和,超出整数长度表示范围!" << endl; }SumValue.print();system("pause");return 0;}第五题:#include "stdio.h"#include "stdlib.h"#include "time.h"int main(){ 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();return 0;}。

相关主题