# 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
部份程式碼修改,改成如下:
# 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()
請高手解惑 謝謝!
部份程式碼修改,改成如下:
# 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()
請高手解惑 謝謝!
# 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
# 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