笔试题集锦(2)

1 下面代码有哪些错误?(台湾某公司0512月笔试题)

<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" />

None.gif #include < iostream >
None.gif
using namespace std;
None.gif
None.gif
int main()
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
InBlock.gif
int&t1;
InBlock.gif
int*pi;
InBlock.gif
*pi=3;
InBlock.gif
constdoubledt;
InBlock.gifcout
<<pi<<endl;
InBlock.gif
return0;
ExpandedBlockEnd.gif}

ContractedBlock.gifExpandedBlockStart.gif
答案 #region答案
InBlock.gif
1引用不能为空,必须在定义时同时初始化
InBlock.gif
2声明了一个整型指针,但没有指向实际的地址,因此赋值的操作为出错
InBlock.gif
3常量定义时应该同时初始化
ExpandedBlockEnd.gif
#endregion

None.gif

2下面是一个蛇型矩阵

21 22 23。。。

20 7 8 9 10

19 6 1 2 11

18 5 4 3 12

17 16 15 14 13


1点的坐标为(00),X方向向右为正,y方向向下为正,如:7的坐标为(-1-1),2的坐标为(01),编程实现输入任意一坐标(x,y,输出所对应的数.(诺基亚05年笔试题).

None.gif #include < iostream >
None.gif#include
< cstdlib >
None.gif#include
< algorithm >
None.gif
None.gif
using namespace std;
None.gif
None.gif
const int N = 100 ;
None.gif
None.gif
int data[N + 1 ][N + 1 ];
None.gif
None.gif
enum DIRECTION
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
InBlock.gifRIGHT,DOWN,LEFT,UP
ExpandedBlockEnd.gif}
;
None.gif
None.gif
// 模拟整个过程
None.gif
void Simulate( int n)
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
InBlock.gif
intx,y;
InBlock.gifx
=y=(n-1)/2;//1的位置
InBlock.gif
data[x][y]=1;
InBlock.gif
intlen=1;
InBlock.gif
intcount=0;
InBlock.gif
intnum=2;
InBlock.gifDIRECTIONdir
=RIGHT;
InBlock.gif
while(num<=n*n)
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif{
InBlock.gif
for(inti=0;i<len;i++)
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif{
InBlock.gif
switch(dir)
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif{
InBlock.gif
caseLEFT:
InBlock.gif
--y;break;
InBlock.gif
caseRIGHT:
InBlock.gif
++y;break;
InBlock.gif
caseUP:
InBlock.gif
--x;break;
InBlock.gif
caseDOWN:
InBlock.gif
++x;break;
InBlock.gif
default:break;
ExpandedSubBlockEnd.gif}

InBlock.gifdata[x][y]
=num++;
ExpandedSubBlockEnd.gif}

InBlock.gifcount
++;
InBlock.gif
if(count==2)
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif{
InBlock.gifcount
=0;
InBlock.giflen
++;
ExpandedSubBlockEnd.gif}

InBlock.gifdir
=(DIRECTION)((dir+1)%4);
ExpandedSubBlockEnd.gif}

ExpandedBlockEnd.gif}

None.gif
None.gif
// 打印螺旋矩阵
None.gif
void Output( int n)
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
InBlock.gif
inti,j;
InBlock.gif
for(i=0;i<n;i++)
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif{
InBlock.gifcout
<<data[i][0];
InBlock.gif
for(j=1;j<n;j++)
InBlock.gifcout
<<"/t"<<data[i][j];
InBlock.gifcout
<<endl;
ExpandedSubBlockEnd.gif}

ExpandedBlockEnd.gif}

None.gif
None.gif
// 以(1,1)所在位置作为原点,向右作为x正半轴,向下作为y正半轴
None.gif
int GetValue( int x, int y)
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
InBlock.gif
intm=max(abs(x),abs(y));
InBlock.gif
intrightBottom=m*m*4-2*m+1;
InBlock.gif
intvalue=0;
InBlock.gif
if(x==-m)
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif{
InBlock.gifvalue
=rightBottom+2*m+m-y;
ExpandedSubBlockEnd.gif}

InBlock.gif
elseif(y==m)
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif{
InBlock.gifvalue
=rightBottom+m-x;
ExpandedSubBlockEnd.gif}

InBlock.gif
elseif(y==-m)
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif{
InBlock.gifvalue
=rightBottom+4*m+x+m;
ExpandedSubBlockEnd.gif}

InBlock.gif
elseif(x==m)
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif{
InBlock.gifvalue
=rightBottom-(m-y);
ExpandedSubBlockEnd.gif}

InBlock.gif
InBlock.gif
InBlock.gif
returnvalue;
ExpandedBlockEnd.gif}

None.gif
None.gif
void TestPos( int n)
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
InBlock.gif
inti,j;
InBlock.gif
for(i=0;i<n;i++)
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif{
InBlock.gifcout
<<GetValue(0-(n-1)/2,i-(n-1)/2);
InBlock.gif
for(j=1;j<n;j++)
InBlock.gifcout
<<"/t"<<GetValue(j-(n-1)/2,i-(n-1)/2);
InBlock.gifcout
<<endl;
ExpandedSubBlockEnd.gif}

ExpandedBlockEnd.gif}

None.gif
None.gif
int main()
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
InBlock.gif
intn;
InBlock.gif
while(cin>>n)
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif{
InBlock.gif
if(n<=0||n>100)
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif{
InBlock.gifcerr
<<"Sizeerror!"<<endl;
InBlock.gif
break;
ExpandedSubBlockEnd.gif}

InBlock.gif
else
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif{
InBlock.gifSimulate(n);
InBlock.gifOutput(n);
InBlock.gifcout
<<"*******************"<<endl;
InBlock.gifTestPos(n);
ExpandedSubBlockEnd.gif}

ExpandedSubBlockEnd.gif}

InBlock.gif
InBlock.gif
return0;
ExpandedBlockEnd.gif}

None.gif
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值