#22372: 運用switch轉換數值變成文字 //C++


dick930924@gmail.com (諾亞)

School : No School
ID : 127460
IP address : [111.241.92.215]
Last Login :
2020-08-28 23:12:30
a003. 兩光法師占卜術 | From: [111.241.92.215] | Post Date : 2020-08-27 21:29

#include <iostream>

using namespace std;

int main(){

int M, D, S; //宣告三個變數(整數)

cin>>M>>D; //輸入題目要求

S = (M * 2 + D) % 3; //將運算結果存取在變數S

switch(S){

case 0:

cout<<"普通"<<endl;

break;

case 1:

cout<<"吉"<<endl;

break;

case 2:

cout<<"大吉"<<endl;

break;

}

return 0;

}

 
#23272: Re:運用switch轉換數值變成文字 //C++


10931537@gafe.cksh.tp.edu.tw (冠庭黃)

School : No School
ID : 134450
IP address : []
Last Login :
2020-09-27 17:37:57
a003. 兩光法師占卜術 | From: [220.129.64.180] | Post Date : 2020-11-02 20:44

投機取巧方式:
利用case未用break指令會繼續往下執行
直接將大吉的吉字和吉一起運行

#include <iostream>

using namespace std;

 

int main(){

int m, d, s;

 

cin >> m >> d;

 

s = (m * 2 + d) % 3;

 

switch(s){

case 0:

cout << "普通";

break;

 

case 2:

cout << "大";

default:

cout << "吉";

break;

}

 

return 0;

}

 
ZeroJudge Forum