现在的位置: 首页 > 综合 > 正文

下图是一个英文等式,每个字母代表一个数字。求W代表哪个数。

2018年05月22日 ⁄ 综合 ⁄ 共 1889字 ⁄ 字号 评论关闭

程序一:

import java.util.Date;
import java.util.HashSet;
import java.util.Set;


public class WhatAShow {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		int count = 0;
		Set nums = null;
		long start = new Date().getTime();
		for(int w=1; w<=9; w++) {
			for(int h=0; h<=9; h++) {
				if(h != w) {
					for(int a=0; a<=9; a++) {
						if(a != w && a != h) {
							for(int t=0; t<=9; t++) {
								if(t != w && t != h && t != a) {
									for(int s=0; s<=9; s++) {
										if(s != w && s != h && s != a && s != t) {
											for(int o=0; o<=9; o++) {
												if(o != w && o != h && o != a && o != t && o != s) {
													nums = new HashSet();
													if(!nums.contains(w) && w != 0) {
														nums.add(w);
														if(!nums.contains(h)) {
															nums.add(h);
															if(!nums.contains(a)) {
																nums.add(a);
																if(!nums.contains(t)) {
																	nums.add(t);
																	if(!nums.contains(s)) {
																		nums.add(s);
																		if(!nums.contains(o)) {
																			nums.add(o);
																			if((1000*w + 100*h + 10*a + t) * a == (1000*s + 100*h + 10*o + w)) {
																				System.out.println("WHAT = " + w + h + a + t);
																				System.out.println("x  A =    " + a);
																				System.out.println("---------------");
																				System.out.println("SHOW = " + s + h + o + w);
																				
																				System.out.println("W代表:" + w);
																				count++;
																			}
																		}
																	}
																}
															}
														}
													}
												}
											}
										}
									}
								}
							}
						}
					}
				}
			}
		}
		long end = new Date().getTime();
		System.out.println("运行时间:" + (end - start));	//for循环的运行时间
		
		System.out.println("总数:" + count);
	}

}

运行结果:

WHAT = 4027
x       A =    2
---------------
SHOW = 8054
W代表:4
运行时间:94
总数:1

程序二(不用set做排重):

import java.util.Date;

public class WhatAShow {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		int count = 0;
		long start = new Date().getTime();
		for(int w=1; w<=9; w++) {
			for(int h=0; h<=9; h++) {
				if(h != w) {
					for(int a=0; a<=9; a++) {
						if(a != w && a != h) {
							for(int t=0; t<=9; t++) {
								if(t != w && t != h && t != a) {
									for(int s=0; s<=9; s++) {
										if(s != w && s != h && s != a && s != t) {
											for(int o=0; o<=9; o++) {
												if(o != w && o != h && o != a && o != t && o != s) {
													if((1000*w + 100*h + 10*a + t) * a == (1000*s + 100*h + 10*o + w)) {
														System.out.println("WHAT = " + w + h + a + t);
														System.out.println("x  A =    " + a);
														System.out.println("---------------");
														System.out.println("SHOW = " + s + h + o + w);
														
														System.out.println("W代表:" + w);
														count++;
													}
												}
											}
										}
									}
								}
							}
						}
					}
				}
			}
		}
		long end = new Date().getTime();
		System.out.println("运行时间:" + (end - start));	//for循环的运行时间
		
		System.out.println("总数:" + count);
	}

}

运行结果:

WHAT = 4027
x       A =    2
---------------
SHOW = 8054
W代表:4
运行时间:6
总数:1

速度快好几倍,期待更优化的算法

抱歉!评论已关闭.