找出3個數中不爲-1的最小數

假設有3個數,它們的取值都可能爲-1,現在要求找出其中不爲-1的最小的數。本來我一開始的想法是利用8次if判斷,知道我看到一位老師如下去實現。

int taaIndex = findStopCodon(dna, startIndex, "TAA");
int tagIndex = findStopCodon(dna, startIndex, "TAG");
int tgaIndex = findStopCodon(dna, startIndex, "TGA");
        
int minIndex = 0;
if (taaIndex == -1 || 
         (tgaIndex != -1 && tgaIndex < taaIndex)) {
               minIndex = tgaIndex;
}
else {
      minIndex = taaIndex;
}
        
if (minIndex == -1 ||
         (tagIndex != -1 && tagIndex < minIndex)) {
                minIndex = tagIndex;
}            
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章