f****e 发帖数: 923 | 1 class ListNode{
int val;
ListNode next;
ListNode(int val){
this.val = val;
}
}
class Solution{
public int live(int n, int k){
ListNode head = new ListNode(1);
ListNode node = head;
for(int i = 2; i <= n; i++){
node.next = new ListNode(i);
node = node.next;
}
node.next = head;
System.out.println("before remove");
printList(head);
System.out.println("removing the nodes");
while(head.next != head){
int count = 1;
while(count < k - 1){
head = head.next;
count++;
}
System.out.print(head.next.val + "->");
head.next = head.next.next;
head = head.next;
}
System.out.println();
System.out.println("after remove");
return head.val;
}
public void printList(ListNode head){
if (head != null){
ListNode temp = head;
do{
System.out.print(temp.val + " ");
temp = temp.next;
} while (temp != head);
}
System.out.println();
}
public static void main(String[] args){
Solution s = new Solution();
int n = 5;
int k = 2;
System.out.println(s.live(n, k));
int n1 = 7;
int k1 = 3;
System.out.println(s.live(n1, k1));
int n2 = 14;
int k2 = 2;
System.out.println(s.live(n2, k2));
}
} |
r*****s 发帖数: 1815 | |
y**********u 发帖数: 2839 | |
W***o 发帖数: 6519 | 4 红狗哥有没有poj的全套答案啊?
这题有意思
【在 y**********u 的大作中提到】 : http://poj.org/problem?id=1012 : 加油
|
y**********u 发帖数: 2839 | 5 都是靠google……,等我到了1000就把我的git repo share给各位基友
【在 W***o 的大作中提到】 : 红狗哥有没有poj的全套答案啊? : 这题有意思
|
z*********n 发帖数: 1451 | |
o*******r 发帖数: 73 | 7 记得我以前回复过。
public class Solution {
public int lastStand(int n, int m) {
int ret = 0;
for (int i = 2; i <= n; i++) {
ret = (ret + m) % i;
}
return (ret + 1);
}
} |