leetcode 412. Fizz Buzz(C语言)8
来源:互联网 发布:入库单打印软件 编辑:程序博客网 时间:2024/06/03 21:04
贴原题:
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 412. Fizz Buzz(C语言)8
- LeetCode 412. Fizz Buzz C语言
- LeetCode 412. Fizz Buzz 题解(C++)
- 412. Fizz Buzz(C语言)
- 412. Fizz Buzz(C++)
- LeetCode 412. Fizz Buzz
- [LeetCode]--412. Fizz Buzz
- LeetCode-412. Fizz Buzz
- LeetCode 412. Fizz Buzz
- 【leetcode】412. Fizz Buzz
- LeetCode 412. Fizz Buzz
- leetcode 412. Fizz Buzz
- leetcode 412. Fizz Buzz
- Leetcode 412.Fizz Buzz
- leetcode-412. Fizz Buzz
- LeetCode 412. Fizz Buzz
- 【LeetCode】 412. Fizz Buzz
- LeetCode 412. Fizz Buzz
- 第三周项目二
- Java序列化与反序列化
- APUE 第1-2章笔记
- android tv box launcher开发之添加自定义apk
- 第三周【项目4
- leetcode 412. Fizz Buzz(C语言)8
- 对象池:子弹的发射和子弹撞击后的回收
- PTP/IP协议
- 剑指offer-用两个栈实现队列
- 小论python的迭代器和生成器
- C#217课的主要内容
- LeetCode.125 Valid Palindrome
- java跳出多重循环
- 招聘需求高级AI工程师有意者联系博主