我有两个已经给出值的并行数组。一个是短[标记]和另一个字符串[名称]。我想比较两者的价值观,这样我就可以打印出学生多年来获得特定成绩的次数。值已经定义并给出。所以如果:索菲已经收到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.
我无法使用类进行此分配。
任何人都可以帮助我开始吗?
答案 0 :(得分:0)
一个建议是从数组,标记和名称,通用对<name, mark>
构建。然后,您只需计算每name
个相同对的数量,即可获得*
的数量
希望它有所帮助!
答案 1 :(得分: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"]
我可能会错过几步,但这是一个粗略的想法,我希望你理解它。最好有一个第三个数组而不是替换名称[],好吧,自己决定。总的来说,它可能需要3-4个嵌套循环(我可以想象这...... =。=)
答案 2 :(得分: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
}
}
并不是说这对于课程来说更容易。多么奇怪的任务