• FizzBuzz


    写一个程序,输出从 1 到 n 数字的字符串表示。

    1. 如果 是3的倍数,输出“Fizz”;

    2. 如果 是5的倍数,输出“Buzz”;

    3.如果 同时是3和5的倍数,输出 “FizzBuzz”。

    示例:

    n = 15,
    
    返回:
    [
        "1",
        "2",
        "Fizz",
        "4",
        "Buzz",
        "Fizz",
        "7",
        "8",
        "Fizz",
        "Buzz",
        "11",
        "Fizz",
        "13",
        "14",
        "FizzBuzz"
    ]

     1 /**
     2  * Return an array of size *returnSize.
     3  * Note: The returned array must be malloced, assume caller calls free().
     4  */
     5 char** fizzBuzz(int n, int* returnSize) {
     6     char  string[] = "0123456789";
     7     int i=0;
     8     char tmp[25]={''};
     9     char ** p;
    10     p=(char **)malloc(sizeof(char *) * n);
    11     if(!p) return NULL;
    12 
    13     //开辟空间  **p
    14     for(i=0;i<n;i++){
    15         p[i] = (char *) malloc(sizeof(char) * 9);
    16         if(!p[i]) return NULL;
    17         memset(p[i] , '',9);
    18     }
    19 
    20     for(i=1;i<=n;i++){
    21         if(i%3 == 0 && i%5 == 0)
    22         {memcpy(p[i-1], "FizzBuzz",strlen("FizzBuzz")); continue;}
    23         else if(i%3 == 0){
    24             memcpy(p[i-1],"Fizz",4);
    25             continue;
    26         }
    27         else if(i%5 == 0){ 
    28             memcpy(p[i-1],"Buzz",4);continue;
    29         }
    30         else{
    31             
    32             //itoa(i,tmp,10);
    33             int t=0,j=0,len;int k=i;
    34             while(k != 0){
    35                 t = k%10;
    36                 tmp[j++] = string[t];
    37                 k = k/10;
    38             }
    39             //roll back
    40             len =j;j=j-1;
    41             while(k<len/2){
    42                 tmp[k] =tmp[k] ^ tmp[j];
    43                 tmp[j] =tmp[j] ^ tmp[k];
    44                 tmp[k] =tmp[k] ^ tmp[j];
    45                 k++;j--;
    46             }
    47             memcpy(p[i-1], tmp,strlen(tmp) );
    48         }    
    49     }
    50     //for(i=0;i<n;i++)
    51         //printf("%s
    ",p[i]);
    52     return p;
    53 }
  • 相关阅读:
    spring cloud项目搭建
    获取iframe的window对象
    数学杂谈 #7
    [AGC023D] Go Home
    JOISC 2021 部分题解
    [NOI2017]泳池
    [NOI2016] 循环之美
    [NOI2016] 优秀的拆分
    [LG P3676]小清新数据结构题
    [ARC113F]Social Distance
  • 原文地址:https://www.cnblogs.com/micoblog/p/9313794.html
Copyright © 2020-2023  润新知