Problem 33

问题描述:

The fraction 49/98 is a curious fraction, as an inexperienced mathematician in attempting to simplify it may incorrectly believe that 49/98 = 4/8, which is correct, is obtained by cancelling the 9s.

We shall consider fractions like, 30/50 = 3/5, to be trivial examples.

There are exactly four non-trivial examples of this type of fraction, less than one in value, and containing two digits in the numerator and denominator.

If the product of these four fractions is given in its lowest common terms, find the value of the denominator.

解决问题:

public static int find(){
		int result =0;
		int[][] numbers = new int[4][2];
		
		int fenzi = 11;
		int index = 0;
		for(fenzi=11; fenzi<100&&index<4; fenzi++){
			if(fenzi%10==0){
				continue;
			}else{
				for(int fenmu = fenzi+1; fenmu<100&&index!=4; fenmu++){
					int[] element = new int[4];
					element[0] = fenzi/10;
					element[1] = fenzi%10;
					element[2] = fenmu/10;
					element[3] = fenmu%10;
					//System.out.println(fenzi+":"+fenmu);
					if(element[0]==element[2]){
						if(fenzi*element[3]==fenmu*element[1]){
							numbers[index][0] = element[1];
							numbers[index][1] = element[3];
							index++;
							break;
						}
					}
					if(element[0]==element[3]){
						if(fenzi*element[2]==fenmu*element[1]){
							numbers[index][0] = element[1];
							numbers[index][1] = element[2];
							index++;
							break;
						}
					}
					if(element[1]==element[2]){
						if(fenzi*element[3]==fenmu*element[0]){
							numbers[index][0] = element[0];
							numbers[index][1] = element[3];
							index++;
							break;
						}
					}
					if(element[1]==element[3]){
						if(fenzi*element[2]==fenmu*element[0]){
							numbers[index][0] = element[0];
							numbers[index][1] = element[2];
							index++;
							break;
						}
					}
				}
			}
		}
		
		fenzi = 1;
		int fenmu = 1;
		for(int i=0; i<numbers.length; i++){
			fenzi *= numbers[i][0];
			fenmu *= numbers[i][1];
		}
		
		if(fenmu%fenzi==0){
			result = fenmu/fenzi;
		}else{
			
		}
		
		return result;
	}

猜你喜欢

转载自to-zoe-yang.iteye.com/blog/1151391
33
今日推荐