根据条件打印特定输出

编写一个程序,打印给定范围内的数字。但是对于三的倍数打印“Fizz”而不是数字,对于五的倍数打印“Buzz”。对于同时是三和五的倍数的数字打印“FizzBuzz”。在每个字符串或数字后打印一个新行。


输入格式:- 第一行是测试用例的数量 T。下一行将有 T 个整数,用 N 表示。


输出格式:- 对于每个测试用例,打印从 1 到 N 的数字。但请遵循问题陈述中给出的规则。


样本输入1


2

3 15

这是我的代码:-


n_input = int(input())


x, y = map(int, input().split(" "))


for i in range(1, x + 1):

    if i % 3 == 0 and i % 5 == 0:

        print("FizzBuzz", sep="\n")

    elif i % 3 == 0:

        print("Fizz", sep="\n")

    elif i % 5 == 0:

        print("Buzz", sep="\n")

    else:

        print(i, sep="\n")



for i in range(1, y+1):

    if i % 3 == 0 and i % 5 == 0:

        print("FizzBuzz", sep="\n")

    elif i % 3 == 0:

        print("Fizz", sep="\n")

    elif i % 5 == 0:

        print("Buzz", sep="\n")

    else:

        print(i, sep="\n")

我知道我的错误是我必须根据初始输入进行打印,但我不知道如何修复它。谢谢


浮云间
浏览 158回答 2
2回答

森栏

k = int(input()) # useless given what we're doing belowcases = [int(i) for i in input().split()]for case in cases:    for k in range(1, case + 1):        out = ""        if (k % 3) == 0:            out += "fizz"        if (k % 5) == 0:            out += "buzz"        if out != "":            print(out)        else:            print(k)第二行是简写cases = []for i in input().split():    cases.append(int(i))

POPMUISE

Answer in C#:&nbsp;A first line is a number of case.The second line is a value.&nbsp;Example :&nbsp;&nbsp; &nbsp;Line1: 3&nbsp; &nbsp;Line2: 10,55,20Example 2:&nbsp; &nbsp;Line1: 4&nbsp; Line2: 10,55,20, 44代码:using System;class sasikumarv{public static void Main(){&nbsp; &nbsp; var read = Console.ReadLine();&nbsp; &nbsp; int T= Convert.ToInt32(read);&nbsp; &nbsp;&nbsp; &nbsp; var line = Console.ReadLine();&nbsp; &nbsp; var numbers = line.Split(' ');&nbsp; &nbsp; &nbsp;&nbsp;&nbsp; &nbsp;if(first>=1 && first<=10)&nbsp; &nbsp; {&nbsp; &nbsp;&nbsp;&nbsp; &nbsp; for(int cnt=0;cnt<T;cnt++)&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp;for(int i = 1; i <= Convert.ToInt32(numbers[cnt]); i++)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &nbsp;{&nbsp; &nbsp; &nbsp; &nbsp; if (i % 15 == 0)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Console.WriteLine("FizzBuzz" + " ");&nbsp; &nbsp; &nbsp; &nbsp; else if (i % 3 == 0)&nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Console.WriteLine("Fizz" + " ");&nbsp; &nbsp; &nbsp; &nbsp; else if (i % 5 == 0)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Console.WriteLine("Buzz" + " ");&nbsp; &nbsp; &nbsp; &nbsp; else&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Console.WriteLine(i + " ");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }&nbsp; &nbsp; }}}
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Python