412. Fizz Buzz

来源:互联网 发布:正版mac ps软件多少钱 编辑:程序博客网 时间:2024/05/16 00:11
/*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%  6mschar** 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;}
原创粉丝点击