1) 下面代码有哪些错误?(台湾某公司05年12月笔试题)
#include
<
iostream
>
using
namespace
std;
![](/Images/OutliningIndicators/None.gif)
int
main()
![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
{
int &t1;
int *pi;
*pi = 3;
const double dt;
cout<<pi<<endl;
return 0;
}
![](/Images/OutliningIndicators/ContractedBlock.gif)
答案
#region 答案
1 引用不能为空,必须在定义时同时初始化
2 声明了一个整型指针,但没有指向实际的地址,因此赋值的操作为出错
3 常量定义时应该同时初始化
#endregion
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点的坐标为(0,0),X方向向右为正,y方向向下为正,如:7的坐标为(-1,-1),2的坐标为(0,1),编程实现输入任意一坐标(x,y),输出所对应的数.(诺基亚05年笔试题).
#include
<
iostream
>
#include
<
cstdlib
>
#include
<
algorithm
>
![](/Images/OutliningIndicators/None.gif)
using
namespace
std;
![](/Images/OutliningIndicators/None.gif)
const
int
N
=
100
;
![](/Images/OutliningIndicators/None.gif)
int
data[N
+
1
][N
+
1
];
![](/Images/OutliningIndicators/None.gif)
enum
DIRECTION
![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
{
RIGHT, DOWN , LEFT, UP
}
;
![](/Images/OutliningIndicators/None.gif)
//
模拟整个过程
void
Simulate(
int
n)
![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
{
int x, y;
x = y = (n - 1) / 2; //1的位置
data[x][y] = 1;
int len = 1;
int count = 0;
int num = 2;
DIRECTION dir = RIGHT;
while(num <= n * n)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
for(int i = 0; i < len; i++)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
switch(dir)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
case LEFT:
--y; break;
case RIGHT:
++y; break;
case UP:
--x; break;
case DOWN:
++x; break;
default: break;
}
data[x][y] = num++;
}
count++;
if(count == 2)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
count = 0;
len++;
}
dir = (DIRECTION)((dir + 1) % 4);
}
}
![](/Images/OutliningIndicators/None.gif)
//
打印螺旋矩阵
void
Output(
int
n)
![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
{
int i, j;
for(i = 0; i < n; i++)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
cout << data[i][0];
for(j = 1; j < n; j++)
cout << "/t" << data[i][j];
cout << endl;
}
}
![](/Images/OutliningIndicators/None.gif)
//
以(1,1)所在位置作为原点,向右作为x正半轴,向下作为y正半轴
int
GetValue(
int
x,
int
y)
![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
{
int m = max(abs(x), abs(y));
int rightBottom = m * m * 4 - 2 * m + 1;
int value = 0;
if(x == -m)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
value = rightBottom + 2 * m + m - y;
}
else if( y == m)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
value = rightBottom + m - x;
}
else if(y == -m)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
value = rightBottom + 4 * m + x + m;
}
else if( x == m )
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
value = rightBottom - (m - y);
}
![](/Images/OutliningIndicators/InBlock.gif)
return value;
}
![](/Images/OutliningIndicators/None.gif)
void
TestPos(
int
n)
![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
{
int i, j;
for(i = 0; i < n; i++)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
cout << GetValue(0 - (n - 1) / 2, i - (n - 1) / 2);
for(j = 1; j < n; j++)
cout << "/t" << GetValue(j - (n - 1) / 2, i - (n - 1) / 2);
cout << endl;
}
}
![](/Images/OutliningIndicators/None.gif)
int
main()
![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
{
int n;
while(cin >> n)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
if(n <= 0 || n > 100)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
cerr << "Size error!" << endl;
break;
}
else
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
Simulate(n);
Output(n);
cout << "*******************" << endl;
TestPos(n);
}
}
![](/Images/OutliningIndicators/InBlock.gif)
return 0;
}