我是 CP 的新手,当我在做这个在三个数字中找到第二个最大值的问题时,我编写了这段代码,它适用于 int,但即使它们是相同的数据类型,也不会工作很长时间。
输入格式:
第一行包含三元组的数量,N。
接下来的 N 行每行都有三个空格分隔的整数。
使用的输入:
3
1 2 3
10 15 5
100 999 500
#include<bits/stdc++.h>
using namespace std;
int main(){
int n;
cin >>n ;
while(n--){
long a,b,c,d;//if i change long to int output is correct
scanf("%i%i%i",&a,&b,&c);
d=min(max(a,b),max(a,c));
printf("%i\n",d);// outputs 1 \n 10 \n 100 \n(\n means new line)
}
return 0;
}
最佳答案
可能是因为你用错了specifier . %i
用于int
,而%li
用于long
。
关于c++ - 为什么 long 在查找三个整数的第二大时显示错误输出?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50570980/