簡體   English   中英

如果 x 為正,我如何將 x 設置為負值,如果 x 為正,我如何將其設置為負。 c++

[英]How do i set x to be a negative value if x is positive, and if x is positive how do i set it to be negative. c++

我正在制作一個人工智能/算法來在 2x2 正方形中找到一個棋盤格圖案。 我需要它們對 hidden_1 和 hidden_3 為負,但對其他 2 為正。

#include <iostream>
#include <math.h>
#include <string.h>  
    
using namespace std;
    
int main (){
    int x;
    int y;
    int a;
    int b;
    int x1 = -x;
    int y1 = -y;
    int a1 = -a;
    int b1 = -b;
    cout << "Is there a square in the top left?\n1 for Yes/-1 for No\n";
    cin >> x;
    cout << "Is there a square in the bottom right?\n1 for Yes/-1 for No\n";
    cin >> y;
    cout << "Is there a square in the bottom left?\n1 for Yes/-1 for No\n";
    cin >> a;
    cout << "Is there a square in the top right?\n1 for Yes/-1 for No\n";
    cin >> b;


    int hidden_1 << x1 + y1 + -1;
    int hidden_2 << x + y + -1;
    int hidden_3 << a1 + b1 + -1;
    int hidden_4 << a + b + -1;

您使用未讀取的變量的負數初始化 x1, y1, a1, b1。 在閱讀 x、y、a、b 之后嘗試聲明它們。

#include <iostream>
#include <math.h>
#include <string.h>  

using namespace std;

int main () {
  int x;
  int y;
  int a;
  int b;

  cout << "Is there a square in the top left?\n1 for Yes/-1 for No\n";
  cin >> x;
  cout << "Is there a square in the bottom right?\n1 for Yes/-1 for No\n";
  cin >> y;
  cout << "Is there a square in the bottom left?\n1 for Yes/-1 for No\n";
  cin >> a;
  cout << "Is there a square in the top right?\n1 for Yes/-1 for No\n";
  cin >> b;

  int x1 = -x;
  int y1 = -y;
  int a1 = -a;
  int b1 = -b;

  int hidden_1 = x1 + y1 + -1;
  int hidden_2 = x + y + -1;
  int hidden_3 = a1 + b1 + -1;
  int hidden_4 = a + b + -1;

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM