1个简单算法的面试题,大家看看有没有什么比较简洁的方法。

BearRui 2009-08-04 01:01:27
用程序实现下面功能:有100个数字组成的序列,从序列第1个数字开始数,数到7的时候,
把对应的数字从序列中删除,当数到最后1个数字的时候,跳到第1个数字,如此一直循环,直到把所有数字删除。
...全文
484 30 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
30 条回复
切换为时间正序
请发表友善的回复…
发表回复
冰思雨 2009-08-05
  • 打赏
  • 举报
回复
简单写了一个环,然后,按题意,循环删除。
public class Cycle {
static class Entry{
int num;
Entry next;
}
private static Entry createCycle(int size){
Entry header = new Entry();
header.num=1;
Entry last = header;
for(int i=2;i<=size;i++){
Entry e = new Entry();
e.num=i;last.next=e;
last = e;
}
last.next=header;
return last;
}

public static void main(String[] args) {
final int SIZE = 100;
final int LEN = 7 ;
Entry e = createCycle(SIZE);
int lineNumbers = 0;
while(e.next!=e){
for(int i=LEN;i>1;i--){
e=e.next;
}
Entry ne = e.next;
System.out.printf("%4d",new Object[]{Integer.valueOf(ne.num)});
if(lineNumbers++%10==9)System.out.println();
e.next=ne.next;
}
System.out.printf("%4d",new Object[]{Integer.valueOf(e.num)});
}

}
feishare 2009-08-05
  • 打赏
  • 举报
回复

package cn.fee.arithmetic;

public class Quit7Test {

public static void main(String[] args) {
boolean[] kid = new boolean[100];

for(int i = 0; i < kid.length; i++) {
kid[i] = true;
}
int countNum = 100;
int number = 0;
int index = 0;

while(countNum > 1) {
if(kid[number] == true ) {
index++;
if(index == 7) {
kid[number] = false;
countNum--;
index = 0;
}
}
number++;

if(number == 100) {
number = 0;
}
}

for(int i = 0; i < kid.length; i++) {
if(kid[i] == true) {
System.out.println(i);
}
}
}
}

wym2005em 2009-08-04
  • 打赏
  • 举报
回复
这个名字叫什么圈圈?很经典的?忘记了呀!
用C实现过
wwfsfe 2009-08-04
  • 打赏
  • 举报
回复
我只会用C语言写
KAKUKYOWU 2009-08-04
  • 打赏
  • 举报
回复
[Quote=引用楼主 bearrui 的回复:]
用程序实现下面功能:有100个数字组成的序列,从序列第1个数字开始数,数到7的时候,
把对应的数字从序列中删除,当数到最后1个数字的时候,跳到第1个数字,如此一直循环,直到把所有数字删除。
[/Quote]

可以把所有的数字删除吗?前六个数字不可能删除吧?
sebatinsky 2009-08-04
  • 打赏
  • 举报
回复
[Quote=引用 13 楼 lgg201 的回复:]
Java codepackage selfimpr.test;/**
*
*@author selfimpr
* @mail lgg860911@yahoo.com.cn
* @bloghttp://blog.csdn.net/lgg201*/publicclass Algorithm {/**
*@param args
* 有100个数字组成的序列,
* 从序列第1个数字开始数,数到7的时候
* 把对应的数字从序列中删除,
* 当数到最后1个数字的时候,
* 跳到第1个数字,如此一直循环,直到把所有数字删除。*/publicstaticvoid main(String[] args) {
Sequence seq= init();int i=0;while(seq.first!=null) {
seq.getNext();
i++;
System.out.print(seq.current.data+"-");if(i%7==0) {
seq.remove();
}
}
}privatestatic Sequence init() {
Sequence seq=new Sequence(1);for(int i=2; i<=100; i++) {
seq.add(i);
}return seq;
}

}/**
* 模拟题目要求的数据结构序列
*@author selfimpr
* @mail lgg860911@yahoo.com.cn
* @bloghttp://blog.csdn.net/lgg201*/class Sequence {
Node first;
Node previou;
Node current;int i=0;
Sequence(long data) {
first=new Node(data);
current= first;
}/*
* 添加一个数据为data的节点在Sequence末尾*/void add(long data) {
current.next=new Node(data);
current= current.next;
previou= current;
}/*
* 移除当前节点current*/void remove() {
System.out.println(current.data+" is removed, all removed count is:"+++i);if(previou==null&& current== first) {
first= current= current.next;return ;
}
previou.next= current.next;
current= previou;
}/*
* 获取序列的下一个节点, 并移动序列的current和previou指针*/
Node getNext() {if(current.next==null) {
previou=null;
current= first;
}else {
previou= current;
current= current.next;
}return current;
}
}/**
* 序列的节点
*@author selfimpr
* @mail lgg860911@yahoo.com.cn
* @bloghttp://blog.csdn.net/lgg201*/class Node {
Node next;long data;
Node(long data) {this.data= data;
}
}
[/Quote]
只能说技术啊,哈哈,
sebatinsky 2009-08-04
  • 打赏
  • 举报
回复
呵呵,当前,出了我用循环,用其他的还不会,哎,以前学的链表和树都忘记了,不然就应该快
heaiso1985 2009-08-04
  • 打赏
  • 举报
回复
以下列出了一些使用的Java代码规范的原则,这些原则来源于java.sun.com,是Sun公司制定的Java编程语言编码规范。(注意:这个网站上有很多Sun推荐的规范,大家有时间一定要去看一看。)

1、 所有文件的开头都要有Java文档的注释。

2、文件里的代码的首行是所在包的说明,接下来是引入类的说明。

3、包名以一个小写的顶层域名开始(例如,com.或edu.)。

4、类和接口名应该是名词,而且组成它的每个单词的首字母必须大写(例如,EmployeeHours)。

5、所有的类文件应该包含以下部分:

6、该类的Java文档;

7、 变量列表:静态变量、实例变量(public、protected、no access specified和private);

8、方法列表:构造体和方法(方法应该按照功能分组)。

9、方法名应该是动词,第一个单词的首字母小写,接下来的单词的首字母大写(例如:getHoursWorked)。

10、 变量应该以字母打头,只有一个字母的变量(例如,i、j、k)应该避免出现,除非是一个暂时变量(例如,在一个for语句中)。

11、尽量使类中所有的变量都是私有的,只能通过方法来访问它们。

12、常量应该全部大写,单词之间由下划线分隔(例如,MAX_WORK_HOURS)。

13、用常量来定义具体数值(例如,int MAX_WORK_HOURS=24)。

14、内部变量声明时应该对它进行初始化。

15、避免一行中超过80个字符。

16、一行应该包含一个语句。

17、在使用if-else、for、while、do和switch语句时应该使用大括号。
tianxueer 2009-08-04
  • 打赏
  • 举报
回复
[Quote=引用 4 楼 bearrui 的回复:]
引用 2 楼 knightzhuwei 的回复:
这个什么都没返回 什么都没显示 最后所有数字都删了 什么都没看出来 有什么意义?


每个人的看法不一样,这是考验算法意识的题,难道面试一定要做1个有用的项目出来,才算是有意义???
[/Quote]

是你没把问题说清,按照你说的,最后不就是全部删除了吗?还用写什么程序???
BearRui 2009-08-04
  • 打赏
  • 举报
回复
[Quote=引用 16 楼 bigbug9002 的回复:]
Java codeimport java.util.*;publicclass Test1{publicstaticvoid main(String[] args){//josephus(8,3); josephus(100,7);

}publicstaticvoid josephus(int num,int k){
LinkedList<Integer> linkedList=new LinkedList<Integer>();for(int i=0;i<num;i++){
linkedList.add(i+1);
}int index=0;int count=0;//return new line for each 10 result.while(linkedList.size()>0){if(count%10==0) System.out.println();
index=(index+k-1)%linkedList.size();
System.out.printf("%4d",linkedList.get(index));
linkedList.remove(index);
count++;
}
System.out.println();
}
}

F:\java>java Test1

  7  14  21  28  35  42  49  56  63  70
  77  84  91  98  5  13  22  30  38  46
  54  62  71  79  87  95  3  12  23  32
  41  51  60  69  80  89  99  9  19  31
  43  53  65  75  86  97  10  24  36  48
  61  74  88  1  16  29  45  59  76  92
  6  25  40  58  78  94  15  34  55  73
  96  18  44  67  90  17  47  72  2  33
  66 100  37  81  11  57  4  52  8  68
  27  93  83  82  85  26  64  20  39  50
[/Quote]

非常漂亮,赞1个。
bigbug9002 2009-08-04
  • 打赏
  • 举报
回复
删除元素不是为了节省空间,是为了一下子跳到下一个要输出的元素。
Inhibitory 2009-08-04
  • 打赏
  • 举报
回复
用数组实现循环链表. 每次直接过滤掉不需要的, 不用删除移动元素,反正也不在乎这点空间, 用空间换取时间.
ITwinter 2009-08-04
  • 打赏
  • 举报
回复
新手 米看懂

sdluhaibing 2009-08-04
  • 打赏
  • 举报
回复
约瑟夫问题,用求余方法,算法比较简单
bigbug9002 2009-08-04
  • 打赏
  • 举报
回复
import java.util.*;
public class Test1{
public static void main(String[] args){
//josephus(8,3);
josephus(100,7);

}
public static void josephus(int num,int k){
LinkedList<Integer> linkedList=new LinkedList<Integer>();
for(int i=0;i<num;i++){
linkedList.add(i+1);
}
int index=0;
int count=0; //return new line for each 10 result.
while(linkedList.size()>0){
if(count%10==0) System.out.println();
index=(index+k-1)%linkedList.size();
System.out.printf("%4d",linkedList.get(index));
linkedList.remove(index);
count++;
}
System.out.println();
}
}


F:\java>java Test1

7 14 21 28 35 42 49 56 63 70
77 84 91 98 5 13 22 30 38 46
54 62 71 79 87 95 3 12 23 32
41 51 60 69 80 89 99 9 19 31
43 53 65 75 86 97 10 24 36 48
61 74 88 1 16 29 45 59 76 92
6 25 40 58 78 94 15 34 55 73
96 18 44 67 90 17 47 72 2 33
66 100 37 81 11 57 4 52 8 68
27 93 83 82 85 26 64 20 39 50
bigbug9002 2009-08-04
  • 打赏
  • 举报
回复
[Quote=引用 14 楼 liboofsc 的回复:]
要用也用LinkedList吧。
如果用ArrayList要删中间的元素,后面的元素要往前面移,用数组显然不合适的。
[/Quote]
同意.
ArrayList定位方便,LinkedList删除方便,各有千秋吧.总的来说LinkedList效率高一些.
liboofsc 2009-08-04
  • 打赏
  • 举报
回复
要用也用LinkedList吧。
如果用ArrayList要删中间的元素,后面的元素要往前面移,用数组显然不合适的。
goosman 2009-08-04
  • 打赏
  • 举报
回复
package selfimpr.test;

/**
*
* @author selfimpr
* @mail lgg860911@yahoo.com.cn
* @blog http://blog.csdn.net/lgg201
*/
public class Algorithm {

/**
* @param args
* 有100个数字组成的序列,
* 从序列第1个数字开始数,数到7的时候
* 把对应的数字从序列中删除,
* 当数到最后1个数字的时候,
* 跳到第1个数字,如此一直循环,直到把所有数字删除。
*/
public static void main(String[] args) {
Sequence seq = init();

int i = 0;
while(seq.first != null) {
seq.getNext();
i++;
System.out.print(seq.current.data + "-");
if(i%7==0) {
seq.remove();
}
}
}

private static Sequence init() {
Sequence seq = new Sequence(1);
for(int i = 2; i<=100; i++) {
seq.add(i);
}
return seq;
}

}

/**
* 模拟题目要求的数据结构序列
* @author selfimpr
* @mail lgg860911@yahoo.com.cn
* @blog http://blog.csdn.net/lgg201
*/
class Sequence {
Node first;
Node previou;
Node current;
int i = 0;
Sequence(long data) {
first = new Node(data);
current = first;
}
/*
* 添加一个数据为data的节点在Sequence末尾
*/
void add(long data) {
current.next = new Node(data);
current = current.next;
previou = current;
}
/*
* 移除当前节点current
*/
void remove() {
System.out.println(current.data + " is removed, all removed count is: " + ++i);
if(previou == null && current == first) {
first = current = current.next;
return ;
}
previou.next = current.next;
current = previou;
}
/*
* 获取序列的下一个节点, 并移动序列的current和previou指针
*/
Node getNext() {
if(current.next == null) {
previou = null;
current = first;
} else {
previou = current;
current = current.next;
}
return current;
}
}
/**
* 序列的节点
* @author selfimpr
* @mail lgg860911@yahoo.com.cn
* @blog http://blog.csdn.net/lgg201
*/
class Node {
Node next;
long data;
Node(long data) {
this.data = data;
}
}
BearRui 2009-08-04
  • 打赏
  • 举报
回复
[Quote=引用 10 楼 bigbug9002 的回复:]
嗯,只求最后剩下的那个,算法是可以优化的

但楼主好像要的是每一次出列的编号,是吧?
[/Quote]

对,需要知道每次删除的数字。
bigbug9002 2009-08-04
  • 打赏
  • 举报
回复
8楼的博客不错啊。
加载更多回复(10)

62,634

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧