N
N
Nubik2014-06-09 07:22:43
C++ / C#
Nubik, 2014-06-09 07:22:43

I wrote a program, but I doubt a little about the correctness of the conditions

Task:
The field of the chessboard is determined by a pair of numbers (a,b), each from 1 to 8, the first number is the column number, the second is the row number. 2 cells are given. Determine if the chess king can get from the first cell to the second in 1 move?

#include <iostream>
//
using namespace std;
int main()
{
int a, b, c, d;
cout << "Vvedite nomer stroki\n";
cin >> a;
cout << "Vvedite nomer stolbca\n";
cin >> b;
cout << "Vvedite nomer stroki drugoi kletki\n";
cin >> c;
cout << "Vvedite nomer stolbca drugoi kletki\n";
cin >> d;
int as = a - c;
int asa =b - d;
if (as < 0) as = -as;
if (asa < 0) asa = -asa;
if (as + asa == 1)
    cout<<"YES";
  else
    if (as + asa == 2 && as == 1 && asa == 1)
      cout<<"YES ";
    else
      cout<<"NO ";
    system ("PAUSE");
return 0;
}

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
Sumor, 2014-06-09
@Nubik

The condition as + asa == 2 is redundant, since it follows from the condition as == 1 && asa == 1.
You can use the abs function to calculate the modulus. Or, instead of if, when choosing a sign, use the conditional operator ?:
For the first else, it is better to immediately use brackets {} - so as not to get confused.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question