亲兄弟数之java版

来源:互联网 发布:怎么在mac上设置邮箱 编辑:程序博客网 时间:2024/04/29 07:22
package 期末算法设计;

import java.util.ArrayList;
import java.util.Scanner;

//亲兄弟问题
public class brother {

int max;//最大兄弟数
int a[]=new int[100];//保存兄弟
int brotherA[] = new int[100];//保存亲兄弟
public void getA(){//输入兄弟

Scanner read = new  Scanner(System.in);
max=read.nextInt();
for(int i=0;i
a[i]=read.nextInt();
  }
}
public void findBrother(){//找兄弟
int i,j;
brotherA[max-1]=-1;
for( i=0;i
brotherA[i]=-1;
for( j=i+1;j
if(a[i]<=a[j]&&brotherA[i]<0){
brotherA[i]=j;
}
}
}
}
public void showBrother(){//输出兄弟
for(int i=0;i
System.out.print(brotherA[i]+" ");
}
}
    
public static void main(String[] args){
brother aBrother = new brother();
aBrother.getA();
aBrother.findBrother();
aBrother.showBrother();
}
}

0 0
原创粉丝点击