中级农民
- 积分
- 251
- 大米
- 颗
- 鳄梨
- 个
- 水井
- 尺
- 蓝莓
- 颗
- 萝卜
- 根
- 小米
- 粒
- 学分
- 个
- 注册时间
- 2015-4-23
- 最后登录
- 1970-1-1
|
1: answer 不写了,有答案,做错了好几个;
2:SortedComparableList.java
import java.util.Formatter;
/**
* SortedComparableList.java
* A list of Comparables in ascending order.
*/
public class SortedComparableList {
/** First element of list. */
public Comparable head;
/** Remaining elements of list. */
public SortedComparableList tail;
/** A list with head HEAD0 and tail TAIL0. */
public SortedComparableList(Comparable head0, SortedComparableList tail0) {
// REPLACE THIS LINE WITH YOUR SOLUTION
this.head=head0;
this.tail=tail0;
}
/** A list with null tail, and head = 0. */
public SortedComparableList(){
// REPLACE THIS LINE WITH YOUR SOLUTION
this.head=null;
this.tail=null;
}
/** Inserts Comparable c into its correct location in this list. */
public void insert(Comparable c) {
// REPLACE THIS LINE WITH YOUR SOLUTION
if(c==null) return;
SortedComparableList temp=this;
SortedComparableList pre=null;
if(temp.head==null) System.out.println(">>>>>>>>>>");
while(c.compareTo(temp.head)>=0){
pre=temp;
temp=temp.tail;
if(temp==null){//do not forget the boarder
System.out.println(">>>>>>>>>>");
break;
}
}
if(pre!=null){
SortedComparableList node=new SortedComparableList(c,pre.tail);
pre.tail=node;
}else{
SortedComparableList node=new SortedComparableList(this.head,this.tail);
this.tail=node;
this.head=c;
//How to insert into the front??
}//insert into the front
}
/** Returns the i-th int in this list.
* The first element, which is in location 0, is the 0th element.
* Assume i takes on the values [0, length of list - 1]. */
public Comparable get(int i) {
int count=0;
SortedComparableList temp=this;
for(;count<i;count++){
temp=temp.tail;
}
return temp.head; // REPLACE THIS LINE WITH YOUR SOLUTION
}
/** Adds every item in THAT to this list. */
//then we should distinguish between a node and a list
public void extend(SortedComparableList that) {
// REPLACE THIS LINE WITH YOUR SOLUTION
while(that!=null){
this.insert(that.head);
that=that.tail;
}
}
/** Returns a list consisting of the elements of L starting from
* position START, and going all the way to the END. The head of the
* list L is the 0th element of the list.
*
* This method should NOT modify L. */
public static SortedComparableList subTail(SortedComparableList L, int start) {
SortedComparableList temp,pre,result;
temp=L;
int count=0;
while(count<start){
temp=temp.tail;
count+=1;
}
pre=result=new SortedComparableList(temp.head,null);
temp=temp.tail;
while(temp!=null){
SortedComparableList inTemp=new SortedComparableList(temp.head,null);
pre.tail=inTemp;
pre=inTemp;
temp=temp.tail;
}
return result;
// REPLACE THIS LINE WITH YOUR SOLUTION
}
/** Returns the sublist consisting of LEN items from list L,
* beginning with item START (where the first item is 0).
*
* Does not modify the original list elements.
* Assume START and END are >= 0.
*/
public static SortedComparableList sublist(SortedComparableList L, int start, int len) {
int count=0;
SortedComparableList temp=L;
while(count < start){
temp=temp.tail;
count++;
}
count=0;
SortedComparableList result=new SortedComparableList(temp.head,null);
SortedComparableList pre=result;
temp=temp.tail;
while(count<len&&temp!=null){
SortedComparableList inTemp= new SortedComparableList(temp.head,null);
pre.tail=inTemp;
pre=inTemp;
temp=temp.tail;
count++;//always forget this
}
return result; // REPLACE THIS LINE WITH YOUR SOLUTION
}
/** Removes items from L at position len+1 and later. */
public static void expungeTail(SortedComparableList L, int len) {
// REPLACE THIS LINE WITH YOUR SOLUTION
SortedComparableList temp=L;
int count=0;
while(count<len){
L=L.tail;
count++;
}
L.tail=null;
L=temp;
}
/**
* Takes this list and, wherever two or more consecutive items are
* equal, it removes duplicates so that only one consecutive copy
* remains. No two consecutive items in this list are equals at the
* end of this method.
*
* You can assume the list is in sorted order when this method is
* called.
*
* For example, if the input list is [ 0 0 0 0 1 1 3 3 3 4 ], the
* output list is [ 0 1 3 4 ].
**/
public void squish() {
SortedComparableList pre,cur;
pre=this;
cur=this.tail;
while(cur!=null){
if(pre.head.compareTo(cur.head)==0){
cur=cur.tail;
pre.tail=cur;//do not forget this
}else{
pre=pre.tail;
cur=cur.tail;
}
}
// REPLACE THIS LINE WITH YOUR SOLUTION
}
/** Duplicates each Comparable so that for every original
* Comparable, there will end up being two consecutive Comparables.
*
* You can assume the list is in sorted order when this method is
* called.
*
* For example, if the input list is [ 2 3 4 7 ], the
* output list is [ 2 2 3 3 4 4 7 7 ].
*
* NOTE: Do not try to make copies of the Comparables. Set
* the HEAD variable equal to the HEAD variable you are trying to
* duplicate.
**/
public void twin() {
// REPLACE THIS LINE WITH YOUR SOLUTION
SortedComparableList temp=this;
SortedComparableList create;
while(temp!=null){//differentiate between temp!=null and temp.tail!=null
create=new SortedComparableList(temp.head,temp.tail);
temp.tail=create;
temp=create.tail;
}
}
/** Returns NULL if no cycle exists, else returns cycle location. */
private int detectCycles(SortedComparableList A) {
SortedComparableList tortoise = A;
SortedComparableList hare = A;
if (A == null) {
return 0;
}
int cnt = 0;
while (true) {
cnt++;
if (hare.tail != null) {
hare = hare.tail.tail;
}
else{
return 0;
}
tortoise = tortoise.tail;
if (tortoise == null || hare == null) {
return 0;
}
if (hare == tortoise) {
return cnt;
}
}
}
/** Returns true iff X is a SortedComparableList containing the
* same sequence of Comparables as THIS. Cannot handle cycles. */
public boolean equals(Object x) {
if (!(x instanceof SortedComparableList)) {
return false;
}
SortedComparableList L = (SortedComparableList) x;
SortedComparableList p;
for (p = this; p != null && L != null; p = p.tail, L = L.tail) {
if (p.head != L.head) {
return false;
}
}
if (p != null || L != null) {
return false;
}
return true;
}
public String printInt(){
SortedComparableList temp=this;
String re="";
while(temp!=null){
re=re+((imComparable)(temp.head)).forCompare+" ";
temp=temp.tail;
}
return re;
}
@Override
public String toString() {
Formatter out = new Formatter();
String sep;
sep = "(";
int cycleLocation = detectCycles(this);
int cnt = 0;
for (SortedComparableList p = this; p != null; p = p.tail) {
out.format("%s%d", sep, p.head);
sep = ", ";
cnt++;
if ((cnt > cycleLocation) && (cycleLocation > 0)) {
out.format("... (cycle exists) ...");
break;
}
}
out.format(")");
return out.toString();
}
}
part3:
import java.util.Formatter;
/**
* ApplicableIntList.java
* A list that stores integers in ascending order.
*/
public class ApplicableIntList{
/** First element of list. */
public int head;
/** Remaining elements of list. */
public ApplicableIntList tail;
/** A list with head HEAD0 and tail TAIL0. */
public ApplicableIntList(int head0, ApplicableIntList tail0) {
// REPLACE THIS LINE WITH YOUR SOLUTION
this.head=head0;
this.tail=tail0;
}
/** A list with null tail, and head = 0. */
public ApplicableIntList() {
this.head=-1;
this.tail=null;
// REPLACE THIS LINE WITH YOUR SOLUTION
}
/** Inserts int i into its correct location, doesn't handle cycles. */
public void insert(int i) {
System.out.println("insert");
ApplicableIntList pre,cur;
cur=this;
pre=null;
if(cur.head>=i){
ApplicableIntList temp= new ApplicableIntList(this.head,this.tail);
this.tail=temp;
this.head=i;
return ;
}//insert into the first node
while(cur!=null){
if(cur.head<=i){
pre=cur;
cur=cur.tail;
}else break;//do not forget this branch
}
if(cur==null){//do not forget this case
ApplicableIntList temp= new ApplicableIntList(i,null);
pre.tail=temp;
}else{
ApplicableIntList temp= new ApplicableIntList(i,cur);
pre.tail=temp;
}
// REPLACE THIS LINE WITH YOUR SOLUTION
}
/** Returns the i-th int in this list.
* The first element, which is in location 0, is the 0th element.
* Assume i takes on the values [0, length of list - 1]. */
public int get(int i) {
// REPLACE THIS LINE WITH YOUR SOLUTION
int count=0;
ApplicableIntList temp=this;
while(count<i){
temp=temp.tail;
count++;
}
return temp.head;
}
/** Applies the function f to every item in this list. */
public void apply(IntUnaryFunction f) {
// REPLACE THIS LINE WITH YOUR SOLUTION
ApplicableIntList temp=this;
while(temp!=null){
temp.head=f.apply(temp.head);
temp=temp.tail;
}
}
/** Returns NULL if no cycle exists, else returns cycle location. */
private int detectCycles(ApplicableIntList A) {
ApplicableIntList tortoise = A;
ApplicableIntList hare = A;
if (A == null) {
return 0;
}
int cnt = 0;
while (true) {
cnt++;
if (hare.tail != null) {
hare = hare.tail.tail;
}
else{
return 0;
}
tortoise = tortoise.tail;
if (tortoise == null || hare == null) {
return 0;
}
if (hare == tortoise) {
return cnt;
}
}
}
/** Returns true iff X is a ApplicableIntList containing the
* same sequence of Comparables as THIS. Cannot handle cycles. */
public boolean equals(Object x) {
if (!(x instanceof ApplicableIntList)) {
return false;
}
ApplicableIntList L = (ApplicableIntList) x;
ApplicableIntList p;
for (p = this; p != null && L != null; p = p.tail, L = L.tail) {
if (p.head != L.head) {
return false;
}
}
if (p != null || L != null) {
return false;
}
return true;
}
@Override
public String toString() {
// System.out.println("----------------------");
Formatter out = new Formatter();
String sep;
sep = "(";
int cycleLocation = detectCycles(this);
int cnt = 0;
for (ApplicableIntList p = this; p != null; p = p.tail) {
out.format("%s%d", sep, p.head);
sep = ", ";
cnt++;
if ((cnt > cycleLocation) && (cycleLocation > 0)) {
out.format("... (cycle exists) ...");
break;
}
}
out.format(")");
return out.toString();
}
}
part4:
MassCompatator.java
import java.util.Comparator;
/**
* MassComparator.java
*/
public class MassComparator implements Comparator<Planet> {
public MassComparator() {
}
/** Returns the difference in mass as an int.
* Round after calculating the difference. */
public int compare(Planet planet1, Planet planet2) {
// REPLACE THIS LINE WITH YOUR SOLUTION
double result=planet1.getMass()-planet2.getMass();
System.out.println(planet1.getMass()+" : "+planet2.getMass()+" : "+result);
return (int)result;
}
RadiusCompatator 类似;
MaxPlanet.java
public class MaxPlanet {
public static Planet getPlanet(In in){
double x=in.readDouble();
double y=in.readDouble();
double vx=in.readDouble();
double vy=in.readDouble();
double mass=in.readDouble();
String s="ss";
double radius=in.readDouble();
return new Planet(x,y,vx,vy,mass,s,radius);
}
/** Returns the Planet with the maximum value according to Comparator c. */
public static Planet maxPlanet(Planet[] planets, Comparator<Planet> c) {
// REPLACE THIS LINE WITH YOUR SOLUTION
int length=planets.length;
int index=0;
Planet temp=planets[0];
for(int i=0;i<length;i++){
if(c.compare(planets[index],planets[i])<0) index=i;
}
return planets[index];
}
public static void main(String args[]){
In in= new In("data/planets.txt");
int numOfPlanets=in.readInt();
double radius=in.readDouble();
Planet [] pArray=new Planet[numOfPlanets];
for(int i=0;i<numOfPlanets;i++){
pArray[i]=getPlanet(in);
// pArray.draw();
}
//it is essitially a kind of function pointer
Planet p= maxPlanet(pArray,new MassComparator());
System.out.println(p.mass);
}
}
看似简单,但是还是写了非常久。。。。。
|
|