#14908: 真是怪事,請問這樣寫怎麼會有問題?


phjk (有時沒有)


這是程式內容:

 

#include <stdio.h>
#include <stdlib.h>
#include <math.h>


int main()
{
 char manywords[1000];
 int count=0;
 int ItTrue=1;
 int shift=0;


while( scanf("%s",manywords) != EOF){
//scanf("%s",manywords);

for(int i=0; (int)manywords[i] != 0 ;i++)   {
    count++;
}

for( shift= -32 ; shift < 3; shift++) {
        ItTrue = 1;
        for(int i=0; i<count;i++){
            if(    ( ( (int)manywords[i] + shift) <32 ) || (  ( (int)manywords[i] +shift )> 126)     )   {
                    ItTrue = 0;
                    i=count; // to break.
                }
            }
if(ItTrue)  {  break; }
}

for(int i=0;i < count  ;i++){
    printf("%c",   (char)  ( (int)manywords[i] + shift)   );
}
printf("\n");

}
}

在輸入迴圈內輸入第二次, shift 和 count 就會跑掉,我電腦上的和 zero judge 系統的都會。
是鬼月提前展開嗎?
 

 

#14910: Re:真是怪事,請問這樣寫怎麼會有問題?


who_am_I (kruztw)


你把 count = 0 寫在外面,當然每一次都不一樣阿

while(...){

    count = 0;

}

這樣才會每次都從 0 開始

 

#14913: Re:真是怪事,請問這樣寫怎麼會有問題?


phjk (有時沒有)




啊。太疏忽了。