leetcode 412. Fizz Buzz(C语言)8
2017-09-20 20:12
323 查看
贴原题:
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” ]
代码:
笔记:(1、2摘自百度百科)
1. sprintf 功能:把格式化的数据写入某个字符串中;
2. void *memcpy(void *dest, const void *src, size_t n); 功能:从源src所指的内存地址的起始位置开始拷贝n个字节到目标dest所指的内存地址的起始位置中。
4. 要注意的就是:1,返回值类型为char** 及char*的 区别。2,指针数组和数组指针的区别。
5. 我目前所理解的是,char**的返回值类型是一个指针数组,而char*的返回值类型是一个数组。
6. 指针数组和数组指针的区别大概是,指针数组是一个数组里的元素都是指针,数组指针是指向数组的指针。
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(). */ char** fizzBuzz(int n, int* returnSize) { *returnSize=n; char buff[10];//用来存放"Fizz"、"Buzz"、"FizzBuzz"以及1246啥的;长度需大于"FizzBuzz"的长度+1 char **s=(char **)malloc(n*sizeof(char *));//数组指针 for(int i=1; i<=n; i++) { if(i%15==0)//15的倍数 { sprintf(buff, "%s", "FizzBuzz");//把"FizzBuzz"存在buff } else if(i%3==0)//3的倍数 { sprintf(buff, "%s", "Fizz"); } else if(i%5==0)//5的倍数 { sprintf(buff, "%s", "Buzz"); } else//其他 { sprintf(buff, "%d", i); } *(s+i-1)=malloc(sizeof(buff));//为s的该位分配需要存放的空间 memcpy(*(s+i-1), buff, strlen(buff)+1);//把buff存放的数据拷贝到s数组 memset(buff, ' ', 11);//把buff清空,方便下次使用 } return s; }
笔记:(1、2摘自百度百科)
1. sprintf 功能:把格式化的数据写入某个字符串中;
2. void *memcpy(void *dest, const void *src, size_t n); 功能:从源src所指的内存地址的起始位置开始拷贝n个字节到目标dest所指的内存地址的起始位置中。
4. 要注意的就是:1,返回值类型为char** 及char*的 区别。2,指针数组和数组指针的区别。
5. 我目前所理解的是,char**的返回值类型是一个指针数组,而char*的返回值类型是一个数组。
6. 指针数组和数组指针的区别大概是,指针数组是一个数组里的元素都是指针,数组指针是指向数组的指针。
相关文章推荐
- [leetcode-236]Lowest Common Ancestor of a Binary Tree(C语言)236
- LeetCode Remove Duplicates from Sorted Array(C语言O(n)解法)
- LeetCode-55-Jump Game(C语言实现)
- LeetCode 412. Fizz Buzz
- leetcode 500. Keyboard Row(C语言,判断单词是否在键盘同一行,多级指针)22
- leetcode-412. Fizz Buzz
- Leetcode 2.Add Two Numbers的c语言解决
- [leetcode]Symmetric Tree (对称树 C语言实现)
- [C语言][LeetCode][20]Valid Parentheses
- [leetcode]Remove Duplicates from Sorted List (删除有序节点的值重复的节点 C语言)
- [leetcode]-507. Perfect Number(C语言)
- leetcode 467 c语言. Unique Substrings in Wraparound String
- [C语言][LeetCode][169]Majority Element
- leetcode 338. Counting Bits(C语言,二进制中1的个数)39
- LeetCode-14-Longest Common Prefix(C语言实现)
- leetcode 349. Intersection of Two Arrays(C语言)10
- [LeetCode练习题-C语言]203. Remove Linked List Elements
- LeetCode38 Count and Say(C语言解决)
- leetcode题目: Sort Characters By Frequency 的C语言解法
- leetcode 412. Fizz Buzz