随机对角2D阵列输入 [英] Random Diagonal 2D Array Input

查看:77
本文介绍了随机对角2D阵列输入的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将对角线的单词输入到C中的2d数组中.我能够对水平和垂直的单词进行此操作,但是在这里比较棘手,因为可以对角线向上或向下输入单词.我希望变量X在循环的第一次迭代中为0,然后对于循环的其余部分递增,或者对于循环的其余部分递减(无论哪个,它都是随机选择的).有可能吗?

I'm trying to input words diagonally into a 2d array in C. I was able to do this for words horizontally and vertically, however it's trickier here because the words can be input going upwards or downwards diagonally. I want the variable X to be 0 for the first iteration of the loop, and then either increment for the rest of the loop or decrement for the rest of the loop (whichever one it does to be chosen randomly). Is that possible?

            for(i=0, j = 0; i < wordLen && j < arrayHeight; j++, i++)
                    array[arrayRow+X][arrayColumn+i] = wordList[k][j];

tl; dr我着迷于让X变量以这种方式做特定的事情,而我不知道该怎么做

tl;dr I'm fixated on getting the X variable to do something specific in this manner, and I can't figure out how to do it

推荐答案

为什么不

for(i=0, j = 0; i < wordLength && j < HEIGHT; j++, i++)
    array[arrayRow-i][arrayColumn+i] = animals[k][j];

for(i=0, j = 0; i < wordLength && j < HEIGHT; j++, i++)
    array[arrayRow+j][arrayColumn-i] = animals[k][j];

等不同的方向

或者

for(i=0, j = 0; i < wordLength && j < HEIGHT; j++, i++)
    array[arrayRow+wordlength-i-1][arrayColumn+i] = animals[k][j];

,或者如果您想使用 x

for(i=0, j = 0; i < wordLength && j < HEIGHT; j++, i++)
 {
    x=wordlength-i-1;
    array[arrayRow+x][arrayColumn+i] = animals[k][j];
 }

这篇关于随机对角2D阵列输入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆