找不到第二个数字而不使用数组

给定一个整数序列作为输入,以-1结尾。 (也就是说,输入整数可以是正数,负数或0.输入中的A -1表示输入结束。)

-1不被视为输入的一部分。

找到输入中的第二大数字。 您可能不使用数组。

测试用例通过,但得分为0.0

这是代码

int main() { int a=-32768,b=-32768,temp=-32768; while(1) { scanf("%d",&a); if(a==-1) break; else if(a>0) { temp=b; b=a; } else { if(a>b) { temp=b; b=a; } else { temp=b; } } } printf("%d",temp); return 0; } 

以你的方式检查价值。 例如,像这样

 #include  #define EOI -1 //End Of Input int main(void){ int input, first, second; first = second = EOI; while(1){ if(1 != scanf("%d", &input)){ fprintf(stderr, "invalid input!\n"); return -1; } if(input == EOI) break; if(first == EOI){ first = input; } else if(first <= input){ second = first;//slide first = input; } else if(second == EOI || second < input){ second = input; } } if(second == EOI){//first == EOI || second == EOI fprintf(stderr, "No valid value is entered more than one!\n"); return -2; } printf("%d\n", second); return 0; }