zoukankan      html  css  js  c++  java
  • ZOJ ACM 1314(JAVA)

    昨天做了几个题目。过于简单,就不在博客里面写了。

    1314这道题也比較简单,写出来是由于我认为在这里有一个小技巧,对于时间复杂度和空间复杂度都比較节省。

    这个题目类似哈希表的求解。可是更简单。刚拿到题目时,我想当然的希望将查询结果放到一个数组里面。然后遍历查询是否有一样的mod值。可是感觉这样肯定是最普遍的方法并且效率也不是太高。

    后来想到了其它的一些查询算法,比方二分,可是都感觉不太合适。

    直到我意识到这个过程和计算哈希表的过程类似,所以直接用mod值作为数组的下标索引。能够直接定位到当前值是否已经被计算过,从而高速推断是否一个好循环。

    public class Main {
    	
    	public static void main(String argv[]) {
    		int Step, Mod;
    		java.util.Scanner scanner = new java.util.Scanner(System.in);
    		while(scanner.hasNext()) {
    			String strLine = scanner.nextLine();
    			String strNums[] = strLine.split(" ");
    			Step = Integer.parseInt(strNums[0]);
    			Mod = Integer.parseInt(strNums[1]);
    			
    			int mods[] = new int[Mod];
    			mods[0] = 0;
    			int seed = 0;
    			String strResult = "Good Choice";
    			for(int i=1;i<Mod;i++) {
    				seed = (seed + Step) % Mod;
    				if(mods[seed] == 0 && seed != 0) {
    					mods[seed] = seed;
    				}
    				else {
    					strResult = "Bad Choice";
    					break;
    				}
    			}
    			System.out.format("%10s", Step);
    			System.out.format("%10s", Mod);
    			System.out.println("    "+strResult+"
    ");
    		}
    	}
    }
    


  • 相关阅读:
    URAL 2067 Friends and Berries (推理,数学)
    URAL 2070 Interesting Numbers (找规律)
    URAL 2073 Log Files (模拟)
    URAL 2069 Hard Rock (最短路)
    URAL 2068 Game of Nuts (博弈)
    URAL 2066 Simple Expression (水题,暴力)
    URAL 2065 Different Sums (找规律)
    UVa 1640 The Counting Problem (数学,区间计数)
    UVa 1630 Folding (区间DP)
    UVa 1629 Cake slicing (记忆化搜索)
  • 原文地址:https://www.cnblogs.com/yfceshi/p/7209324.html
Copyright © 2011-2022 走看看