/* Write a program that outputs the string representation of numbers from 1 to n. But for multiples of three it should output “Fizz” instead of the number and for the multiples of five output “Buzz”. For numbers which are multiples of both three and five output “FizzBuzz”. Example: n = 15, Return: [ "1", "2", "Fizz", "4", "Buzz", "Fizz", "7", "8", "Fizz", "Buzz", "11", "Fizz", "13", "14", "FizzBuzz" ] */ /** * Return an array of size *returnSize. * Note: The returned array must be malloced, assume caller calls free(). */ //本体题意就是说每次遇到三,都输出Fizz,每次遇到5,都输出Buzz。每次遇到3与5的倍数,输出FizzBuzz。 //11% 6ms char** fizzBuzz(int n, int* returnSize) { *returnSize = n;//返回数组的行数 int i; char** str; char buf[11]; str = (char**)malloc(sizeof(char*)*n); for( i=0; i <n; i++) { if((i+1)%3==0 && (i+1)%5==0 && i) sprintf(buf,"%s","FizzBuzz");//把第三个参数的值复制到第一个参数中 else if((i+1)%3==0) sprintf(buf,"%s","Fizz"); else if((i+1)%5==0) sprintf(buf,"%s","Buzz"); else sprintf(buf,"%d",i+1); str[i] = malloc(sizeof(buf)); memcpy(str[i],buf,strlen(buf)+1);//把第二个内存中的值赋值给第一个参数,赋值范围为第三个参数 memset(buf,"",11); } return str; }
412. Fizz Buzz
原创
©著作权归作者所有:来自51CTO博客作者jenslee的原创作品,请联系作者获取转载授权,否则将追究法律责任
提问和评论都可以,用心的回复会被更多人看到
评论
发布评论
相关文章
-
【快乐水题】412. Fizz Buzz
原题:力扣链接:412. Fizz Buzz题目简述:给你一个整数 n ,找出从 1 到 n 各个整数的 Fi
c语言 leetcode i++ 二维数组 二级指针