机试题目,没写出来,巨汗!请大侠帮忙 比如说有10个数字(0--9),第一次去掉所有奇数位,就剩下(1,3,5,7,9),然后去掉奇数位,剩下(3,7),然后剩下奇数位,剩下7.就是这个意思.用java实现. 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 这有什么难的? 你用一个计数器来做不就行了?或者费点性能, 用ARRAYLIST什么的.先ADD,然后REMOVE注意每次REMOVE后长度会减1.如果用数组的话要记得移位!或都把删除的数值都设为一个特殊值以示不参加计数. 我写的代码public class Test { public static void main(String [] args) { int [] array = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 }; Test test = new Test(); test.process(array); } public void process(int [] array) { int [] array2 = new int[array.length / 2]; System.out.println("array2.length: " + array2.length); int j = 0; for (int i = 0; i < array.length; i++) { if ((i % 2) == 1) { System.out.println("array[" + i + "]: " + array[i]); array2[j] = array[i]; System.out.println("array2[" + j + "]: " + array2[j]); j++; } } if (array2.length > 1) { process(array2); } }} 用vector或者arraylist来做不难的,都是可变数组而且可以用下标来访问 部分代码: ArrayList list = new ArrayList(); for (int i = 0; i < 10; i++) { list.add(i); } System.out.println("输入:" + list); do { for (int i = 0, size = list.size(); i < size; i = i + 2) { if (size == 1) { System.out.println("最终输出:" + list.get(0)); return; } list.set(i, null); } for (int i = 0; i < list.size(); i++) { if (list.get(i) == null) { list.remove(i); i--; } } } while (list.size() >= 1);输入:[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]最终输出:7 int i,rlt;for(i=0,rlt=1;rlt<=MAXSIZE;i++) rlt*=rlt;System.out.println("最后剩下的数为:"+array[i-1]); for(rlt=1;rlt<=array.length;)rlt*=2;rlt/=2;System.out.println(array[rlt-1]); String format问题 求java记事本源码 非常急 谁快给谁分! 我使用了TableCellRenderer 但是在table中无法选中行了,怎么解决?高手来 推荐一本关于数据结构java语言描述的好书。 大家帮忙看下代码哪里有问题 求Xml格式文档结合模板进行web显示和打印的方案 JM1.3.2发布,扩展Swing,数据库整合 我相信JAVA的界面会越来越漂亮! 求助线程问题? implements 是用来做什么的? JAVA中如何实现状态栏 java调用command命令,输出outputStream写入文件的一个问题
或者费点性能, 用ARRAYLIST什么的.
先ADD,然后REMOVE
注意每次REMOVE后长度会减1.如果用数组的话要记得移位!
或都把删除的数值都设为一个特殊值以示不参加计数.
public static void main(String [] args) {
int [] array = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
Test test = new Test(); test.process(array);
} public void process(int [] array) {
int [] array2 = new int[array.length / 2]; System.out.println("array2.length: " + array2.length); int j = 0; for (int i = 0; i < array.length; i++) {
if ((i % 2) == 1) {
System.out.println("array[" + i + "]: " + array[i]);
array2[j] = array[i];
System.out.println("array2[" + j + "]: " + array2[j]);
j++;
}
} if (array2.length > 1) {
process(array2);
}
}
}
ArrayList list = new ArrayList();
for (int i = 0; i < 10; i++) {
list.add(i);
}
System.out.println("输入:" + list);
do {
for (int i = 0, size = list.size(); i < size; i = i + 2) {
if (size == 1) {
System.out.println("最终输出:" + list.get(0));
return;
}
list.set(i, null);
}
for (int i = 0; i < list.size(); i++) {
if (list.get(i) == null) {
list.remove(i);
i--;
}
}
} while (list.size() >= 1);输入:[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
最终输出:7
for(i=0,rlt=1;rlt<=MAXSIZE;i++)
rlt*=rlt;
System.out.println("最后剩下的数为:"+array[i-1]);
rlt/=2;
System.out.println(array[rlt-1]);