Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/algorithm/10.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/asp.net-mvc/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 编写算法的帮助_Java_Algorithm - Fatal编程技术网

Java 编写算法的帮助

Java 编写算法的帮助,java,algorithm,Java,Algorithm,我正在为一项作业设计一个算法,我不确定我写的算法是否正确,请你指导我好吗?问题是: 有n名学生S1、S2、…、Sn和n级:G1、G2、…、Gn。每个学生必须被分配到恰好一个年级,并且恰好一个学生被分配到任何一个年级。如果Tij是将Si赋值给Gj的值,我必须找到T的Q子集,它是最大的。(我必须指派工人从事尽可能好的工作) 这个问题的一个例子是,如果我有两个学生S1和S2,还有两个年级G1和G2,我有T12=12,T21=7,T11=9,T22=16,那么子集必须是Q={T12,T22} 我已经用j

我正在为一项作业设计一个算法,我不确定我写的算法是否正确,请你指导我好吗?问题是: 有n名学生S1、S2、…、Sn和n级:G1、G2、…、Gn。每个学生必须被分配到恰好一个年级,并且恰好一个学生被分配到任何一个年级。如果Tij是将Si赋值给Gj的值,我必须找到T的Q子集,它是最大的。(我必须指派工人从事尽可能好的工作) 这个问题的一个例子是,如果我有两个学生S1和S2,还有两个年级G1和G2,我有T12=12,T21=7,T11=9,T22=16,那么子集必须是Q={T12,T22} 我已经用java编写了以下算法:

算法studentG(J[],W[],V[],x[] { //我使用堆数据结构来解决这个问题 //初始所有x[][]=0 ArrayList students=新ArrayList(); students=Heap(v[]];//此方法为每个学生创建一个堆, 对于(int k=0;k
这行得通吗?谢谢。

你的例子不清楚。选择T12和T22是否违反了你的条件(即有两名学生被分配到二年级)。

你试图解决的实际上是对的重新表述,谷歌会给你很多可能的算法来解决它

您的算法在分配分数时不起任何作用,没有优化。您最好将分数和学生按顺序排列,然后简单地将每个分数和学生分配给另一个。这将为解决方案生成一个可能的集合,但它(可能,1次机会)不是最优的

在实现算法之前,请尝试用伪语言术语来表达它。该算法的一个简单实现可能如下所示:

foreach student S do
  foreach unassigned grade G do
    Add {G, S} to the solutions
    Compute the solution score
    If (this score > greater score so far) Then 
      Keep solution like this
      Mark G as assigned
    Else 
      Remove {G, S} from the solution
  Next
Next
就数据结构而言,Java中可能有:

// The number of grades and students
public static final int N = 10;

// The students and grades are just a suite of numubers
List<int> students = new ArrayList<int>(N);
List<int> grades = new ArrayList<int>(N);    
for (int i=0; i<N; ++i) {
  students.set(i, i);
  grades.set(i, i);
}

// Each score for a possible pair of grade student is stored in a matrix 
int[][] scores = new int[N][N];
for (int s=0; s<N; ++s) {
  for (int g=0; g<N; ++g) {
    scores[s][g] = students.get(s) * grades.get(g);
  }
}

// An association of student and grade
class Association {
  int student;
  int grade;
  int score;
  public Association(int student, int grade, int score) {
    this.student = student;
    this.grade = grade;
    this.score = score;
  }
}

// The solution
Stack<Association> solution = new Stack<Association>(N);
//年级和学生人数
公共静态最终整数N=10;
//学生和分数只是一组数字
列表学生=新数组列表(N);
列表等级=新阵列列表(N);
对于(inti=0;i@Samuel:“每个学生必须被分配到一个年级,而任何一个年级都必须分配到一个学生。”

这意味着具有将学生映射到任何年级S->G的函数。该条件似乎不会引入旁侧约束(即,在保持1对1约束的同时,必须以最佳方式在学生集合中分配所有年级。)


因此,本质上(如果问题的表述确实正确),这意味着简单地选择

Q=argmax_j(Tij)表示所有i的

它只是成本矩阵T的每一行的最大值


我想我不必提供代码示例,因为查找最大元素是O(n)的一个相当简单的操作。如果需要,可以使用堆,但是简单的扫描和保持最大值也可以

因为这看起来太简单了,所以问题的表述可能不正确

// The number of grades and students
public static final int N = 10;

// The students and grades are just a suite of numubers
List<int> students = new ArrayList<int>(N);
List<int> grades = new ArrayList<int>(N);    
for (int i=0; i<N; ++i) {
  students.set(i, i);
  grades.set(i, i);
}

// Each score for a possible pair of grade student is stored in a matrix 
int[][] scores = new int[N][N];
for (int s=0; s<N; ++s) {
  for (int g=0; g<N; ++g) {
    scores[s][g] = students.get(s) * grades.get(g);
  }
}

// An association of student and grade
class Association {
  int student;
  int grade;
  int score;
  public Association(int student, int grade, int score) {
    this.student = student;
    this.grade = grade;
    this.score = score;
  }
}

// The solution
Stack<Association> solution = new Stack<Association>(N);