写一个程序,输出从 1 到 n 数字的字符串表示。
1. 如果 n 是3的倍数,输出“Fizz”;
2. 如果 n 是5的倍数,输出“Buzz”;
3.如果 n 同时是3和5的倍数,输出 “FizzBuzz”。
示例:
n = 15, 返回: [ "1", "2", "Fizz", "4", "Buzz", "Fizz", "7", "8", "Fizz", "Buzz", "11", "Fizz", "13", "14", "FizzBuzz" ]
1 /**
2 * Return an array of size *returnSize.
3 * Note: The returned array must be malloced, assume caller calls free().
4 */
5 char** fizzBuzz(int n, int* returnSize) {
6 char string[] = "0123456789";
7 int i=0;
8 char tmp[25]={' '};
9 char ** p;
10 p=(char **)malloc(sizeof(char *) * n);
11 if(!p) return NULL;
12
13 //开辟空间 **p
14 for(i=0;i<n;i++){
15 p[i] = (char *) malloc(sizeof(char) * 9);
16 if(!p[i]) return NULL;
17 memset(p[i] , ' ',9);
18 }
19
20 for(i=1;i<=n;i++){
21 if(i%3 == 0 && i%5 == 0)
22 {memcpy(p[i-1], "FizzBuzz",strlen("FizzBuzz")); continue;}
23 else if(i%3 == 0){
24 memcpy(p[i-1],"Fizz",4);
25 continue;
26 }
27 else if(i%5 == 0){
28 memcpy(p[i-1],"Buzz",4);continue;
29 }
30 else{
31
32 //itoa(i,tmp,10);
33 int t=0,j=0,len;int k=i;
34 while(k != 0){
35 t = k%10;
36 tmp[j++] = string[t];
37 k = k/10;
38 }
39 //roll back
40 len =j;j=j-1;
41 while(k<len/2){
42 tmp[k] =tmp[k] ^ tmp[j];
43 tmp[j] =tmp[j] ^ tmp[k];
44 tmp[k] =tmp[k] ^ tmp[j];
45 k++;j--;
46 }
47 memcpy(p[i-1], tmp,strlen(tmp) );
48 }
49 }
50 //for(i=0;i<n;i++)
51 //printf("%s
",p[i]);
52 return p;
53 }