iT邦幫忙

第 11 屆 iThome 鐵人賽

DAY 19
1
自我挑戰組

資工系大一課程/日常筆記系列 第 19

[Day 19] 第三堂程式設計

這次的題目是 UVA10409: Die Game
題目主要是骰子經過旋轉後,由上往下看的數字為多少,且1,2,3三面分別為朝上,朝北,及朝西。

#include <stdio.h>
#include <string.h>
int main(){
	int n;
	while(scanf("%d",&n)){
		if(n==0) return 0;
		int up=1,down=6,north=2,south=5,east=4,west=3; //宣告每面的數字,但其實可以優化到只宣告兩個,但在這裡先沒有喔~
		int i=0;
		for(i=0;i<n;i++){
			char act[10];
			scanf("%s",act);
			if(act[0]=='n'){ //以下為旋轉骰子的過程
				int ss = down;
				down = north;
				north = up;
				up = south;
				south = ss;
			}
			else if(act[0]=='e'){
				int ss = down;
				down = east;
				east = up;
				up = west;
				west = ss;
			}
			else if(act[0]=='s'){
				int ss = down;
				down = south;
				south = up;
				up = north;
				north = ss;
			}
			else if(act[0]=='w'){
				int ss = down;
				down = west;
				west = up;
				up = east;
				east = ss;
			} //以上為旋轉骰子的過程
		}
		printf("%d\n",up);
	}
	return 0;
}

上一篇
[Day 18] 教授請假,來寫程設作業
下一篇
[Day 20] 週五寫程設作業
系列文
資工系大一課程/日常筆記30
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言