412. Fizz Buzz

来源:互联网 发布:python 渗透脚本 编辑:程序博客网 时间:2024/05/06 11:30


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) {    char ** t;    int i,m=16;    t=(char**)malloc(n*sizeof(char*));    for(i=0;i<n;i++)    {        t[i]=(char*)malloc(m*sizeof(char));    }    for(i=0;i<n;i++)    {        if((i+1)%15==0)        *(t+i)="FizzBuzz";        else if((i+1)%3==0)        *(t+i)="Fizz";        else if((i+1)%5==0)        *(t+i)="Buzz";        else        sprintf(*(t+i),"%d",i+1);    }    *returnSize=n;    return t;}

0 0
原创粉丝点击