![](/img/trans.png)
[英]The task is: Given a group of integers, find the maximum value among them all, how do i do it without arrays using c++?
[英]Check which number nearest to the value 0 among two given integers
我有 2 个整数:
int number1 = 45;
int number2 = 40;
现在我想比较两者并检查哪个更接近数字 0。
作为 output 我想要 true / false。
例如:
if (compare(number1, number2))
{
console.write("number1 is nearer to 0");
}
else
{
console.write("number2 is nearer to 0");
}
Output:
数字 2 更接近于 0
在比较之前,您应该使用 ABS。
我假设您想要实际的 function“比较”,并且整数可以是负数
int compare(int number1, int number2) {
return abs(number1) < abs(number2);
}
int compare(int a, int b) {
if (abs(a) > abs(b)){
std::cout <<b <<" is closer to zero" <<std::endl ;
return b;
}
else{
std::cout <<a <<" is closer to zero" <<std::endl ;
return a;
}
}
这个 function 可能对你有帮助。 它既在终端中显示答案,又返回接近 0 的 int 值,以便程序的其他部分使用。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.