绝对值/减法
absolute value / subtraction
我正在尝试解决一个问题:
编写一个程序计算非负整数之间的差值。
输入:
输入的每一行都由一对整数组成。每个整数都在 0 到 10 之间提高到 15(含)。输入在文件末尾终止。
输出:
对于输入中的每一对整数,输出一行,包含它们差的绝对值。
这是我的解决方案:
#include <iostream>
#include <cmath>
using namespace std;
int main(){
int x;
int y;
int z;
cin >> x >> y;
if(x && y <= 1000000000000000){
z=x-y;
cout << std::abs (z);
}
}
但问题是我的答案是错误的,请帮我更正我的答案并说明错误的原因。
你的条件写错了。应该是
if(x <= 1000000000000000 && y <= 1000000000000000) {
z=x-y;
cout << std::abs (z);
}
但是,您的条件 if(x && y <= 1000000000000000)
用简单的英语计算为 if x is true AND y is less than or equal to 1000000000000000
,这绝对不是您想要的。按照您的操作方式,当 y
小于 1000000000000000
并且 x
是零以外的任何值, 正或负 [=38 时,条件将评估为真=].这背后的原因是在 C 中任何非零值的计算结果为真。
注意:当 x
和 y
被赋予任何不在 int
范围内的值时,您的代码将失败,即 - 2^31 到 2^31-1(考虑 int
是 32 位,这是最常见的)。您必须在此处为 x
、y
和 z
使用 long long
数据类型。
long long x, y, z;
long long
的范围是-2^63到2^63-1,这个范围足以容纳指定范围内的任意数字,即0
到1000000000000000
。
正如其他人已经提到的那样,您的 if 条件是错误的。对于除零以外的每个值,您的变量 x 结果都为真。所以你必须写 (x <= MAX && y <= MAX).
您还可以使用函数 numeric_limits(限制库)阻止使用固定最大值购买:
#include <iostream>
#include <limits>
#include <cmath>
int main() {
int x;
int y;
int z;
std::cin >> x >> y;
if (x <= std::numeric_limits<long>::max() && y <= std::numeric_limits<long>::max()) {
z=x-y;
std::cout << std::abs (z);
}
}
对于初学者来说,int
类型对象的有效值范围通常小于 1000000000000000
。
您应该使用足够宽的整数类型来存储这么大的值。合适的类型是 unsigned long long int
因为根据赋值输入的值是非负的。
否则,您还需要检查这些值是否不是大于或等于零的负数。
还有if语句中的条件
if(x && y <= 1000000000000000){
错了。相当于
if(x && ( y <= 1000000000000000 )){
反过来等同于
if( ( x != 0 ) && ( y <= 1000000000000000 )){
与
不一样
if ( ( x <= 1000000000000000 ) && ( y <= 1000000000000000 ) ){
程序可以这样看
#include <iostream>
int main()
{
const unsigned long long int UPPER_VALUE = 1000000000000000;
unsigned long long int x, y;
while ( std::cin >> x >> y )
{
if ( x <= UPPER_VALUE && y <= UPPER_VALUE )
{
std::cout << "The difference between the numbers is "
<< ( x < y ? y - x : x - y )
<< std::endl;
}
else
{
std::cout << "The numbers shall be less than or equal to "
<< UPPER_VALUE
<< std::endl;
}
}
}
例如,如果要输入这些值
1000000000000000 1000000000000000
1000000000000001 1000000000000000
1 2
那么程序输出会像
The difference between the numbers is 0
The numbers shall be less than or equal to 1000000000000000
The difference between the numbers is 1
我正在尝试解决一个问题:
编写一个程序计算非负整数之间的差值。
输入:
输入的每一行都由一对整数组成。每个整数都在 0 到 10 之间提高到 15(含)。输入在文件末尾终止。
输出:
对于输入中的每一对整数,输出一行,包含它们差的绝对值。
这是我的解决方案:
#include <iostream>
#include <cmath>
using namespace std;
int main(){
int x;
int y;
int z;
cin >> x >> y;
if(x && y <= 1000000000000000){
z=x-y;
cout << std::abs (z);
}
}
但问题是我的答案是错误的,请帮我更正我的答案并说明错误的原因。
你的条件写错了。应该是
if(x <= 1000000000000000 && y <= 1000000000000000) {
z=x-y;
cout << std::abs (z);
}
但是,您的条件 if(x && y <= 1000000000000000)
用简单的英语计算为 if x is true AND y is less than or equal to 1000000000000000
,这绝对不是您想要的。按照您的操作方式,当 y
小于 1000000000000000
并且 x
是零以外的任何值, 正或负 [=38 时,条件将评估为真=].这背后的原因是在 C 中任何非零值的计算结果为真。
注意:当 x
和 y
被赋予任何不在 int
范围内的值时,您的代码将失败,即 - 2^31 到 2^31-1(考虑 int
是 32 位,这是最常见的)。您必须在此处为 x
、y
和 z
使用 long long
数据类型。
long long x, y, z;
long long
的范围是-2^63到2^63-1,这个范围足以容纳指定范围内的任意数字,即0
到1000000000000000
。
正如其他人已经提到的那样,您的 if 条件是错误的。对于除零以外的每个值,您的变量 x 结果都为真。所以你必须写 (x <= MAX && y <= MAX).
您还可以使用函数 numeric_limits(限制库)阻止使用固定最大值购买:
#include <iostream>
#include <limits>
#include <cmath>
int main() {
int x;
int y;
int z;
std::cin >> x >> y;
if (x <= std::numeric_limits<long>::max() && y <= std::numeric_limits<long>::max()) {
z=x-y;
std::cout << std::abs (z);
}
}
对于初学者来说,int
类型对象的有效值范围通常小于 1000000000000000
。
您应该使用足够宽的整数类型来存储这么大的值。合适的类型是 unsigned long long int
因为根据赋值输入的值是非负的。
否则,您还需要检查这些值是否不是大于或等于零的负数。
还有if语句中的条件
if(x && y <= 1000000000000000){
错了。相当于
if(x && ( y <= 1000000000000000 )){
反过来等同于
if( ( x != 0 ) && ( y <= 1000000000000000 )){
与
不一样if ( ( x <= 1000000000000000 ) && ( y <= 1000000000000000 ) ){
程序可以这样看
#include <iostream>
int main()
{
const unsigned long long int UPPER_VALUE = 1000000000000000;
unsigned long long int x, y;
while ( std::cin >> x >> y )
{
if ( x <= UPPER_VALUE && y <= UPPER_VALUE )
{
std::cout << "The difference between the numbers is "
<< ( x < y ? y - x : x - y )
<< std::endl;
}
else
{
std::cout << "The numbers shall be less than or equal to "
<< UPPER_VALUE
<< std::endl;
}
}
}
例如,如果要输入这些值
1000000000000000 1000000000000000
1000000000000001 1000000000000000
1 2
那么程序输出会像
The difference between the numbers is 0
The numbers shall be less than or equal to 1000000000000000
The difference between the numbers is 1