华为机试—掷骰子游戏

在掷骰子游戏中。会依据所掷数字在地图中前进几步。前进完毕后须要依据当前地图位置所看到的的障碍进行对应操作,当中障碍表示:
1)9:无障碍
2)1:停掷一轮,即下轮所掷数字无效。
3)2:后退两步,假设已经到起点不再后退。
4)3:奖励前进一步

假设在游戏过程中,已经走到地图终点,则游戏结束。依据输入的地图数组,和5个骰子数的数组,返回终于玩家前进了多少步。

演示样例
1)输入:map_len = 15, map = {9,1,9,9,9,2,9,9,9,9,9,9,9,9,9}。dice_val = {1,2,1,3,1},
返回:4
2)输入:map_len = 16, map = {9,9,9,9,9,1,9,3,9,9,2,9,9,9,9,9},dice_val = {2,1,4,1,6},
返回:15

#include <stdio.h>

void dice(int map_len, int* map, int* dice_val, int* output)
{
    int step=0, i;
    for(i=0; i<5; i++)
    {
        step = step + dice_val[i];
        if(step>=map_len - 1)
        {
            step = map_len - 1;
            break;
        }
        else if(map[step] == 1)
            i++;
        else if(map[step] == 2)
        {
            if(step>1)
                step = step - 2;
            else
                step = 0;
        }
        else if(map[step] == 3)
            step++;
    }
    *output = step;
    printf("the output is %d
",*output);
}

int main()
{
    int map_len = 15,  map[] = {9,1,9,9,9,2,9,9,9,9,9,9,9,9,9},  dice_val[] = {1,2,1,3,1};//4
    //int a=0 , * output=&a;
    int output[1];
    dice(map_len, map,  dice_val,  output);

    int map_len1 = 16,  map1[] = {9,9,9,9,9,1,9,3,9,9,2,9,9,9,9,9}, dice_val1[] = {2,1,4,1,6};//15
    //int a1=0 , * output1=&a1;
    int output1[1];
    dice(map_len1, map1,  dice_val1,  output1);

    return 0;
}

原文地址:https://www.cnblogs.com/bhlsheji/p/5272387.html