简体   繁体   English

C:使用数组,一直给我错误的输出

[英]C: Using Array, keeps giving me wrong output

I have been struggling with this all day, the output I get is all correct execept the middle result. 我整天都在努力,除了中间结果,我得到的输出都是正确的。 If anyone can help me it would be great. 如果有人可以帮助我,那就太好了。 The output I get is for id[A] instead of id[C], it seems like in the loop the ID for A is carried to C. 我得到的输出是给id [A]而不是id [C],似乎在循环中,A的ID被带给了C。

    int firstA=0, firstS=0, secondS=0, firstC=0, secondC=0;

    for (row=0; row<totalSize; row++) {

        if (category[row]=='A' && ranking[firstA] < ranking[row]) {
            firstA = row;

        }

        if (category[row]=='C' && ranking[firstC] < ranking[row]) {
            secondC = firstC;
            firstC = row;
        }
        else if (category[row]=='C' && ranking[secondC] < ranking[row]) {
            secondC = row;
        }

        if (category[row]=='S' && ranking[firstS] < ranking[row]) {
            secondS = firstS;
            firstS = row;
        }
        else if (category[row]=='S' && ranking[secondS] < ranking[row]) {
            secondS = row;
        }


    }

    printf("A : %d %.2lf \n", id[firstA], ranking[firstA]);
    printf("C : %d %.2lf \n", id[firstC], ranking[firstC]);
    printf("C : %d %.2lf \n", id[secondC], ranking[secondC]);
    printf("S : %d %.2lf \n", id[firstS], ranking[firstS]);
    printf("S : %d %.2lf \n", id[secondS], ranking[secondS]);

    return 0;
}

INPUT FILE 输入文件

10
14 A 447 252 68 34 978
2 C 230 299 597 180 9
27 A 318 220 97 28 1317
32 C 563 450 547 112 28
8 C 669 260 200 36 171
11 S 179 45 1342 732 174
19 S 74 249 861 1165  6 
21 A 757 240 97 119 2032
15 S 275 177 588 577 52
6 C 886 401 327 109 48

EXPECTED OUTPUT 预期的输出

A: 21 1171.00
C: 6 696.70
C: 32 578.00
S: 11 1094.20
S: 19 1046.50

The problem is here, I get this 问题在这里,我明白了

A : 21 1171.00
C : 6 696.70
C : 14 601.10
S : 11 1094.20
S : 19 1046.50

The Middle C is carrying the ID of one of the A's. 中间C带有A之一的ID。 I can't seem to figure out whats wrong in my loop. 我似乎无法弄清楚循环中出了什么问题。 Any help would be appreciated! 任何帮助,将不胜感激!

The secondS is initialised to row 0 which is category 'A'. secondS初始化到类别'A'的第0行。 The rank of that row is less than the expected secondS row, so secondS stays 0. You need to do something so the initial values will always get assigned to the correct category, like this 该行的等级小于期望的secondS行,因此secondS保持为0。您需要执行一些操作,以便始终将初始值分配给正确的类别,如下所示

   int firstA=-1, firstS=-1, secondS=-1, firstC=-1, secondC=-1;

   for (row=0; row<totalSize; row++) {

       if (category[row]=='A' && (firstA == -1 || ranking[firstA] < ranking[row])) {
           firstA = row;
       }

       if (category[row]=='C' && (firstC == -1 || ranking[firstC] < ranking[row])) {
           secondC = firstC;
           firstC = row;
       }
       else if (category[row]=='C' && (secondC == -1 || ranking[secondC] < ranking[row])) {
           secondC = row;
       }

       if (category[row]=='S' && (firstS == -1 || ranking[firstS] < ranking[row])) {
           secondS = firstS;
           firstS = row;
       }
       else if (category[row]=='S' && (secondS == -1 || ranking[secondS] < ranking[row])) {
           secondS = row;
       }


   }

Since all of the initial indices are -1, as soon as you see a row of the corresponding category they are assigned. 由于所有初始索引均为-1,因此一旦您看到对应类别的行,便会对其进行分配。 Initialising an index as -1 can be dangerous, but this is safe because C should stop evaluating the conditional statements when it sees eg firstA == -1, and will never evaluate ranking[-1] due to short circuit evaluation. 将索引初始化为-1可能很危险,但这是安全的,因为C在看到例如firstA == -1时应停止评估条件语句,并且由于短路评估而绝不会评估等级[-1]。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM