最後更新於 2021 年 6 月 16 日
題目原文:https://onlinejudge.org/index.php?option=onlinejudge&Itemid=8&page=show_problem&problem=1131
題目概要:判斷a是否為b的n次方,如果是則輸出a/b的所有結果,否則輸出Boring。
Sample Input
125 5
30 3
80 2
81 3
Sample Output
125 25 5 1
Boring!
Boring!
81 27 9 3 1
程式碼
import java.util.*; public class main{ public static void main(String[] args) { Scanner sc=new Scanner(System.in); while(sc.hasNext()){ int a = sc.nextInt(); int b = sc.nextInt(); int tot=1; while(tot<a && b>=2){ //用於判斷a是否為b的n次方 tot *= b; } if(tot==a && b>=2 && a>=b){ for(long i=a;i>=1;i/=b){ //將a除以b直至1的所有結果列出來 System.out.print(i); if(i!=1) System.out.print(" "); //若非1 則每個結果中間空一格 } System.out.println(); }else{ System.out.println("Boring!"); //若a不是b的n次方 或 b<2 或 a<b 則輸出Boring } } } };