java权重代码 java权重是什么意思
如何在java代码中设置权重
editText.setLayoutParams(new LinearLayout.LayoutParams(width, height, weight)); 只有LinearLayout才有layout_weight属性。
创新互联建站专业为企业提供菏泽网站建设、菏泽做网站、菏泽网站设计、菏泽网站制作等企业网站建设、网页设计与制作、菏泽企业网站模板建站服务,十余年菏泽做网站经验,不只是建网站,更提供有价值的思路和整体网络服务。
设计一种java数据结构,按照权重进行排序
public enum SortBy {
Number,
Weights1,
Weights2
}
import java.util.Comparator;
public class MyComparator implements ComparatorStudent{
private SortBy sortBy;
public MyComparator (SortBy sortBy){
this.sortBy = sortBy;
}
@Override
public int compare(Student s1, Student s2) {
switch (this.sortBy) {
case Weights1:
return s1.getWeights1() - s2.getWeights1();
case Weights2:
return s1.getWeights2() - s2.getWeights2();
default:
return s1.getNumber() - s2.getNumber();
}
}
}
import java.util.Collections;
import java.util.Comparator;
import java.util.LinkedList;
import java.util.List;
public class Student {
private int number;
private int weights1;
private int weights2;
public Student(int number, int weights1, int weights2) {
this.number = number;
this.weights1 = weights1;
this.weights2 = weights2;
}
public int getNumber() {
return this.number;
}
public int getWeights1() {
return this.weights1;
}
public int getWeights2() {
return this.weights2;
}
public static void main(String[] args){
final Student s1 = new Student(1, 20, 300);
final Student s2 = new Student(2, 10, 100);
final Student s3 = new Student(3, 30, 200);
final ListStudent array = new LinkedListStudent();
array.add(s1);
array.add(s2);
array.add(s3);
final ComparatorStudent numberComparator = new MyComparator(SortBy.Number);
Collections.sort(array, numberComparator);
System.out.println("sort by number");
System.out.print("Number: ");
for(Student s: array){
System.out.print(s.getNumber() + " ");
}
System.out.println("\n\n");
final ComparatorStudent weights1Comparator1 = new MyComparator(SortBy.Weights1);
System.out.println("sort by weights1");
System.out.print("Number: ");
Collections.sort(array, weights1Comparator1);
for(Student s: array){
System.out.print(s.getNumber() + " ");
}
System.out.println("\n\n");
final ComparatorStudent weights2Comparator = new MyComparator(SortBy.Weights2);
System.out.println("sort by weights2");
System.out.print("Number: ");
Collections.sort(array, weights2Comparator);
for(Student s: array){
System.out.print(s.getNumber() + " ");
}
System.out.println("\n\n");
}
}
sort by number
Number: 1 2 3
sort by weights1
Number: 2 1 3
sort by weights2
Number: 2 3 1
java实现最大权重单增序列
import java.util.Scanner;
public class Main {
public static int data[] = null;
public static int weight[] = null;
public static int res[] = null;
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner scan = new Scanner(System.in);
System.out.println("输入序列个数:");
int n = 0;
n = scan.nextInt();
System.out.println("输入"+n+"个数(空格隔开):");
data = new int[n];
for (int i = 0; i n; ++i) {
data[i] = scan.nextInt();
}
System.out.println("输入"+n+"个数的权重(空格隔开):");
weight = new int[n];
for (int i = 0; i n; ++i) {
weight[i] = scan.nextInt();
}
doit(n);
int max = res[0];
int k = 0;
for (int i = 1; i n; ++i) {
if (max res[i]) {
max = res[i];
k = i;
}
}
System.out.println("最大权值:"+max);
System.out.println("序列为:");
print(k);
System.out.println();
}
public static void doit(int n) {
res = new int[n];
for (int i = 0; i n; ++i) {
res[i] = weight[i];
for (int j = 0; j i; ++j) {
if (data[i] data[j] res[i] weight[i]+res[j]) {
res[i] = weight[i]+res[j];
}
}
}
}
public static void print(int k) {
for (int i = k-1; i = 0; --i) {
if (res[i]+weight[k] == res[k]) {
print(i);
}
}
System.out.print(" "+data[k]);
}
}
输入样例:
输入序列个数:
5
输入5个数(空格隔开):
1 4 2 3 9
输入5个数的权重(空格隔开):
1 10 1 1 1
输出样例
最大权值:12
序列为:
1 4 9
注:序列可能不唯一,就输出了一组
java按权重选取对象,大家知道怎么做吗?
int rand = 49;//随机数
int sum= 0;
List大海 list = new ArrayList大海();//假设这是一个武将列表
for(int i=0;ilist.size();i++){
sum = list.get(i).getWeight();//大海的出现机率
if(rand=sum){
return i;//返回该武将对应的索引
}
}
本文标题:java权重代码 java权重是什么意思
当前URL:http://pwwzsj.com/article/higsis.html