-2

我有 2 个已给出值的并行数组。一个是short[marks],另一个是String[names]。我想比较两者的值,这样我就可以打印出学生多年来获得特定成绩的次数。值已经定义和给出。所以如果: Sophie 收到了 85/100,一年内收到了 5 次。我希望它打印:

       Sophie : *****

我知道这是我必须做的:

Use an array, such that the 100th index corresponds to the grade 100, the 99th
index corresponds to the grade 99, etc. This way if somebody gets a grade g you 
can just increment the gth element of the array.

我不能为这项作业使用课程。

谁能帮我开始?

4

3 回答 3

0

One suggestion is to build from both arrays, marks and names, generic pairs <name, mark>. Then you just have to count the number of identical pairs you have per name to obtain the number of *

Hope it helps!

于 2013-04-16T14:37:31.690 回答
0

好的...为了这个奇怪的任务,让我们做一些奇怪的事情(我不知道这是否可行)......实际上可能还有糟糕的编码和性能......无论如何......

现在我们有 2 个数组:名称和标记。例如,我们有 ["A", "B", "A", "A"] 用于名称和 [80,100,80,70] 用于标记。

1) Loop through names and if you do not find, say "-1", set a temp var called, say, candidate to store the person i.e. "A" at first.
2) Get the corresponding mark from marks[]
3) Replace name with the cast-ed mark => names["80", "B", "A", "A"]
4) Keep looping names and repeat the above if the name = candidate 
5) At the end, you have names["80", "B", "80", "70"]
6)Loop through it and Make two temp var, say, count and target to record the first mark not equals "-1" and not containing any character i.e A-Z
7) count is used to keep the count of the target. So, target="80" and count = 2 in the 1st loop.
8) Set the element to "-1" when you have accessed it.
9) Print candidate, target and count <-- do the * thing
10) names become ["-1", "B", "-1", "70"]
11) repeat everything
12) The idea is like names[] will go like:  ["A", "B", "A", "A"] => ["80", "B", "80", "70"] => ["-1", "B", "-1", "70"] => ["-1", "B", "-1", "-1"] => ["-1", "100", "-1", "-1"] => ["-1", "-1", "-1", "-1"]

我可能错过了几个步骤,但这是一个粗略的想法,我希望你能理解。最好有一个第三个数组而不是替换 names[],好吧,自己决定。总的来说,它可能需要 3-4 个嵌套循环(我无法想象这...=.=)

于 2013-04-16T15:25:33.513 回答
0

好的,因为你不能使用类......不确定你是指你自己的类还是任何java类(除了像String这样的极端基础......)所以我假设最坏的情况。

您的输入是:

  • String[] names包含一堆名字
  • int[] marks包含一堆标记(对应于相应索引的名称
  • int markToFind您要查找其出现的标记

你的输出是:

  • String[] uniqueNames再次包含名称,但唯一
  • int[] markOccurences包含每个名称的出现次数(与前一个数组的相应索引)

算法(伪java,可以自己修)

{

  String[] uniqueNames = new String[names.length]; // this is oversized, which we can live with.  Counting uniques without a Set is annoying
  int[] markOccurences = new int[uniqueNames.length]; // will default to 0 everywhere

  for (int i=0; i<names.length; i++){
    int nameIdx = addUnique(uniqueNames, names[i]);
    if (marks[i] == markToFind) markOccurences[nameIdx]++;
  }

  // print asterisks per name (insert your code here)
}

int addUnique(String[] array, String element){
  int idx = indexOf(array, element);
  if (idx<0){
    idx = indexOf(array, null);
    array[idx] = element;
  }
  return id;
}

int indexOf(String[] haystack, String needle){
 // implement naively
}

}

并不是说上课会容易得多。多么奇怪的任务

于 2013-04-17T08:01:40.180 回答