| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 3333 人关注过本帖
标题:LeetCode 412 FizzBuzz
只看楼主 加入收藏
myishh
Rank: 1
等 级:新手上路
帖 子:2
专家分:0
注 册:2016-12-9
结帖率:0
收藏
已结贴  问题点数:20 回复次数:4 
LeetCode 412 FizzBuzz
https://

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”.
写一个程序,输出从1到n全部数字的字符串形式。但是,如果这个数字是3的倍数,则输出“Fizz”,而不要输出数字本身;同理,如果这个数字是5的倍数,则要输出“Buzz”;如果这个数字既是3的倍数也是5的倍数,则输出“FizzBuzz”。


I write code in C to slove this problem, but I get two troubles about this question.
1)I can not convert an integer into a string when the integer is greater than 9, for example, I know that char '9' can be print as 9+'0', which is its char expression of char '9', but I do not know how to express '15' in char/string.
2)Even the output is in integer, my code is shown to be output nothing after I submit my code to leetcode, while my code has the correct result in my own PC. I use DEV-C++(version 5.11)
我用C语言写的这个程序,但是遇到2个问题。
1)不知道咋把整数转换成字符串形式,尤其是当数字是两位数以上的时候。例如我知道整数9可以用9+'0'的形式输出,就是输出char类型的9,但是如果是数字15,我就不知道怎么转换成string形式了。
2)即便是输出整数形式,我的代码似乎也是不成功的,例如,对于输入n = 1, 我自己的电脑上显示成功输出了"1",但是把代码提交到Leetcode上去,就显示输出为空。我用Dec-C++写的代码,版本是5.11。

My code in C is shown below:
程序代码:
#include <stdio.h>
#include <stdlib.h>

int main (int argc, char* argv[]) {
    int num, i;
    scanf("%d", &num);                 
    for (i = 1; i <= num; i++) {
        if (i%3 == 0 && i%5 == 0){           //if num is the multiples of 3 and also the multiples of 5, print "FizzBuzz"
            printf("\"FizzBuzz\",\n");
        }
        else if (i%3 == 0) {
            printf("\"Fizz\",\n");           //if num is the multiples of 3, print "Fizz"
        }
        else if (i%5 == 0){
            printf("\"Buzz\",\n");           //if num is the multiples of 5, print "Buzz"
        }
        else {
            printf("\"%d\",\n", i);          //if nothing happens above, just print the integer itself out
        }    
    }
    return 0;
}


Thank you so much for pointing out my mistakes and thank you so so much for correcting them !!!!!

[此贴子已经被作者于2016-12-9 05:27编辑过]

搜索更多相关主题的帖子: should numbers instead 
2016-12-09 05:18
marlow
Rank: 6Rank: 6
等 级:侠之大者
威 望:2
帖 子:125
专家分:419
注 册:2016-7-18
收藏
得分:7 
用n%10提取数字n每一位上的字符,但当n可以被3,5,15整除时,输出相应的提示,另外你最后一个输出中格式控制符应为%c吧?

一切都在学习、尝试、摸索中
2016-12-09 06:26
吹水佬
Rank: 16Rank: 16Rank: 16Rank: 16
等 级:版主
威 望:451
帖 子:10551
专家分:42996
注 册:2014-5-20
收藏
得分:7 
数值转字符串
1、sprintf()
2、itoa(),是非标准C语言和C++语言扩展函数,通常在stdlib或cstdlib。
2016-12-09 07:15
rjsp
Rank: 20Rank: 20Rank: 20Rank: 20Rank: 20
等 级:版主
威 望:528
帖 子:9007
专家分:53942
注 册:2011-1-18
收藏
得分:7 
LeetCode上是这么写的
/**
 * Return an array of size *returnSize.
 * Note: The returned array must be malloced, assume caller calls free().
 */
char** fizzBuzz(int n, int* returnSize) {
   

}

2016-12-09 08:27
myishh
Rank: 1
等 级:新手上路
帖 子:2
专家分:0
注 册:2016-12-9
收藏
得分:0 
回复 4楼 rjsp
然后应该怎么办呢?
2016-12-09 10:12
快速回复:LeetCode 412 FizzBuzz
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.021721 second(s), 9 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved