天天看點

杭電oj2098分拆素數和&2099整除的尾數杭電oj2098分拆素數和&2099整除的尾數

杭電oj2098分拆素數和&2099整除的尾數

一、2098分拆素數和

題目描述:

Problem Description

把一個偶數拆成兩個不同素數的和,有幾種拆法呢?

Input

輸入包含一些正的偶數,其值不會超過10000,個數不會超過500,若遇0,則結束。

Output

對應每個偶數,輸出其拆成不同素數的個數,每個結果占一行。

Sample Input

30 26 0

Sample Output

3 2

思路:本題較簡單,主要是時間複雜度的問題,提高算法運作時間。在下面的代碼中,我寫了Isprime函數,用來判斷素數,從3開始判斷,因為1、2都已知,并且每次跳2兩個,提高代碼效率。

代碼:

import java.util.Scanner;
public class Main{
	public static boolean Isprime(int n){
		for(int i=3;i<=n/2;i+=2){
			if(n%i==0){
				return true;
			}
		}
		return false;
	}
	public static void main(String[] args) {
		Scanner in=new Scanner(System.in);
		while(in.hasNext()){
			int a=in.nextInt();
			if(a!=0){
				int count=0;
				int i=3;
			while(i<a/2){
				if(Isprime(i)==false){
					if(Isprime(a-i)==false)
					 count++;
				}
				i+=2;
			}
			System.out.println(count);
			}
			else{
				break;
			}
		}
		
	}
}
 
           

二、2099 整數的尾數

Problem Description 一個整數,隻知道前幾位,不知道末二位,被另一個整數除盡了,那麼該數的末二位該是什麼呢?   Input 輸入資料有若幹組,每組資料包含二個整數a,b(0<a<10000, 10<b<100),若遇到0 0則處理結束。   Output 對應每組資料,将滿足條件的所有尾數在一行内輸出,格式見樣本輸出。同組資料的輸出,其每個尾數之間空一格,行末沒有空格。   Sample Input 200 40 1992 95 0 0   Sample Output 00 40 80 15   思路:本題其實難度不大,要注意出錯的一點是輸出0的時候要輸出00。  

Java代碼:

import java.util.Scanner;
public class Main {
public static void main(String[] args) {
	Scanner in=new Scanner(System.in);
	while(in.hasNext()) {
		int a=in.nextInt();//輸入兩個數
		int b=in.nextInt();
		if(a==0&&b==0)
			break;
		int flag=0;
		for(int i=0;i<100;i++) {//作為尾數添加在a的後面
			if((a*100+i)%b==0) {
				if(flag==0) {
					System.out.printf("%02d",i);
					flag=1;
				}
				else {
					System.out.print(" ");
					System.out.printf("%02d",i);
				}
			}
		}
		System.out.println();
	}
}
}