uva 133 - The Dole Queue|java实现

题意:所有n个人围成一个圈,一边从1号开始逆时针数k个,出局;一边从n号开始顺时针数m个,出局。两个同时发生。如果这两个数到同一个人,就这个人出局。


分析:其实就是一个双向链表,从两边同时开始数,注意是同时,说是同时,但是程序里面必然有处理的先后。我们可以先处理正着数的,然后处理倒着数的。但是这样处理有一个情况就是,当要处理的两个人相邻,这时,要同时处理,也就是同时去除这两个人(否则先处理其中一个,那么这个的指针会指到本该同时被处理的人身上)


public class Person {
	int num;
	Person pre=null;
	Person next = null;
	
	public Person(int num) {
		this.num = num;
	}
}

public class Circle {
	Person head;
	int size =0;
	public Circle() {
		this.head = new Person(0);		
	}
		
	Person getFirst(){
		return head.next;		
	}
	
	Person getLast(){
		return head.next.pre;
	}
	
	void add(Person person){
		if(size==0){
			head.next = person;
			person.pre = person;
			person.next = person;
		}else{
			head.next.pre.next = person;
			person.pre = head.next.pre;
			person.next = head.next;
			head.next.pre = person;						
		}
		size++;
	}
	
	void delete(Person person){
		
	}
}


import java.io.IOException;
import java.util.LinkedList;
import java.util.Scanner;

public class Test {
	
	public static void main(String[] args) throws IOException {		
		int n = 7;//总人数
		int k = 2;//正着数
		int m =3;//倒着数	
		Circle circle = new Circle();
		//装载数据
		for(int i=0;i<n;i++){
			circle.add(new Person(i+1));
		}
		
		
		int step1=0;
		int step2=0;
		Person first = circle.getFirst();
		Person last = circle.getLast();	
		int count=0;
		//开始两端同时数数		
		while(circle.size!=0){//剩下一个人才结束			
			step1++;
			step2++;				
			if(step1 == k && step2 == m &&first.num == last.num){//如果数到同一个
				step1=1;
				step2=1;
				first.next.pre = last.pre;
				last.pre.next = first.next;
				circle.size--;
				System.out.println(first.num+"同时出局");
				first = first.next;
				last = last.pre;
			}
			
			if(step1 == k && step2 == m && first.next.num == last.num){//同时删除,而且这个两个人相邻
				step1=1;
				step2=1;
				first.pre.next = last.next;
				last.next.pre = first.pre;										
				circle.size = circle.size-2;
				System.out.println(first.num+"整除"+k+"出局,同时"+last.num+"整除"+m+"出局");
				first = first.next.next;
				last = last.pre.pre;
			}else{				
				if(step1 == k){
					step1=1;
					first.next.pre = first.pre;
					first.pre.next = first.next;
					circle.size--;				
					System.out.println(first.num+"整除"+k+"出局");
					first = first.next;
				}
				
				if(step2 == m){
					step2=1;
					last.next.pre = first.pre;
					last.pre.next = first.next;
					circle.size--;				
					System.out.println(last.num+"整除"+m+"出局");
					last = last.pre;
				}	
			}
			first = first.next;
			last = last.pre;
		}				
	}
}


用C++编写程序,实现以下问题2、题目ID Codes(POJ1146) Time Limit: 1000MS Memory Limit: 10000K 描述: It is 2084 and the year of Big Brother has finally arrived, albeit a century late. In order to exercise greater control over its citizens and thereby to counter a chronic breakdown in law and order, the Government decides on a radical measure--all citizens are to have a tiny microcomputer surgically implanted in their left wrists. This computer will contains all sorts of personal information as well as a transmitter which will allow people's movements to be logged and monitored by a central computer. (A desirable side effect of this process is that it will shorten the dole queue for plastic surgeons.) An essential component of each computer will be a unique identification code, consisting of up to 50 characters drawn from the 26 lower case letters. The set of characters for any given code is chosen somewhat haphazardly. The complicated way in which the code is imprinted into the chip makes it much easier for the manufacturer to produce codes which are rearrangements of other codes than to produce new codes with a different selection of letters. Thus, once a set of letters has been chosen all possible codes derivable from it are used before changing the set. For example, suppose it is decided that a code will contain exactly 3 occurrences of a', 2 of b' and 1 of c', then three of the allowable 60 codes under these conditions are: abaabc abaacb ababac These three codes are listed from top to bottom in alphabetic order. Among all codes generated with this set of characters, these codes appear consecutively in this order. Write a program to assist in the issuing of these identification codes. Your program will accept a sequence of no more than 50 lower case letters (which may contain repeated characters) and print the successor code if one exists or the message No Successor' if the given code is the last in the sequence for that set of characters. 输入: Input will consist of a series of lines each containing a string representing a code. The entire file will be terminated by a line consisting of a single #. 输出: Output will consist of one line for each code read containing the successor code or the words 'No Successor'. 样例输入 abaacb cbbaa # 样例输出 ababac No Successor
最新发布
05-22
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值