# 金山2013笔试题含答案

1、请实现这么一个函数：传入一个int值，在屏幕输出类似LED显示屏效果的字母拼图，例如：

import java.util.Scanner;
/*
A, B, C, D 表示数字的各二进制位
a, b, c, d, e, f, g 表示 LED 的各段，为 1 时该段显示，为 0 时该段不显示

a
#######
#     # b
f #  g  #
#######
#     # c
e #     #
#######
d

+---+---+---+---+---+  +---+---+---+---+---+---+---+
|   | A | B | C | D |  | a | b | c | d | e | f | g |
+---+---+---+---+---+  +---+---+---+---+---+---+---+
| 0 | 0 | 0 | 0 | 0 |  | 1 | 1 | 1 | 1 | 1 | 1 | 0 |
+---+---+---+---+---+  +---+---+---+---+---+---+---+
| 1 | 0 | 0 | 0 | 1 |  | 0 | 1 | 1 | 0 | 0 | 0 | 0 |
+---+---+---+---+---+  +---+---+---+---+---+---+---+
| 2 | 0 | 0 | 1 | 0 |  | 1 | 1 | 0 | 1 | 1 | 0 | 1 |
+---+---+---+---+---+  +---+---+---+---+---+---+---+
| 3 | 0 | 0 | 1 | 1 |  | 1 | 1 | 1 | 1 | 0 | 0 | 1 |
+---+---+---+---+---+  +---+---+---+---+---+---+---+
| 4 | 0 | 1 | 0 | 0 |  | 0 | 1 | 1 | 0 | 0 | 1 | 1 |
+---+---+---+---+---+  +---+---+---+---+---+---+---+
| 5 | 0 | 1 | 0 | 1 |  | 1 | 0 | 1 | 1 | 0 | 1 | 1 |
+---+---+---+---+---+  +---+---+---+---+---+---+---+
| 6 | 0 | 1 | 1 | 0 |  | 1 | 0 | 1 | 1 | 1 | 1 | 1 |
+---+---+---+---+---+  +---+---+---+---+---+---+---+
| 7 | 0 | 1 | 1 | 1 |  | 1 | 1 | 1 | 0 | 0 | 0 | 0 |
+---+---+---+---+---+  +---+---+---+---+---+---+---+
| 8 | 1 | 0 | 0 | 0 |  | 1 | 1 | 1 | 1 | 1 | 1 | 1 |
+---+---+---+---+---+  +---+---+---+---+---+---+---+
| 9 | 1 | 0 | 0 | 1 |  | 1 | 1 | 1 | 1 | 0 | 1 | 1 |
+---+---+---+---+---+  +---+---+---+---+---+---+---+
*/
public class Num2LEDTest {

public static void main(String[] args) {
LED led = new LED();
Scanner in=new Scanner(System.in);
String LEDStr=in.nextLine();
char[][] chss = led.getLED(LEDStr);
LED.print(chss);
}
}

class LED {

/**
* 每个 LED 的大小，可以进行调整
*/
public final static int ROW = 7;
public final static int COL = 7;

/**
* 每个 LED 的间隔
*/
private final static int SEPARATOR = 1;

private final static char FILL_CHAR = '#';
private final static char SPACE_CHAR = ' ';

/**
* 工具方法，用于输出 LED
* @param chs
*/
public static void print(char[][] chs) {
for(int i = 0; i < chs.length; i++) {
for(int j = 0; j < chs[i].length; j++) {
System.out.print(chs[i][j]);
}
System.out.println();
}
}

/**
* 根据数字得到 LED 显示数组
* @param num
* @return
*/
public char[][] getLED(String num) {
char[] chs = num.toCharArray();
char[][][] chsss = new char[chs.length][][];
for(int i = 0; i < chs.length; i++) {
chsss[i] = showLed(chs[i] - '0');
}
return putManyLed(chsss);
}

/**
* 将多个 LED 组成一排
* @param chsss
* @return
*/
private char[][] putManyLed(char[][][] chsss) {
if(chsss.length < 1) {
throw new IllegalArgumentException("LED is NULL!");
}
if(chsss.length == 1) {
return chsss[0];
}
char[][] chss = new char[ROW][chsss.length * (COL + SEPARATOR) - SEPARATOR];
for(int i = 0; i < chsss.length; i++) {
int m = i * (COL + SEPARATOR);
for(int j = 0; j < chsss[i].length; j++) {
for(int k = 0; k < chsss[i][j].length; k++) {
chss[j][m + k] = chsss[i][j][k];
}
}
}
for(int i = 0; i < chss.length; i++) {
for(int j = 0; j < chss[i].length; j++) {
if(chss[i][j] != FILL_CHAR) {
chss[i][j] = SPACE_CHAR;
}
}
}
return chss;
}

/**
*
* @param num
* @return
*/
private char[][] showLed(int num) {
boolean[] b = getLed(num);
char[][] chs = new char[ROW][COL];
if(b[0])
for(int i = 0; i < COL; i++) chs[0][i] = FILL_CHAR;
if(b[1])
for(int i = 0; i <= ROW / 2; i++) chs[i][COL - 1] = FILL_CHAR;
if(b[2])
for(int i = ROW / 2; i < ROW; i++) chs[i][COL - 1] = FILL_CHAR;
if(b[3])
for(int i = 0; i < COL; i++) chs[ROW - 1][i] = FILL_CHAR;
if(b[4])
for(int i = ROW / 2; i < ROW; i++) chs[i][0] = FILL_CHAR;
if(b[5])
for(int i = 0; i <= ROW /2; i++) chs[i][0] = FILL_CHAR;
if(b[6])
for(int i = 0; i < COL; i++) chs[ROW / 2][i] = FILL_CHAR;
return chs;
}

/**
* 译码器
*
*       0
*    #######
*    #     # 1
*  5 #  6  #
*    #######
*    #     #
*  4 #     # 2
*    #######
*       3
*
* 0 表示 leds[0]，若为 true 表示该 LED 显示，否则不显示
*
* @param num
* @return
*/
private boolean[] getLed(int num) {
boolean a = (num & 8) >>> 3 == 1;
boolean b = (num & 4) >>> 2 == 1;
boolean c = (num & 2) >>> 1 == 1;
boolean d = (num & 1) == 1;
boolean[] leds = new boolean[7];
leds[0] = (!a & !b & !c & d) | (b & !d);
leds[1] = (b & !c & d) | (b & c & !d);
leds[2] = !b & c & !d;
leds[3] = (b & !c & !d) | (!b & !c & d) | (b & c & d);
leds[4] = d | (b & !c);
leds[5] = (c & d) | (!b & c) | (!a & !b & d);
leds[6] = (!a & !b & !c) | (b & c & d);
for(int i = 0; i < 7; i++) {
leds[i] = !leds[i];
}
return leds;
}
}

2、请编写一段代码测试以下函数执行的正确性：

void unique(std::vector<int> & v);

void unique(vector<int> & v)
{
cout<<"去除重复前的数据"<<v.size()<<endl;
for(int i=0;i<v.size();i++)
cout<<v[i]<<" ";

vector<int>::iterator itr = v.begin();
for(int i=0;i<v.size();i++)
{
while (itr != v.end())
{
if (*itr == v[i])
v.erase(itr);
//v.erase(remove(v.begin(),v.end(),v[i]),v.end());
itr++;
}
}

cout<<"去除重复后的数据"<<v.size()<<endl;
for(int i=0;i<v.size();i++)
cout<<v[i]<<" ";
}

int main()
{
vector<int> v;
v.push_back(111);
v.push_back(111);
v.push_back(11);
v.push_back(111);
v.push_back(12);
unique(v);
}

当连续出现三个相同的时候会出错，求网友给出正解可参考http://wonderflow.info/archives/424

3、实现如下函数：

void printInChinese(int num);

17 -> 一十七

120 -> 一百二十

201 -> 二百零一

1074 -> 一千零七十四

65536 -> 六万五千五百三十六

1010101 -> 一百零一万零一百零一

17 -> 十七

120 -> 一百二

package javaJY;

/* * 金额转换，阿拉伯数字转换成中国传统形式。 * 例如：1010 0000 1010   转换为
壹仟零壹拾亿零壹仟零壹拾圆整
思路:在两个数组中分别存取和数字对应的传统形式和单位 *   然后从后面往前面打印.
*/
public class Num2Rmb2 {
public static void main(String[] args) {
System.out.println(printInChinese(1));
System.out.println(printInChinese(10));
System.out.println(printInChinese(100));
System.out.println(printInChinese(5005));
System.out.println(printInChinese(1357902468));
System.out.println(printInChinese(1010101));
}

public static String printInChinese(int number) {
char[] chineseNumber = new char[] { '零', '壹', '贰', '叁', '肆', '伍', '陆',
'柒', '捌', '玖' };
char[] units = new char[] { '元', '拾', '佰', '仟', '万', '拾', '佰', '仟',
'亿', '拾', '佰', '仟' };
StringBuilder sb = new StringBuilder();
long lastNumber = 0;
int index = 0;
while (number > 0) {
lastNumber = number % 10;
if (lastNumber != 0)// 如果不是零
{
sb.insert(0, units[index++]);// 先存进去单位
sb.insert(0, chineseNumber[(int) lastNumber]);// 再存进去数字
} else// 如果是零
{
if (sb.length() == 0)// 如果个位上是零,就只存进去单位,就是那个'元'
{
sb.insert(0, units[index++]);
} else { // 如果不是此位之后也都是零,则不存任何单位
if (sb.lastIndexOf("元") == 0) {
index++;
} else {
if (sb.charAt(0) != '零')// 如果下一位是零,则不存单位
{
sb.insert(0, chineseNumber[(int) lastNumber]);// 再存进去数字
index++;
} else {
index++;
}
}
}
}
number /= 10;
}
return sb.toString();
}
}

4、已知完全弹性碰撞公式如下：

struct ball_t {

double m; // 质量

double v; // 速度，速度为正表示球体往x轴正方向运动

double pos; // x坐标轴的位置

};

void progress(ball_t & b1, ball_t & b2, double leftWall, double rightWall, double t);

struct ball_t {
double m; // 质量
double v; // 速度，速度为正表示球体往x轴正方向运动
double pos; // 在x坐标轴的位置
};

void progress(struct ball_t b1,struct ball_t b2, double leftWall, double rightWall, double t)
{
double i;
b1.pos=leftWall+(int)(b1.v*t+b1.pos)%(int)(rightWall-leftWall);//取余存在一点问题
b2.pos=leftWall+(int)(b2.v*t+b2.pos)%(int)(rightWall-leftWall);
for(i=0;i<t;i+=0.1)//精确到千分位
{
if((b1.v*t+b1.pos>leftWall&&b1.v*t+b1.pos<rightWall)&&(b2.v*t+b2.pos>leftWall&&b2.v*t+b2.pos<rightWall))
{
if(b1.v*t+b1.pos-b2.v*t+b2.pos<=0.000001||b1.v*t+b1.pos-b2.v*t+b2.pos>=-0.000001)
{
b1.v=(b1.v*(b1.m-b2.m)+2*b2.m*b2.v)/(b1.m+b2.m);
b2.v=(b2.v*(b2.m-b1.m)+2*b1.m*b1.v)/(b1.m+b2.m);
}
}
else if(b1.v*t+b1.pos<=leftWall||b1.v*t+b1.pos>=rightWall)
{
b1.v=-b1.v;
}
else if(b2.v*t+b2.pos<=leftWall||b2.v*t+b2.pos>=rightWall)
{
b2.v=-b2.v;
}
}
printf("t %lf\nb1.m %lf b1.v %lf b1.pos %lf\nb2.m %lf b2.v %lf b2.pos %lf",t,b1.m,b1.v,b1.pos,b2.m,b2.v,b2.pos);
}

int main()
{
struct ball_t b1,b2;
double leftWall,rightWall,t;

printf("输入第一个球的状态\n");
scanf("%lf%lf%lf",&b1.m,&b1.v,&b1.pos);
printf("输入第二个球的状态\n");
scanf("%lf%lf%lf",&b2.m,&b2.v,&b2.pos);
printf("输入左右边界和时间状态\n");
scanf("%lf%lf%lf",&leftWall,&rightWall,&t);
progress(b1,b2,leftWall,rightWall,t);

return 0;
}

5、一个工程由如下文件组成：

gcc    main.o   src1.o   src2.o   -o     xxx

gcc    main.c      -c   mian.o

gcc -c src1.cpp                              等价于  gcc   src1.cpp   -c   src1.o

gcc -c src2.cpp                              等价于  gcc   src2.cpp   -c   src2.o

xxx: src1.o src2.o main.o
gcc  -o main.o xxx   -----
main.o:main.c              |不知道这两步是否正确
gcc -c main.c           -----
gcc -c src1.cpp
gcc -c src2.cpp

• 评论

7

• 下一篇
• 上一篇