2009/07/03

fizzbuzz

#define X write(1,&F,1);write(1,&i,1);write(1,&z,1);write(1,&z,1);
#define Y write(1,&B,1);write(1,&u,1);write(1,&z,1);write(1,&z,1);
//Xはprintf("Fizz");、Yはprintf("Buzz");とほぼ同じ

char *strrev(char *s){//文字列を反転させる
char *ret = s;
char *t = s;
char c;
while( *t != '\ 0' )
t++;
t--;
while(t > s) {
c = *s;
*s = *t;
*t = c;
s++;
t--;
}
return ret;
}

void itoa(int value, char *buff){//itoa
char *p;
int mod;
if( 0 > value ) {
*buff++ = '-';
value = -value;
}
p = buff;
do{
mod = value % 10 + 48;
*p++ = (char)mod;
value /= 10;
}while(value);
*p = '\ 0';
strrev(buff);
}

int main(void){
int j;
int k;
char F='F';
char B='B';
char i='i';
char u='u';
char z='z';
char N=0x0A;//改行コード
char buf[16];
//こっからなぜか崩れる(´・ω・`)ショボーン
for(j=1;j<=100;j++){//1から100まで if(j%15==0){ X Y }else if(j%5==0){ Y }else if(j%3==0){ X }else{ itoa(j,buf); for(k=0;buf[k] != '\ 0';k++){ write(1,&buf[k],1); } } write(1,&N,1); } return 0; }

0 件のコメント: