62,628
社区成员
发帖
与我相关
我的任务
分享import java.util.*;
class site implements Cloneable{
char name;
int cost ;
int sale ;
site[] relations;
site(char a,int c,int b){
this.name = a;
this.cost = c;
this.sale = b;
}
site(site s){
this.name = s.name;
this.cost = s.cost;
this.sale = s.sale;
this.relations = s.relations;
}
void set_relations(site[] s){
this.relations = s;
}
int saleadditor(){
int sum = this.sale;
for(int i = 0;i<relations.length;i++){
sum +=relations[i].sale;
}
return sum;
}
double caculator(){
int sum = saleadditor();
return (double)sum*2/this.cost;
}
protected Object clone(){
site s = null;
try {
s = (site)super.clone();
} catch (CloneNotSupportedException e) {
e.printStackTrace();
}
return s;
}
}
public class cost_performance {
static int check(site[] s, site a,int n){ //n表示现有数组中含有的非空元素个数
boolean check = false;
for(int i = 0;i<n;i++){
if(a.name == s[i].name)
check = true;
}
if(check ==false){
s[n] = new site(a);
n++;
}
return n;
}
public static void main(String args[]){
site A,B,C,D,E,F,G;
A = new site('A',240,700);
B = new site('B',280,500);
C = new site('C',220,1100);
D = new site('D',260,800);
E = new site('E',380,600);
F = new site('F',250,1400);
G = new site('G',200,1600);
site[] r1 = {B,D};
site[] r2 = {A,C,E};
site[] r3 = {B,E,F};
site[] r4 = {A,E};
site[] r5 = {B,C,D,F,G};
site[] r6 = {C,E};
site[] r7 = {E};
A.set_relations(r1);
B.set_relations(r2);
C.set_relations(r3);
D.set_relations(r4);
E.set_relations(r5);
F.set_relations(r6);
G.set_relations(r7);
site[] all = {A,B,C,D,E,F,G};
System.out.println("A单独建站的性价比为:"+A.caculator());
System.out.println("B单独建站的性价比为:"+B.caculator());
System.out.println("C单独建站的性价比为:"+C.caculator());
System.out.println("D单独建站的性价比为:"+D.caculator());
System.out.println("E单独建站的性价比为:"+E.caculator());
System.out.println("F单独建站的性价比为:"+F.caculator());
System.out.println("G单独建站的性价比为:"+G.caculator());
for(int i = 0; i<7;i++){
for(int j = 1+i;j<7;j++){
site[] box = new site[7];
box[0] =new site(all[i]);
int num = 1;
for(int a = 0;a<all[i].relations.length;a++){
box[1+i] = new site(all[i].relations[i]);
num++;
}
num = check(box,all[j],num);
for(int b=0;b<all[j].relations.length;b++){
num = check(box,all[j].relations[b],num);
}
int salenum = 0;
int totalcost = 0;
for(int y = 0;y<num;y++){
salenum = salenum+ box[y].sale;
totalcost = totalcost+ box[y].cost;
}
double costperformance = 2*salenum/totalcost;
System.out.println("在" + all[i].name +"和"+all[j].name+"处建站的性价比为"+costperformance);
}
}
}
}