利用遞迴可以邊算邊列印0、1字串。
C++(0.012)
/*******************************************************/
/* UVa 10931 Parity */
/* Author: Maplewing [at] knightzone.studio */
/* Version: 2015/01/26 */
/*******************************************************/
#include <iostream>
#include <cstdio>
using namespace std;
int calculateAndPrintMod2(int number){
if( number / 2 == 0 ){
if( number % 2 == 1 ){
printf("1");
}
return number % 2;
}
int mod = calculateAndPrintMod2(number / 2) + (number % 2);
printf("%d", number % 2);
return mod;
}
int main(){
int I;
while( scanf("%d", &I) != EOF && I != 0 ){
printf("The parity of ");
int mod = calculateAndPrintMod2(I);
printf(" is %d (mod 2).\n", mod);
}
return 0;
}