| 我的分类 |
|
|
|
|
|
|
|
金山训练营入学考试题
|
1 编写函数实现十进制正整数到十四进制数的转换,在屏幕输出转换结果。 说 明:用0, 1, 2, 3,....., 8, 9, A, B, C, D表示十四进制的基本的14个数。 例:键盘输入14,屏幕输出10。
2 结构RECT可以表示一个平面上的矩形区域 : struct RECT { int left, top, right, bottom; }; 如果给定两个矩形区域A和B,请构造一个函数,用若干矩形区域表示出区域A被B切掉之后剩余的区域,并在屏幕输出结果。例如,区域 (0, 0)-(2, 2) 被 (1, 1)-(3, 3) 切掉之后,剩余的两个区域可以表示为 (0, 0)-(2, 1) 和 (0, 1)-(1, 2),或者是 (0, 0)-(1, 2) 和 (1, 0)-(2, 1)(两种表示方式均可)。
3 文件名:3.cpp 功 能:编程实现猜词游戏 说 明:对于单词“hello”,程序提示输出:?????,等待用户输入。 用户输入时,若单词包含该字母,如“l”,则程序显示输出“??ll?”; 若单词不含该字母,如“a”,则程序提示用户猜错。 继续等待用户输入,直到用户猜出全部字母,或输入错误次数超过最大允许出错次数,游戏结束。 条 件:1) 单词由程序内定,由全小写字母组成 2) 提示输出问号数量等于单词长度 3) 最大允许出错次数等于单词长度
哪位高手写一下代码....小弟做了..感觉不是很好...想看一下高手是怎么写的?? ========================================================================
1: void fun( int input) { if (input >= 14) fun(input/14); printf("%c","0123456789ABCD"[input%14]); }
2.0
#include <iostream> using namespace std ;
class Rect { public: Rect(int x, int y , int m ,int n) { left = x ; top = y ; right = m ; bottom = n ; } int left, top, right, bottom; };
void printArea(int x, int y , int m ,int n) { cout < <"(" < <x < <"," < <y < <")" < <"-" < <"(" < <m < <"," < <n < <")" < <endl; }
void intersection(const Rect &r1, const Rect &r2) { //有15种相切情况,穷举每一种情况。
if(r2.bottom < r1.bottom && r2.bottom > r1.top) { if(r2.left <= r1.left && r2.right >= r1.right && r2.top <= r1.top) { printArea(r1.left , r2.bottom, r1.right , r1.bottom) ;//1 return ; } else if(r2.left <= r1.left && r2.right >= r1.right && r2.top > r1.top) { printArea(r1.left , r1.top , r1.right, r2.top) ;//2 printArea(r1.left , r2.bottom, r1.right , r1.bottom) ; return ; } else if(r2.right < r1.right && r2.right > r1.left && r2.left <= r1.left && r2.top <= r1.top) { printArea(r1.left , r2.bottom , r2.right , r1.bottom) ;//3 printArea(r2.right ,r1.top , r1.right ,r1.bottom ) ; return ; } else if(r2.left > r1.left && r2.left < r1.right && r2.right >= r1.right && r2.top <= r1.top) { printArea(r1.left ,r1.top , r2.left ,r1.bottom) ;//4 printArea(r2.left ,r2.bottom ,r1.right ,r1.bottom) ; return ; } else if(r2.left > r1.left && r2.right < r1.right && r2.top <= r1.top) { printArea(r1.left,r1.top , r2.left ,r2.bottom) ;//5 printArea(r2.right , r1.top , r1.right ,r2.bottom) ; printArea(r1.left , r2.bottom , r1.right ,r1.bottom) ; return ; } } if(r2.top > r1.top && r2.top < r1.bottom && r2.bottom >= r1.bottom ) { if(r2.left <= r1.left && r2.right >= r1.right ) { printArea(r1.left ,r1.top ,r1.right ,r2.top) ;//6 return ; } else if(r2.right < r1.right && r2.right > r1.left && r2.left <= r1.left) { printArea(r1.left , r1.top ,r2.left , r1.bottom) ;//7 printArea(r2.left ,r2.bottom ,r1.right ,r1.bottom) ; return; } else if(r2.left > r1.left && r2.left < r1.right && r2.right >= r1.right) { printArea(r1.left ,r1.top , r2.left , r1.bottom) ;//8 printArea(r2.left , r1.top ,r1.right ,r2.top) ; return ; } else if(r2.left > r1.left && r2.right < r1.right ) { printArea(r1.left ,r1.top ,r1.right ,r2.top) ;//9 printArea(r1.left ,r2.top ,r2.left , r1.bottom) ; printArea(r2.right ,r2.top ,r1.right ,r1.bottom) ; return ; } } if(r2.right > r1.left && r2.right < r1.right) { if(r2.top <= r1.top && r2.bottom >= r1.bottom && r2.left <= r1.left) { printArea(r2.right ,r1.top ,r1.right,r1.bottom) ;//10 return ; } else if(r2.top <= r1.top && r2.bottom >= r1.bottom && r2.left > r1.left) { printArea(r1.left ,r1.top ,r2.left ,r1.bottom) ;//11 printArea(r2.right ,r1.top ,r1.right ,r1.bottom) ; return ; } else if(r2.top > r1.top && r2.bottom < r1.bottom && r2.left <= r1.left) { printArea(r1.left ,r1.top ,r2.right ,r2.top) ;//12 printArea(r1.left ,r2.bottom ,r2.right , r1.bottom) ; printArea(r2.right , r1.top ,r1.right ,r1.bottom) ; return ; } } if(r2.left < r1.right && r2.left >r1.left && r2.right >= r1.right) { if(r2.top <= r1.top && r2.bottom >= r1.bottom ) { printArea(r1.left ,r1.top , r2.left , r2.bottom) ;//13 return ; } else if(r2.top > r1.top && r2.bottom < r1.bottom) { printArea(r1.left , r1.top ,r2.left , r1.bottom) ;//14 printArea(r2.left ,r1.top ,r1.right ,r2.top) ; printArea(r2.left ,r2.bottom , r1.right ,r1.bottom) ; return ; } } if(r2.left > r1.left && r2.right < r1.right && r2.top > r1.top && r2.bottom < r1.bottom) { printArea(r1.left ,r1.top ,r2.left ,r2.top) ;//15 printArea(r2.left,r1.top ,r2.right ,r2.top) ; printArea(r2.right,r1.top ,r1.right ,r2.top) ; printArea(r1.left , r2.top ,r2.left ,r2.bottom) ; printArea(r2.right ,r2.top ,r1.right ,r2.bottom) ; printArea(r1.left , r2.bottom , r2.left , r1.bottom) ; printArea(r2.left ,r2.bottom , r2.right , r1.bottom) ; printArea(r2.right , r2.bottom , r1.right,r1.bottom) ; return ; }
cout < <"不相切!" < <endl;
}
int main() { Rect r1(0,0,2,2) ; Rect r2(1,1,3,3) ; //Rect r1(0,0,3,3) ; //Rect r2(1,1,2,2) ; //Rect r1(0,0,3,3) ; //Rect r2(0,1,3,2) ; intersection(r1,r2) ; return 0 ; }
3.0
#include <iostream> using namespace std;
#include <string>
void guessword(char * srcword){ int len = strlen(srcword); if(len==0) return;
char * buf = new char [len+1]; memset(buf, 0x00, len+1); int i; for(i=0; i<len; i++){ buf[i] = '?'; } cout<<buf<<endl;
int errortime = 0; do{ char chuser; cin>>chuser;
bool errorflag = true; for(i=0; i<len; i++){ if(srcword[i] == chuser){ errorflag = false; buf[i] = chuser; } } cout<<buf<<endl; if(errorflag) // 说明这次猜错了 errortime ++; if(errortime == len){ cout<<"Error! You have tried so many times!"<<endl; break; }
for(i=0; i<len; i++){ if(buf[i]=='?') break; } if(i==len){ cout<<"Congratulations! You are right!"<<endl; break; } }while(1);
delete [] buf; }
void main(){ char * srcword = "appreciate"; guessword(srcword); }
|
|
|
原文地址
http://topic.csdn.net/u/20080517/21/8606a5d6-9c07-4ebc-a7bb-243af402e20b.html?seed=1938171927
|
|
发表于: 2008-05-19,修改于: 2008-05-19 15:30 已浏览492次,有评论1条
推荐
投诉
|
|
|
|
网友评论 |
|
本站网友 | 时间:2008-06-16 14:46:05 IP地址:116.60.129.★ |
|
|
呵呵,看了看题目,有点意思,感觉你第3题做的还行,我就把前两题做了一下,代码水平有限,别介意,只是觉得思路和你有很大不同,有什么问题可以加我QQ,共同讨论:QQ:15483930
用的vc6.0编译成功
第1题:
#include <string>
#include <iostream>
#include <stdlib.h>
using namespace std;
void show_tento14(int n)
{
int r;//余数
string out14num;//要输出的14进制数的倒置
char temps[2];
while(n>0)//辗转相除法
{
r = n%14;
n /= 14;
switch(r)
{
case 10:
out14num += 'A';
break;
case 11:
out14num += 'B';
break;
case 12:
out14num += 'C';
break;
case 13:
out14num += 'D';
break;
default:
out14num += itoa(r, temps, 10);
break;
}
}
//输出
for (int i=out14num.size()-1; i>=0; --i)
{
cout<<out14num[i];
}
cout<<endl;
}
int main()
{
int n = 14;
show_tento14(n);
return 0;
}
第2题:
#include <iostream>
using namespace std;
struct rect
{
int left;
int top;
int right;
int down;
rect(int l = 0, int t = 0, int r = 0, int d = 0)
{
left = l;
top = t;
right = r;
down = d;
}
void setrect(int l, int t, int r,int d)
{
left = l;
top = t;
right = r;
down = d;
}
//判断是否为区域矩形
bool IsRect()
{
if (left>=right || top>=down)
{
return false;//为空
}
else
{
return true;
}
}
//将矩形设置为非区域矩形,即使其分量置零
void SetZero()
{
left = top = right = down;
}
bool IsEqualRect(rect &B)
{
if (left == B.left &&
top == B.top &&
right == B.top &&
down == B.down)
{
return true;
}
else
{
return false;
}
}
//求出矩形A和B的公共区域
rect commonrect(rect &B)
{
rect C;
C.left = (left>B.left ? left:B.left);
C.top = (top>B.top ? top:B.top);
C.right = (right<B.right ? right:B.right);
C.down = (down<B.down ? down:B.down);
return C;
}
void display()
{
if (!IsRect())
{
return;//若为非区域,直接返回
}
else
{
cout<<"("<<left<<","<<top<<") "
<<"("<<right<<","<<down<<")"<<endl;
}
}
};
//表示一个矩形减去其内部一块矩形后的四个矩形,若相应的分矩形为0矩形,表示没有这块
struct last_region
{
rect A;
rect B;
rect C;
rect D;
void display()
{
A.display();
B.display();
C.display();
D.display();
}
};
//求出一个矩形减去其内部一个矩形后剩余的矩形
last_region LastRegion(rect R, rect subR)
{
last_region lastregion;
if (!subR.IsRect())//如果sunR为空
{
lastregion.A = R;
lastregion.B.SetZero();
lastregion.C.SetZero();
lastregion.D.SetZero();
return lastregion;
}
lastregion.A.setrect(R.left, R.top, R.right, subR.top);
lastregion.B.setrect(R.left, subR.top, subR.left, subR.down);
lastregion.C.setrect(subR.right, subR.top, R.right, subR.down);
lastregion.D.setrect(R.left, subR.down, R.right, R.down);
return lastregion;
}
//显示A被B切割后的区域
void show_region(rect A, rect B)
{
rect C = A.commonrect(B);//公共部分
last_region lastregion = LastRegion(A, C);
lastregion.display();
}
int main()
{
rect r1(0,0,2,2);
rect r2(1,1,3,3);
//Rect r1(0,0,3,3) ;
//Rect r2(1,1,2,2) ;
//Rect r1(0,0,3,3) ;
//Rect r2(0,1,3,2) ;
show_region(r1, r2);
return 0;
}
|
|
Blog作者的回复:
呵呵..这个贴是综合csdn上N个人的成果.并非我原创的.只是觉得有学习价值.就贴过来了.谢谢你的热心回复..
|
|
|
|
|
| |