高中生程式解題系統:a003: 兩光法師占卜術

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

題目連結 https://zerojudge.tw/ShowProblem?problemid=a003

此題是練習餘數運算,底下用C、C++、Python、Java做練習。

C程式碼:
#include <stdio.h>

int main() {
 char *msg[] = {"普通", "吉", "大吉"};
 int m;
    while(scanf("%d", &m) != EOF) {
  int d;  
        scanf("%d", &d);
  m = (m*2+d) % 3;
  printf("%s\n", msg[m]);
    }
    return 0;
}

C++程式碼:
#include <iostream>
using namespace std;

int main() {
 char *msg[] = {"普通", "吉", "大吉"};
 short m;
    while(cin >> m){
  short d;  
        cin >> d;
  
  m = (m*2+d) % 3;
  cout << msg[m] << endl;
    }
    return 0;
}

Python程式碼:
import sys
msg = ["普通", "吉", "大吉"]
for line in sys.stdin:
  [m, d] = line.split()
  m = int(m)
  d = int(d)
  m = (m*2+d) % 3
  print(msg[m])

Java程式碼:
import java.util.Scanner;
public class JAVA {
 public static void main(String[] args) {
  Scanner cin = new Scanner(System.in);
     String msg[] = {"普通", "吉", "大吉"};
     int m;
  while (cin.hasNext()) {
      m = cin.nextInt();
   int d;  
   d = cin.nextInt();
   m = (m*2+d) % 3;
   System.out.println(msg[m]);
  }
 }
}

沒有留言: