笔试题
1有两个有序数级R1[5],R2[5],将这两个数组合并成一个有序数组,用程序表达实现的思路
2 1至100一百个自然数(无序),放入一个99个元素的数组a[99],用java语言编写一段程序,找出没有被放入数组这个数。
2.8,64,256都是2的阶次方数(例8是2的3次方),用java编写程序,用两种方法来判断一个整数是不是2的阶次方数,并说明哪个方法更好。
3.有一整型数组有n个数,用java实现第一个元素与第n个元素互换,第二个元素与第n-1个元素互换…。
4.利用jdk中的基本类vector,用java语言编写类Stack和Queue,分别实现堆栈和队列数据结构。
Stack类至少要实现以下基本操作:进栈(push),出栈(pop),清空堆栈(clear)
Queue类至少要实现以下基本操作:进队列(enqueue),出队列(dequeue),清空 队列
5.1至100一百个随机自然数,放入数组a中。用java实现
当中重复次数最多,而且是数是最大的一个,打印出来!
[解决办法]
1、支持任意长度的int型R1,R2数组。其他题目等续。。
int[] R1 = {2,4,5,7,8,12,14,16,18};
int[] R2 = {1,3,6,9,10,11,15,19,20,22};
int[] R3 = new int[R1.length+R2.length];
for(int i=0,j=0; i <R1.length || j <R2.length;){
if(i==R1.length){
R3[i+j] = R2[j++];
continue;
}
if(j==R2.length){
R3[i+j] = R1[i++];
continue;
}
if(R1[i] <R2[j]){
R3[i+j] = R1[i++];
}else{
R3[i+j] = R2[j++];
}
}
for(int i:R3){
System.out.print(i+ " ");
}
System.out.println();
[解决办法]
第二题:
array[] = {1,2,3,4....};
int sum = 0;
for(int x :array)
sum += x;
return 5050 - sum;
[解决办法]
Stack类至少要实现以下基本操作:进栈(push),出栈(pop),清空堆栈(clear)
public class TestStack{
private Vector vector = new Vector();
// 进栈(push)
public void inStack(Object o){
vector.add(o);
}
//出栈(pop)
public Object outStack(){
if(vector.isEmpty()){
return null;
}
return vector.get(vector.size()-1);
}
//清空堆栈(clear)
public void removeAll(){
this.vector = new Vector();
}
}
[解决办法]
第五题
public static void main(String[] args) {
Map <Integer,Integer> map = new TreeMap <Integer,Integer> () ;
Random rand = new Random();
for(int i=1;i <=100;i++) {
int x = rand.nextInt(100) ;
map.put(x, map.get(x)==null?1:map.get(x)+1);
}
int min=-1;
int max =0;
for(int j=100;j> =1;j--){
if(map.get(j)!=null&&map.get(j)> min){
min=map.get(j);
max=j;
}
}
System.out.println(max);
System.out.println(map);
}
}
[解决办法]
package ihavegotyou;
import java.util.Random;
import java.util.Vector;
public class Pow {
public static boolean is2Power(int n) {
boolean result = n > = 1 ? true : false;
if (result) {
if (n == 1)
return true;
int num = 1;
while ((num = num < < 1) < n)
;
result = num == n;
}
return result;
}
public static int[] exChange(int[] a) {
int len = a.length;
for (int i = 0; i < len / 2; i++) {
int temp = a[i];
a[i] = a[len - 1 - i];
a[len - 1 - i] = temp;
}
return a;
}
public static class Stack {
private Vector <String> vc = new Vector <String> ();
synchronized public void clear() {
vc.clear();
}
synchronized public void push(String str) {
// 栈添加元素的时候添在最前面
vc.add(0, str);
}
synchronized public String pop() {
// LIFO
if (vc.isEmpty())
return null;
String tmp = vc.firstElement();
vc.remove(0);
return tmp;
}
}
public static class Queue {
private Vector <String> vc = new Vector <String> ();
synchronized public void clear() {
vc.clear();
}
synchronized public void enQueue(String str) {
// 象排队一样,排最后
vc.add(vc.size(), str);
}
synchronized public String deQueue() {
// FIFO
if (vc.isEmpty())
return null;
String tmp = vc.firstElement();
vc.remove(0);
return tmp;
}
}
public static void hashCount() {
int[] a = new int[100];
Random rd = new Random();
for (int i = 0; i < a.length; i++)
a[i] = 1 + rd.nextInt(100);
int max = 101;
int hashTable[] = new int[max + 1];
for (int i = 0; i < a.length; i++)
hashTable[a[i] % (max + 1)] = hashTable[a[i] % (max + 1)] + 1;
int countMax = hashTable[0];
for (int i = 0; i < hashTable.length; i++)
countMax = countMax < hashTable[i] ? hashTable[i] : countMax;
for (int i = 0; i < hashTable.length; i++) {
if (hashTable[i] == countMax) {
for (int j = 0; j < countMax; j++)
System.out.print(i + (j != countMax - 1 ? ", " : " "));
System.out.println();
}
}
}
public static void main(String[] args) {
System.out.println(is2Power(128));
int a[] = { 1, 2, 3, 4, 5 };
int b[] = exChange(a);
for (int i : b)
System.out.println(i);
hashCount();
Stack st = new Stack();
st.push( "str1 ");
st.push( "str2 ");
System.out.println(st.pop());
System.out.println(st.pop());
Queue qu = new Queue();
qu.enQueue( "str1 ");
qu.enQueue( "str2 ");
System.out.println(qu.deQueue());
System.out.println(qu.deQueue());
}
}
[解决办法]
1有两个有序数级R1[5],R2[5],将这两个数组合并成一个有序数组,用程序表达实现的思路
用TreeMap之类的集合类,是Collection的实现,而且可以实现有序存储。
2 1至100一百个自然数(无序),放入一个99个元素的数组a[99],用java语言编写一段程序,找出没有被放入数组这个数。
省事的方法大家都说了
2.8,64,256都是2的阶次方数(例8是2的3次方),用java编写程序,用两种方法来判断一个整数是不是2的阶次方数,并说明哪个方法更好。
二进制形如10,1000,1000000的都是,怎么判断还不容易吗?
3.有一整型数组有n个数,用java实现第一个元素与第n个元素互换,第二个元素与第n-1个元素互换…。
确实是reverse一下就行了
4.利用jdk中的基本类vector,用java语言编写类Stack和Queue,分别实现堆栈和队列数据结构。
Stack类至少要实现以下基本操作:进栈(push),出栈(pop),清空堆栈(clear)
Queue类至少要实现以下基本操作:进队列(enqueue),出队列(dequeue),清空 队列
自己去看api里两个类的源代码
5.1至100一百个随机自然数,放入数组a中。用java实现
当中重复次数最多,而且是数是最大的一个,打印出来!
没想出更好的方法,用HashTable了。
[解决办法]
5.1至100一百个随机自然数,放入数组a中。用java实现
当中重复次数最多,而且是数是最大的一个,打印出来!
没想出更好的方法,用HashTable了。
-----------------
可以求下平均值~~~应该可以提高效率
[解决办法]
第一题:
package test1;
import java.util.Arrays;
public class Test3 {
/**
* @param args
*/
public static void main(String[] args) {
// TODO 自动生成方法存根
int[] m1={1,2,3,4,5};
int[] m2={6,7,8,9,10};
int count=m1.length+m2.length;
int[] m3=new int[count];
for(int i=0;i <m1.length;i++){
m3[i]=m1[i];
}
for(int j=0;j <m2.length;j++){
m3[m1.length+j]=m2[j];
}
Arrays.sort(m3);
for(int k=0;k <m3.length;k++){
System.out.print(m3[k]);
}
}
}
[解决办法]
第一题 法二:
int[] m1={1,2,3,4,5};
int[] m2={6,7,8,9,10};
int count=m1.length+m2.length;
int[] m3=new int[count];
System.arraycopy(m1, 0, m3, 0, m1.length);
System.arraycopy(m2, 0, m3, m1.length, m2.length);
Arrays.sort(m3);
for(int i:m3)
System.out.print(i);
[解决办法]
最后一题:
a[]={1,1,……100}
max=0;
maxCishu=0;
for(int i=100;i> 0;i--){
for(i in a){
//计算i出现的次数
inttemp=0;
if(i=a[i]){
temp++;
}
if(temp> num&&i> max){
num=temp;
max=i;
}
}
}
[解决办法]
修正一下:
最后一题:
a[]={1,1,……100}
maxValue=0;
maxTime=0;
for(int i=100;i> 0;i--){
for(i in a){
//计算i出现的次数
inttemp=0;
if(i=a[i]){
temp++;
}
if(temp> maxTime&&i> maxValue){
num=temp;
max=i;
}
}
}
print( "maxValue is: "+maxValue + " which have "+maxTime);
[解决办法]
/**
* 判断一个是不是2的n次方,如果是,返回true,否则返回false
*/
public boolean is2times(int _num){
boolean is2times = false;
if(_num ==1){
is2times = true;
}else if(_num%2 ==0){
is2times = is2times(_num/2);
}
return is2times;
}