亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

根據條件打印特定輸出

根據條件打印特定輸出

浮云間 2023-06-27 14:48:04
編寫一個程序,打印給定范圍內的數字。但是對于三的倍數打印“Fizz”而不是數字,對于五的倍數打印“Buzz”。對于同時是三和五的倍數的數字打印“FizzBuzz”。在每個字符串或數字后打印一個新行。輸入格式:- 第一行是測試用例的數量 T。下一行將有 T 個整數,用 N 表示。輸出格式:- 對于每個測試用例,打印從 1 到 N 的數字。但請遵循問題陳述中給出的規則。樣本輸入123 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")我知道我的錯誤是我必須根據初始輸入進行打印,但我不知道如何修復它。謝謝
查看完整描述

2 回答

?
森欄

TA貢獻1810條經驗 獲得超5個贊

k = int(input()) # useless given what we're doing below

cases = [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))

查看完整回答
反對 回復 2023-06-27
?
POPMUISE

TA貢獻1765條經驗 獲得超5個贊

Answer in C#: 

A first line is a number of case.

The second line is a value. 

Example : 

   Line1: 3

   Line2: 10,55,20

Example 2:

   Line1: 4

  Line2: 10,55,20, 44

代碼:


using System;

class sasikumarv{

public static void Main()

{

    var read = Console.ReadLine();

    int T= Convert.ToInt32(read);   

    var line = Console.ReadLine();

    var numbers = line.Split(' ');      

   if(first>=1 && first<=10)

    {

    

    for(int cnt=0;cnt<T;cnt++)

    {

     for(int i = 1; i <= Convert.ToInt32(numbers[cnt]); i++)                            

     {

        if (i % 15 == 0)

            Console.WriteLine("FizzBuzz" + " ");

        else if (i % 3 == 0)    

            Console.WriteLine("Fizz" + " ");

        else if (i % 5 == 0)                                            

            Console.WriteLine("Buzz" + " ");

        else

            Console.WriteLine(i + " ");                 

      }

    }

    }

}

}


查看完整回答
反對 回復 2023-06-27
  • 2 回答
  • 0 關注
  • 199 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號