#include<iostream>
#include <stdio.h>
#include <stdlib.h>
using namespace std;
int main(){
int a,n[100];
while(scanf("%d",&a)!=EOF){
int i=0;
while((a!=1)&&(a!=0)){
n[i]=a%2;
a=(a-a%2)/2;
i++;
}
if(a==1){
n[i]=a;
}
i++;
while(i){
cout<<n[i-1];
i--;
}
cout<<endl;
}
}
cout
是 C++ 的,貼了一個純 C 的
#include <stdio.h> const unsigned MAXSIZE = 100; int main() { int num, size, ary[MAXSIZE]; while (scanf("%d", &num) != EOF) { size = 0; do { ary[size++] = num % 2; num /= 2; } while (num > 0); for (size--; size >= 0; size--) printf("%d", ary[size]); puts(""); } return 0; }
cout
是 C++ 的,貼了一個純 C 的
#include const unsigned MAXSIZE = 100; int main() { int num, size, ary[MAXSIZE]; while (scanf("%d", &num) != EOF) { size = 0; do { ary[size++] = num % 2; num /= 2; } while (num > 0); for (size--; size >= 0; size--) printf("%d", ary[size]); puts(""); } return 0; }
為甚麼size--是for的初始值