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

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    
const double dt;
InBlock.gif    cout
<<pi<<endl;
InBlock.gif    
return 0;
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.gif    RIGHT, DOWN , LEFT, UP
ExpandedBlockEnd.gif}
;
None.gif
None.gif
//模拟整个过程
None.gif
void Simulate(int n)
ExpandedBlockStart.gifContractedBlock.gif
dot.gif{
InBlock.gif    
int x, y;
InBlock.gif    x 
= y = (n - 1/ 2//1的位置
InBlock.gif
    data[x][y] = 1;
InBlock.gif    
int len = 1;
InBlock.gif    
int count = 0;
InBlock.gif    
int num = 2;
InBlock.gif    DIRECTION dir 
= RIGHT;
InBlock.gif    
while(num  <= n * n)
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        
for(int i = 0; i < len; i++)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
switch(dir)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif            
case LEFT:
InBlock.gif                
--y;    break;
InBlock.gif            
case RIGHT:
InBlock.gif                
++y;     break;
InBlock.gif            
case UP:
InBlock.gif                
--x;    break;
InBlock.gif            
case DOWN:
InBlock.gif                
++x;    break;
InBlock.gif            
default:    break;
ExpandedSubBlockEnd.gif            }

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

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

InBlock.gif        dir 
= (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    
int i, j;
InBlock.gif    
for(i = 0; i < n; i++)
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        cout 
<< data[i][0];
InBlock.gif        
for(j = 1; j < n; j++)
InBlock.gif            cout 
<< "\t" << data[i][j];
InBlock.gif        cout 
<< 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    
int m = max(abs(x), abs(y));
InBlock.gif    
int rightBottom = m * m * 4 - 2 * m + 1;
InBlock.gif    
int value = 0;
InBlock.gif    
if(x == -m)
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        value 
= rightBottom + 2 * m + m - y;
ExpandedSubBlockEnd.gif    }

InBlock.gif    
else if( y == m)
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        value 
= rightBottom + m - x;
ExpandedSubBlockEnd.gif    }

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

InBlock.gif    
else if( x == m )
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        value 
= rightBottom - (m - y);
ExpandedSubBlockEnd.gif    }

InBlock.gif    
InBlock.gif
InBlock.gif    
return value;
ExpandedBlockEnd.gif}

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

ExpandedBlockEnd.gif}

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

InBlock.gif        
else
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            Simulate(n);
InBlock.gif            Output(n);
InBlock.gif            cout 
<< "*******************" << endl;
InBlock.gif            TestPos(n);
ExpandedSubBlockEnd.gif        }

ExpandedSubBlockEnd.gif    }

InBlock.gif
InBlock.gif    
return 0;
ExpandedBlockEnd.gif}

None.gif