思路
- c语言里没有整数转字符串的,需要手动写,而且要反转一次
- 注意i是从1开始的,数组是从0开始
代码
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
void exchange(char *s){
int i=0,j = strlen(s)-1;
while (i<j)
{
char temp ;
temp =s[i];
s[i] =s[j];
s[j] =temp;
i++;j--;
}
}
char * inttostring(int a){
char *r = (char*)malloc(32);
int i=0;
if(a<0){
r[i++] = '-';
}
while (a)
{
int temp = a%10;
r[i++] = temp+48;
a= a/10;
}
r[i] = '\0';
exchange(r);
return r;
}
char ** fizzBuzz(int n, int* returnSize){
char **re = (char **)malloc(sizeof(char*)*n);
int i;
for(i=0;i<n;i++)
re[i] = (char*)malloc(n);
for(i=1;i<=n;i++){
if(i%3==0&&i%5==0)
re[i-1] = "FizzBuzz";
else if(i%5==0)
re[i-1] = "Buzz";
else if(i%3==0)
re[i-1] = "Fizz";
else
re[i-1]=inttostring(i);
}
*returnSize = n;
return re;
}
int main(){
int a = 15;
int size = 0;
int i;
char ** re = fizzBuzz(a,&size);
for(i=0;i<size;i++)
printf("%s\n",re[i]);
}
|