#2229: 小錯誤 但是不知道錯在哪 請大家幫忙


timmymike (超小小蝦米)


# include <iostream>
# include <string.h>

using namespace std ;

int main() {

  char answer[1000] = "\n" ;
  int num = 0, answerlen = 0 ;
  int quo = 1 ; // 商
  //原理:將input用2不斷除的餘數,倒著輸出為解答

  while( cin >> num ) {
   
    strcpy( answer, "" ) ; // answer清空
    quo = 1 ;
 for( int i = 0 ; quo != 0 ; i ++ ) { // 除到商數為零為止
      quo = num / 2 ;

      if ( num % 2 == 0 )
        answer[i] = '0' ;
      else if ( num % 2 == 1 )
        answer[i] = '1' ;
      else ;       
      num /= 2 ;

    } // for
    answerlen = strlen( answer ) ;

    for( int j = answerlen - 1 ; j != -1 ; j -- ) // 倒著印出(印到0)
      cout << answer[j] ;

  
  } // while


  return 0 ;

} // main()

 結果:

與正確輸出不相符(line:1)
您的答案為: 1110001100110110 ...略
正確答案為: 11100011001

 

#2234: Re:小錯誤 但是不知道錯在哪 請大家幫忙


timmymike (超小小蝦米)


部份程式碼修改,改成如下:

# include <iostream>
# include <string.h>

using namespace std ;

int main() {

  char answer[1000] = "" ;
  int num = 0, answerlen = 0 ;
  int quo = 1 ; // 商
  //原理:將input用2不斷除的餘數,倒著輸出為解答

  while( cin >> num ) {
   
    for( int k = 0 ; k < answerlen ; k ++ ) // answer清空
   answer[k] = NULL ; // answer[k] = '\0';

    quo = 1 ;
    for( int i = 0 ; quo != 0 ; i ++ ) { // 除到商數為零為止
      quo = num / 2 ;

      if ( num % 2 == 0 )
        answer[i] = '0' ;
      else if ( num % 2 == 1 )
        answer[i] = '1' ;
      else ;       
      num /= 2 ;


   
  } // for


    answerlen = strlen( answer ) ;

    for( int j = answerlen - 1 ; j != -1 ; j -- ) // 倒著印出(印到0)
      cout << answer[j] ;

  
  } // while


  return 0 ;

} // main()

請高手解惑 謝謝!

#2235: Re:小錯誤 但是不知道錯在哪 請大家幫忙


timmymike (超小小蝦米)


部份程式碼修改,改成如下:

# include <iostream>
# include <string.h>

using namespace std ;

int main() {

  char answer[1000] = "" ;
  int num = 0, answerlen = 0 ;
  int quo = 1 ; // 商
  //原理:將input用2不斷除的餘數,倒著輸出為解答

  while( cin >> num ) {
   
    for( int k = 0 ; k < answerlen ; k ++ ) // answer清空
   answer[k] = NULL ; // answer[k] = '\0';

    quo = 1 ;
    for( int i = 0 ; quo != 0 ; i ++ ) { // 除到商數為零為止
      quo = num / 2 ;

      if ( num % 2 == 0 )
        answer[i] = '0' ;
      else if ( num % 2 == 1 )
        answer[i] = '1' ;
      else ;       
      num /= 2 ;


   
  } // for


    answerlen = strlen( answer ) ;

    for( int j = answerlen - 1 ; j != -1 ; j -- ) // 倒著印出(印到0)
      cout << answer[j] ;

  
  } // while


  return 0 ;

} // main()

請高手解惑 謝謝!

#2236: Re:小錯誤 但是不知道錯在哪 請大家幫忙


timmymike (超小小蝦米)


# include
# include

using namespace std ;

int main() {

  char answer[1000] = "\n" ;
  int num = 0, answerlen = 0 ;
  int quo = 1 ; // 商
  //原理:將input用2不斷除的餘數,倒著輸出為解答

  while( cin >> num ) {
   
    strcpy( answer, "" ) ; // answer清空
    quo = 1 ;
 for( int i = 0 ; quo != 0 ; i ++ ) { // 除到商數為零為止
      quo = num / 2 ;

      if ( num % 2 == 0 )
        answer[i] = '0' ;
      else if ( num % 2 == 1 )
        answer[i] = '1' ;
      else ;       
      num /= 2 ;

    } // for
    answerlen = strlen( answer ) ;

    for( int j = answerlen - 1 ; j != -1 ; j -- ) // 倒著印出(印到0)
      cout << answer[j] ;

  
  } // while


  return 0 ;

} // main()

 結果:

與正確輸出不相符(line:1)
您的答案為: 1110001100110110 ...略
正確答案為: 11100011001

 


#2237: Re:小錯誤 但是不知道錯在哪 請大家幫忙


timmymike (超小小蝦米)


電腦怪怪的 送出很多次抱歉= = 

二樓是目前最新的

#3136: Re:小錯誤 但是不知道錯在哪 請大家幫忙


shinchun (shinchun)


電腦怪怪的 送出很多次抱歉= = 

二樓是目前最新的


你每次都要將字串再初始化

要不然你先輸入比較大的數字再輸入小的數字

小的數字MSB以前仍然是大的數字的資料

例如:先輸入8再輸入2

8:1000

2:10 ==>會變成1010

然後你又反轉 所以後面會有多一串不相關的東西 

#5932: Re:小錯誤 但是不知道錯在哪 請大家幫忙


Zein (TOXIC)


# include <iostream>
# include <string.h>

using namespace std ;

int main() {

  char answer[1000] = "" ;
  int num = 0, answerlen = 0 ;
  int quo = 1 ;
  while( cin >> num ) {
  
    for( int k = 0 ; k < answerlen ; k ++ )
   answer[k] = NULL ;

    quo = 1 ;
    for( int i = 0 ; quo != 0 ; i ++ ) {
      quo = num / 2 ;

      if ( num % 2 == 0 )
        answer[i] = '0' ;
      else if ( num % 2 == 1 )
        answer[i] = '1' ;
      else ;      
      num /= 2 ;


  
  }


    answerlen = strlen( answer ) ;

    for( int j = answerlen - 1 ; j != -1 ; j -- )
      cout << answer[j] ;

  cout<<"\n";
  }

  
  return 0 ;

}
你沒有換行吧

 



#5933: Re:小錯誤 但是不知道錯在哪 請大家幫忙


Zein (TOXIC)


# include <iostream>
# include <string.h>

using namespace std ;

int main() {

  char answer[1000] = "" ;
  int num = 0, answerlen = 0 ;
  int quo = 1 ;
  while( cin >> num ) {
  
    for( int k = 0 ; k < answerlen ; k ++ )
   answer[k] = NULL ;

    quo = 1 ;
    for( int i = 0 ; quo != 0 ; i ++ ) {
      quo = num / 2 ;

      if ( num % 2 == 0 )
        answer[i] = '0' ;
      else if ( num % 2 == 1 )
        answer[i] = '1' ;
      else ;      
      num /= 2 ;


  
  }


    answerlen = strlen( answer ) ;

    for( int j = answerlen - 1 ; j != -1 ; j -- )
      cout << answer[j] ;

  cout<<"\n";
  }

  
  return 0 ;

}
你沒有換行吧

 



#5934: Re:小錯誤 但是不知道錯在哪 請大家幫忙


Zein (TOXIC)


# include
# include

using namespace std ;

int main() {

  char answer[1000] = "\n" ;
  int num = 0, answerlen = 0 ;
  int quo = 1 ; // 商
  //原理:將input用2不斷除的餘數,倒著輸出為解答

  while( cin >> num ) {
   
    strcpy( answer, "" ) ; // answer清空
    quo = 1 ;
 for( int i = 0 ; quo != 0 ; i ++ ) { // 除到商數為零為止
      quo = num / 2 ;

      if ( num % 2 == 0 )
        answer[i] = '0' ;
      else if ( num % 2 == 1 )
        answer[i] = '1' ;
      else ;       
      num /= 2 ;

    } // for
    answerlen = strlen( answer ) ;

    for( int j = answerlen - 1 ; j != -1 ; j -- ) // 倒著印出(印到0)
      cout << answer[j] ;

  
  } // while


  return 0 ;

} // main()

 結果:

與正確輸出不相符(line:1)
您的答案為: 1110001100110110 ...略
正確答案為: 11100011001

# include <iostream>
# include <string.h>

using namespace std ;

int main() {

  char answer[1000] = "" ;
  int num = 0, answerlen = 0 ;
  int quo = 1 ;
  while( cin >> num ) {
   
    for( int k = 0 ; k < answerlen ; k ++ )
   answer[k] = NULL ;

    quo = 1 ;
    for( int i = 0 ; quo != 0 ; i ++ ) {
      quo = num / 2 ;

      if ( num % 2 == 0 )
        answer[i] = '0' ;
      else if ( num % 2 == 1 )
        answer[i] = '1' ;
      else ;       
      num /= 2 ;


   
  }


    answerlen = strlen( answer ) ;

    for( int j = answerlen - 1 ; j != -1 ; j -- )
      cout << answer[j] ;

  cout<<"\n";
  }

   
  return 0 ;

}

你沒換行