8. Telnet 协议是基于下面哪种协议开发而来的( )
A. TCP
B. UDP
C. TCP and UDP
D. None of above
解释:选A
9. Please choose the correct options for the ISR below: ( )
interrupt double service(double p)
{
return p*p;
}
A. ISR function should not return any value, service() cannot be used as a ISR.
B. ISR function should not accept any parameter, service() cannot be used as a ISR.
C. Service() is a valid ISR.
D. None of above.
解释:
中断服务程序需要满足如下要求:
所以选AB 不是一个ISR
10. 有一组整型数,其中除了 2 个数字以外的其它数字都是俩俩成对出现的,编写程序找
出这 2 个不成对出现的数字。
函数原型:
void search_diff(int array[], int len, int* pa, int* pb);
示例:
void search_diff(int array[], int len, int* pa, int* pb);
int a = 0;
int b = 0;
int array[] = {3, 4, 5, 5, 3, 4, 1, 6, 6, 7, 2, 8, 7, 8};
search_diff(array, sizeof(a)/sizeof(*a), &a, &b); //调用后 a,b 为 1,2 或者 a,b 为
2,1
解释:看到两两成对出现,就要想到抑或;相同的为0,不同的为1;
#include <iostream>
#include <malloc.h>
using namespace std;
int first_one_bit(unsigned int v)
{
int ret = 0;
while( (v != 0) && ((v & 1) == 0) )
{
v = v >> 1;
ret++;
}
return ret;
}
void search_diff(int array[], int len, int* pa, int* pb)
{
if( (array != NULL) && (pa != NULL) && (pb != NULL) )
{
int r = 0;
int flag = 0;
for(int i=0; i<len; i++)
{
r = r ^ array[i];
}
flag = 1 << first_one_bit(r);
*pa = 0;
*pb = 0;
for(int i=0; i<len; i++)
{
if( array[i] & flag )
{
*pa = *pa ^ array[i];
}
else
{
*pb = *pb ^ array[i];
}
}
}
}
int main()
{
int a = 0;
int b = 0;
int array[] = {3, 4, 5, 5, 3, 4, 1, 6, 6, 7, 2, 8, 7, 8};
search_diff(array, sizeof(array)/sizeof(*array), &a, &b);
cout<<a<<" "<<b<<endl;
}
11. 打印一个 N * N 的矩阵,从首坐标(0, 0)开始顺时针依次增大。
示例:5 * 5 矩阵,其中数字 1 的坐标为(0, 0)
1 2 3 4 5
16 17 18 19 6
15 24 25 20 7
14 23 22 21 8
13 12 11 10 9
#include <iostream>
#include <malloc.h>
using namespace std;
template <int N>
class SpinMatrix
{
private:
int m_matrix[N][N];
struct Offset
{
int dx;
int dy;
};
bool valid(int x, int y);
public:
SpinMatrix();
void run();
void println();
int scale();
};
template <int N>
SpinMatrix<N>::SpinMatrix()
{
for(int i=0; i<N; i++)
{
for(int j=0; j<N; j++)
{
m_matrix[i][j] = 0;
}
}
}
template <int N>
bool SpinMatrix<N>::valid(int x, int y)
{
bool ret = true;
ret = ret && ((0 <= x) && (x < N));
ret = ret && ((0 <= y) && (y < N));
ret = ret && (m_matrix[x][y] == 0);
return ret;
}
template <int N>
void SpinMatrix<N>::run()
{
const Offset OFFSET[] = {{0, 1}, {1, 0}, {0, -1}, {-1, 0}};
const int OSLEN = sizeof(OFFSET) / sizeof(*OFFSET);
int cx = 0;
int cy = 0;
int cd = 0;
int i = 1;
do
{
m_matrix[cx][cy] = i;
if( !valid(cx + OFFSET[cd].dx, cy + OFFSET[cd].dy) )
{
cd = (cd + 1) % OSLEN;
}
cx += OFFSET[cd].dx;
cy += OFFSET[cd].dy;
i++;
} while ( i <= N*N );
}
template <int N>
void SpinMatrix<N>::println()
{
for(int i=0; i<N; i++)
{
for(int j=0; j<N; j++)
{
cout<<m_matrix[i][j]<<'\t';
}
cout<<endl;
}
cout<<endl;
}
template <int N>
int SpinMatrix<N>::scale()
{
return N;
}
int main()
{
SpinMatrix<3> sm1;
SpinMatrix<4> sm2;
cout<<"3 * 3: "<<endl;
sm1.run();
sm1.println();
cout<<"4 * 4: "<<endl;
sm2.run();
sm2.println();
}