-1

これが私のコードです:配列の入力の入力が10001の場合、それはinput [1] = 0のelseブロックにも入力されますが、if(input [j] == 1の場合は外部に条件を設定しました) )なぜそれが起こっているのか、誰かが教えてもらえますか?

#include<stdio.h>
int main()
{
        unsigned int tcase=0,build=0,i=0,j=0,k=0,count=0;
        unsigned int input[1000];
        scanf("%d",&tcase);
        while(tcase--)
        {
                scanf("%d",&build);
                for(i=0;i<build;i++)
                        scanf("%d",&input[i]);

                for(j=0;j<build;j++)
                {
                        if(input[j]==1)
                        {
                                if(j==0)
                                {       input[j+1]=1;

                                        printf("fddf");
                                }
                                else if(j==(build-1))
                                {
                                        input[j-1]=1;

                                        printf("Gfgf");
                                }
                                else
                                {
                                        input[j+1]=1;
                                        input[j-1]=1;
                                        printf("awGfgf");
                                }
                        }
                }
                for(k=0;k<build;k++)

               {
                        if(input[k]==0)
                                ++count;
                }
                printf("%d\n",count);
        }
        return 0;
}
4

2 に答える 2

1

これは、配列境界の終わりを超えた値をチェックし、不確定な値でメモリをテストしているためです。

アレイは次のように定義されます

unsigned int input[1000];

ステートメント

if(input[j]==1)

jが10001の場合、配列境界の終わりを超えてメモリをテストします。そのメモリの価値は未定義であり、実際には多くの要因に依存します。そのメモリアドレスの値が1になる可能性は非常に低いです(実際、メモリが本当にランダムに初期化されている場合、確率は2 ^ 32に1です)。

于 2012-08-04T17:22:21.080 に答える
0

エリックJの答えに対するあなたのコメントに基づいてあなたの質問に答えます:

J.no u got it wrong by i/p 10001 i mean for input[0]=1,input[1]=0 and so on... so it only occupies 5 array spaces

答えの要点は、最初に入力する入力を1として入力すると、条件が最初に成功するということです。後の各反復で、入力配列の値を変更し続けると、後続の反復が条件に入ります。

あなたが言ったようにあなたの入力は

input[0] = 1
input[1] = 0
input[2] = 0
input[3] = 0
input[4] = 1

次に、以下のコードの動作を確認してください。

for(j=0;j< build;j++)
{
    if(input[j]==1) /* First for input[0], this condition succeeds */
    {
         if(j==0)
         {
            input[j+1]=1; /* This line changes input[1] to 1, so next time in the loop the condition if(input[j] == 1 will succeed */
            printf("fddf");
         }
         else if(j==(build-1)) /* This condition will cause input[4] to be set to 1 and will cause if(input[j] == 1) to succeed for the last iteration of the loop */
         {
           input[j-1]=1;

           printf("Gfgf");
         }
        else /** This condition will cause input[2] & input[3] to be set to 1 and will again cause if(input[j] == 1) to succeed in successive loop iterations **/
        {
           input[j+1]=1;
           input[j-1]=1;
           printf("awGfgf");
        }
    }
  }
于 2012-08-04T19:16:23.307 に答える