问题描述
接下来的内容对于 c 程序员来说肯定会显得非常简单,但我正在编写一个小程序来模拟一些名为 gomoku 的游戏.对于用户,您必须输入一个整数 N,它对应于由N 乘 N"个整数组成的N 乘 N"方格.
What is following will surely appear very simple for c coders but I am coding a small program to modelize some game called gomoku. For the user, you have to enter an integer N wich corresponds to a 'N times N' square which consists of 'N times N' integers.
所以代码运行得很好,但我有一些简单的问题:当我输入'N乘N'个整数时,我做了一些
So the code is runnig quite well but I have some simple question : when I enter the 'N times N' integers, I made some
int N;
scanf("%d",&N);
char c[N][N];
while (i<N){
scanf("%s\n",&c[i]);
i++;
}
然后我将每个 c[i]
的 char 转换为 int 以进行一些涉及 c[i][j]
的计算,这是非常不自然的.但是如果我必须声明 int c[N][N]
,就不可能像我输入的那样检索相同的整数 c[i][j]
while 循环正在运行.
then I converted the char to int for each c[i]
to make some computation involving c[i][j]
, which is quite unnatural. But if I had to declare int c[N][N]
, it would be impossible to retrive the same integers c[i][j]
like those I inputed when the while-loop is running.
有没有人有想法声明int c[N][N]
,输入整数,然后用整数计算时同样计算c[i][j] ?
Does anyone has an idea to declare int
c[N][N]
, inputing integers, and then computing the same when computing with integers c[i][j]
?
最好的,新本
推荐答案
您不必读取 char 然后将其转换为 int.你可以简单地读取整数:
You don't have to read char and then convert it to int. You can just simply read integeres:
for(int i = 0; i < N; ++i)
scanf("%d", &c[i]); //of course c has to be int** type
您确定只想读取 N 个整数吗?不是 NN 整个数组?如果您想将 NN 个对象读入数组,代码应如下所示:
And are you sure that you want to read just N integers? Not NN for whole array? In case you want to read NN objects to array, code should look like this:
int N, i, j;
scanf("%d",&N);
int c[N][N];
for(i = 0; i < N; ++i)
{
for(j = 0; j < N; ++j)
{
scanf("%d", &c[i][j]);
/* do something */
}
}
这篇关于在c中正确声明变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!