VIA 10.27笔试火热出炉
职位:Software Engineer(Application Development)
1. 看一段代码,说明功能
我的答案:将一个字符串拷贝到另一个
2.
C++ code title:
#include <iostream> using namespace std; #define Add(pa) {pa++; (*pa)++; } void Add(int *pa) { pa++; (*pa)++; } int main() { int a[3]= {0, 1 , 2}; int *pa = a; Add(pa) Add(pa) cout << a[0] << " " << a[1] << " " << a[2] << endl; pa = a; Add(pa); Add(pa); cout << a[0] << " " << a[1] << " " << a[2] << endl; system("PAUSE"); return 0; }
打印结果:
0 2 3
0 3 2
3.
char *s1="hello,";
char s2[]="world!";
两者有何不同,如不同请说明
4.
C语言中auto, regist, static, extern的作用
5.
C++ code title:
#include <iostream> using namespace std; int main() { int x = 2007, countx = 0; while(x) { countx++; x=x&(x-1); } cout << countx; return 0; }
打印结果:9
6.打印zigzag矩阵
C++ code title:
#include <iostream> using namespace std; int * zigzag(int n) { int count = 0; //第 count条 45°斜线 ,一共有2*n-1条 int num = 0; int direction = 0;//0- down->up 1 - up->down int *arr = new int[n*n]; while(count < 2*n-1) { direction = count%2; for(int i=0; i <= count; ++i) { if(i >= n || count-i >= n)//下标越界 continue; if(direction == 0) { arr[(count-i)*n+i] = num; } else { arr[i*n+(count-i)] = num; } num++; } count++; } return arr; } int main() { int *arr;//= new int[16]; int n = 5; arr = zigzag(n); for(int i=0; i < n; ++i) { for(int j=0; j < n; ++j) { cout << arr[i*n+j] << " "; } cout << "\n"; } system("PAUSE"); return 0; }
n=5时,打印
可惜我写错了,考虑东西欠缺啊
7.
8级的楼梯,每步最大可以走3个楼梯,问一共多少种走法?
我填的81种
按递归得到,如果写程序可以用动态规划
1步待走,1种走法
2步待走,2种走法
3步待走,4种走法
n步待走,走法count(n)=count(n-1)+count(n-2)+cout(n-3)
n=8时,count(n)=81;
#include <iostream>
using namespace std;
int count(int n)
{
switch (n)
{
case 1:
return 1; break;
case 2:
return 2; break;
case 3:
return 4; break;
default:
break;
}
return count(n-1)+count(n-2)+count(n-3);
}
int main()
{
cout << count(8);
system("PAUSE");
return 0;
}
using namespace std;
int count(int n)
{
switch (n)
{
case 1:
return 1; break;
case 2:
return 2; break;
case 3:
return 4; break;
default:
break;
}
return count(n-1)+count(n-2)+count(n-3);
}
int main()
{
cout << count(8);
system("PAUSE");
return 0;
}
8.
将0-9随机写入到一个3x3矩阵,要求将矩阵中值为1的行和列都变成1
#include <iostream>
using namespace std;
void foo(int *mat, size_t dim)
{
int flag[9] = {0};
int row=0;
int col=0;
for(int i=0; i < dim*dim; ++i)
{
if(mat[i] == 1)
{
flag[i] = 1;
}
}
for(int i=0; i < 9; ++i)
{
if(flag[i] == 1)
{
row=i/dim;
col=i%dim;
for(int j=0; j < dim; ++j)
{
mat[row*dim+j]=1;
mat[j*dim+col]=1;
}
}
}
}
int main()
{
int mat[9]={2, 5, 6,
4, 1, 1,
7, 0, 9};
cout << "The original matrix:\n";
for(int i=0; i < 3; ++i)
{
for(int j=0; j < 3; ++j)
{
cout << mat[i*3+j] << " ";
}
cout << "\n";
}
foo(mat, 3);
cout << "The converted matrix:\n";
for(int i=0; i < 3; ++i)
{
for(int j=0; j < 3; ++j)
{
cout << mat[i*3+j] << " ";
}
cout << "\n";
}
system("PAUSE");
return 0;
}
using namespace std;
void foo(int *mat, size_t dim)
{
int flag[9] = {0};
int row=0;
int col=0;
for(int i=0; i < dim*dim; ++i)
{
if(mat[i] == 1)
{
flag[i] = 1;
}
}
for(int i=0; i < 9; ++i)
{
if(flag[i] == 1)
{
row=i/dim;
col=i%dim;
for(int j=0; j < dim; ++j)
{
mat[row*dim+j]=1;
mat[j*dim+col]=1;
}
}
}
}
int main()
{
int mat[9]={2, 5, 6,
4, 1, 1,
7, 0, 9};
cout << "The original matrix:\n";
for(int i=0; i < 3; ++i)
{
for(int j=0; j < 3; ++j)
{
cout << mat[i*3+j] << " ";
}
cout << "\n";
}
foo(mat, 3);
cout << "The converted matrix:\n";
for(int i=0; i < 3; ++i)
{
for(int j=0; j < 3; ++j)
{
cout << mat[i*3+j] << " ";
}
cout << "\n";
}
system("PAUSE");
return 0;
}
9.二维数组问题
#include <iostream>
using namespace std;
int main()
{
int a[3][3] = { {0,1,2},{3, 4, 5}};
int *b = &a[0][0];
int *b1= &a[1][0];
int **c = &b;
int **c1= &b1;
cout << b[0] << "\n";
cout << b1[0] << "\n";
cout << c[0][1] << "\n";
cout << c1[1][2] << "\n";
system("PAUSE");
return 0;
}
using namespace std;
int main()
{
int a[3][3] = { {0,1,2},{3, 4, 5}};
int *b = &a[0][0];
int *b1= &a[1][0];
int **c = &b;
int **c1= &b1;
cout << b[0] << "\n";
cout << b1[0] << "\n";
cout << c[0][1] << "\n";
cout << c1[1][2] << "\n";
system("PAUSE");
return 0;
}
10。
已知线段表示:
struct line
{
int startx;
int starty;
int endx;
int endy;
};
line theLines[32];
写程序求: 对于每条线段,与之相交的线段数
参见《实用算法设计与程序设计》