APCS 實作題 10610 第1題邏輯運算子參考解法

若您覺得文章寫得不錯,請點選文章上的廣告,來支持小編,謝謝。

題目說明請看高中生解題系統上的「c461: apcs 邏輯運算子 (Logic Operators)」或是底下圖片。


此題考C語言的「非零為True之觀念」,解法很單純,將非零的數值轉為True,以及邏輯運算就可以了。筆者用四種版本的語言來實作。

C 語言:
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
#include <stdio.h>
#include <stdbool.h>

int main() {
  int a, b, c;

  while(scanf("%d %d %d", &a, &b, &c) == 3)
  {
    // 非零轉為True
    if(a > 0) a = true;
    if(b > 0) b = true;
    c == 1 ? true : false;

    bool hasAns = false;
    
    // 邏輯運算結果
    if( (a & b) == c )
    {
      printf("AND\n");
      hasAns = true;
    
    }
    if( (a | b) == c )
    {
      printf("OR\n");
      hasAns = true;
    }

    if( (a ^ b) == c )
    {
      printf("XOR\n");
      hasAns = true;
    }

    if( hasAns == false )
      printf("IMPOSSIBLE\n");
  }

  return 0;
}

C++語言:
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
#include <iostream>
#include <cstdbool>

using namespace std;

int main() {
  int a, b, c;
  while(cin >> a >> b >> c)
  {
    // 非零轉為True
    if(a > 0) a = true;
    if(b > 0) b = true;
    c == 1 ? true : false;

    bool hasAns = false;
    // 邏輯運算結果
    if( (a & b) == c )
    {
      cout << "AND" << endl;
      hasAns = 1;
    }

    if( (a | b) == c )
    {
      cout << "OR" << endl;
      hasAns = 1;
    }

    if( (a ^ b) == c )
    {
      cout << "XOR" << endl;
      hasAns = 1;
    }

    if( hasAns == 0 )
      cout << "IMPOSSIBLE" << endl;
  }

  return 0;
}

Java語言:
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
import java.util.*;

class Main {
  public static void main(String[] args) {
    Scanner cin = new Scanner(System.in);
  int a, b, c;
    boolean aFlag, bFlag, cFlag;

  while (cin.hasNext()) {
      a = cin.nextInt();
      b = cin.nextInt();
      c = cin.nextInt();

      // 非零轉為True
      aFlag = a > 0 ? true: false;
      bFlag = b > 0 ? true: false;
      cFlag = c > 0 ? true: false;
      
      boolean hasAns = false;
      // 邏輯運算結果
      if( (aFlag & bFlag) == cFlag )
      {
        System.out.println("AND");
        hasAns = true;
      }

      if( (aFlag | bFlag) == cFlag )
      {
        System.out.println("OR");
        hasAns = true;
      }

      if( (aFlag ^ bFlag) == cFlag )
      {
        System.out.println("XOR");
        hasAns = true;
      }

      if( hasAns == false )
        System.out.println("IMPOSSIBLE");
      }
  }
}

Python語言:
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
import sys

for num in sys.stdin:
    a,b,c = list(map(int, num.split()))
    
    #非零轉為True
    if( a > 0):
      a = True
    else:
      a = False;
  
    if( b > 0):
      b = True
    else:
      b = False;
  
    if( c > 0):
      c = True
    else:
      c = False
  
    hasAns = False
    
    #邏輯運算結果
    if( (a & b) == c ):
      print("AND")
      hasAns = True

    if( (a | b) == c ):
      print("OR")
      hasAns = True

    if( (a ^ b) == c ):
      print("XOR")
      hasAns = True

    if( hasAns == False ):
      print("IMPOSSIBLE")

最後放個艾鍗科技對此題的解說影片: