这道题目是面 fuhu online test 做的三道之一。实现其实很简单,最重要的是想到方法。
Given a function which produces a random integer in the range 1 to 5, write a function which produces a random integer in the range 1 to 7.
int random5();
int random7() {
int vals[5][5] = {
{1,2,3,4,5},
{6,7,1,2,3},
{4,5,6,7,1},
{2,3,4,5,6},
{7,0,0,0,0}
};
int result = 0;
while (result == 0) {
int i = random5();
int j = random5();
result = vals[i-1][j-1];
}
return result;
}