【程序45】 題目:判斷一個素數能被幾個9整除

/*
	2017年3月13日10:28:21
	java基礎50道經典練習題 例45
	Athor: ZJY 
	Purpose:  
	【程序45】
	題目:判斷一個素數能被幾個9整除 

*/
import java.util.Scanner;

public class ProgramNo45_1
{
	public static void main(String[] args) {
		System.out.print("請輸入一個數: ");
		Scanner sc = new Scanner(System.in);
		long number = sc.nextLong();
		sc.close();
		
		int count = 0;
		long number_buf = number;
		while (number_buf >= 9) {
			number_buf /= 9;
			count++;
		}
		System.out.println(number+"最多能被"+count+"個9除");
	}
}
/*
	2017年3月13日10:28:21
	java基礎50道經典練習題 例45
	Athor: ZJY 
	Purpose:  
*/
import java.util.Scanner;
public class ProgramNo45_2
{
	public static void main(String[] args){
	  System.out.print("請輸入一個數:");
	  Scanner scan = new Scanner(System.in);
	  long l = scan.nextLong();
	  long n = l;
	  scan.close();
	  int count = 0;
	  while(n > 8){
		  n /= 9;
		  count++;
	  }
	  System.out.println(l+"能被"+count+"個9整除。");
	}
}



發佈了76 篇原創文章 · 獲贊 11 · 訪問量 13萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章