emu in blogjava

            BlogJava :: 首頁 :: 新隨筆 :: 聯系 :: 聚合  :: 管理 ::
            171 隨筆 :: 103 文章 :: 1052 評論 :: 2 Trackbacks

          Problem Statement

              

          We can substitute each digit of a number with a unique letter from 'A' to 'Z'. This way we can write groups of numbers in a single representation. For example "ABA" can represent any of the following: 101, 151, 343, 767, 929. However, "ABA" cannot mean 555 because each letter must stand for a distinct digit. Furthermore, numbers cannot begin with 0 and thus 'A' cannot be replaced by 0.

          Given two such representations num1 and num2 and the result of their summation return the total number of possible combinations of numbers for which the equation holds. If no combinations are possible then return 0.

          Definition

              
          Class: SecretSum
          Method: countPossible
          Parameters: String, String, String
          Returns: int
          Method signature: int countPossible(String num1, String num2, String result)
          (be sure your method is public)
              

          Constraints

          - num1, num2, and result will each contain exactly 3 uppercase letters ('A' - 'Z').

          Examples

          0)
              
          "AAA"
          "BBB"
          "CCC"
          Returns: 32
          1)
              
          "ABB"
          "DEE"
          "TTT"
          Returns: 112
          2)
              
          "ABC"
          "ABA"
          "ACC"
          Returns: 0
          Leading zeroes are not allowed.
          3)
              
          "AAA"
          "CDD"
          "BAA"
          Returns: 32
          4)
              
          "TEF"
          "FET"
          "AAA"
          Returns: 12
          5)
              
          "ABC"
          "ABC"
          "BCE"
          Returns: 5
          We can have the following 5 sums:
          124 + 124 = 248
          125 + 125 = 250
          249 + 249 = 498
          374 + 374 = 748
          375 + 375 = 750
          6)
              
          "AAA"
          "AAA"
          "BBB"
          Returns: 4
          We can have the following 4 sums:
          111 + 111 = 222
          222 + 222 = 444
          333 + 333 = 666
          444 + 444 = 888

          This problem statement is the exclusive and proprietary property of TopCoder, Inc. Any unauthorized use or reproduction of this information without the prior written consent of TopCoder, Inc. is strictly prohibited. (c)2003, TopCoder, Inc. All rights reserved.

          posted on 2005-12-23 10:28 emu 閱讀(2260) 評論(6)  編輯  收藏 所屬分類: google編程大賽模擬題及入圍賽真題

          評論

          # re: SecretSum (code jam china round2 500分真題) 2005-12-23 15:15 Tendy
          我的解法。
          用的是笨笨的窮舉法~~
          ps: Java的String 不能用[],真不方便~~~
          public class SecretSum
          {
          public int countPossible (String num1, String num2, String result)
          {
          int total = 0;
          String s = num1 + num2 + result;
          int i;
          String uniqueChars = "";
          char ch;
          for (i = 0; i < s.length(); ++i)
          {
          ch = s.charAt(i);
          if (uniqueChars.indexOf(ch) < 0)
          uniqueChars += ch;
          }

          if (uniqueChars.length() > 10) // 10個以上不同的字母,出錯
          return 0;

          int maxTotal = 1; // 窮舉次數
          for (i = 0; i < uniqueChars.length(); ++i)
          maxTotal *= 10;

          int numberMinValue = 1; // 每個數字最小值(用于限制最高位不能為0)
          for (i = 1; i < num1.length(); ++i)
          numberMinValue *= 10;

          String temp; // 臨時變量
          String s1, s2, s3; // 分別對應num1, num2, result,用于將字母替換成數字
          int n1, n2, n3; // 替換后數字
          boolean validNumber;
          // 窮舉
          for (int counter = maxTotal / 10; counter < maxTotal; ++counter)
          {
          temp = String.valueOf(counter);
          if (temp.length() < uniqueChars.length())
          continue;

          validNumber = true;
          for (i = 0; i < temp.length(); ++i)
          {
          if (temp.lastIndexOf(temp.charAt(i)) != i) // 計算每個數字是否只出現一次
          validNumber = false;
          }

          if (validNumber)
          {
          s1 = num1;
          s2 = num2;
          s3 = result;
          for (i = 0; i < temp.length(); ++i)
          {
          s1 = s1.replace(uniqueChars.charAt(i), temp.charAt(i));
          s2 = s2.replace(uniqueChars.charAt(i), temp.charAt(i));
          s3 = s3.replace(uniqueChars.charAt(i), temp.charAt(i));
          }
          n1 = Integer.valueOf(s1);
          n2 = Integer.valueOf(s2);
          n3 = Integer.valueOf(s3);
          if (n1 >= numberMinValue && n2 >= numberMinValue && n1 + n2 == n3)
          ++total; // found 1
          }
          }
          return total;
          }

          public static void main(String[] args)
          {
          SecretSum ss = new SecretSum();
          System.out.println(ss.countPossible("AAA", "BBB", "CCC"));
          System.out.println(ss.countPossible("ABB", "DEE", "TTT"));
          System.out.println(ss.countPossible("ABC", "ABA", "ACC"));
          System.out.println(ss.countPossible("AAA", "CDD", "BAA"));
          System.out.println(ss.countPossible("TEF", "FET", "AAA"));
          System.out.println(ss.countPossible("ABC", "ABC", "BCE"));
          System.out.println(ss.countPossible("AAA", "AAA", "BBB"));
          }
          }  回復  更多評論
            

          # re: SecretSum (code jam china round2 500分真題) 2005-12-23 15:17 Tendy
          if (temp.lastIndexOf(temp.charAt(i)) != i) validNumber = false;
          這句忘了加個 break;
          不影響結果,不過效率高點點..  回復  更多評論
            

          # re: SecretSum (code jam china round2 500分真題) 2006-01-13 16:48 akang
          很不錯的算法,樓主厲害啊  回復  更多評論
            

          # re: SecretSum (code jam china round2 500分真題) 2006-07-21 22:09 清風劍
          如果有更多朋友寫答案上來就更好了。  回復  更多評論
            

          # re: SecretSum (code jam china round2 500分真題) 2006-07-25 23:54 yuyou
          @Tendy
            回復  更多評論
            

          # re: SecretSum (code jam china round2 500分真題) 2009-07-24 12:37 Jamers
          "AAA"
          "CDD"
          "BAA"

          Returns: 32

          我編的算法,做出來怎 么是31?指點下…看看有什么疏忽…
          111+300=411
          111+400=511
          111+500=611
          111+600=711
          111+700=811
          111+800=911
          222+100=322
          222+300=522
          222+400=622
          222+500=722
          222+600=822
          222+700=922
          333+100=433
          333+200=533
          333+400=733
          333+500=833
          333+600=933
          444+100=544
          444+200=644
          444+300=744
          444+500=944
          555+100=655
          555+200=755
          555+300=855
          555+400=955
          666+100=766
          666+200=866
          666+300=966
          777+100=877
          777+200=977
          888+100=988
            回復  更多評論
            

          主站蜘蛛池模板: 岑溪市| 江源县| 昌江| 万载县| 武穴市| 富阳市| 克拉玛依市| 革吉县| 盘锦市| 永泰县| 华蓥市| 临城县| 舟曲县| 大洼县| 沁水县| 陵川县| 宿迁市| 沛县| 铜山县| 长宁区| 东兴市| 浮梁县| 望奎县| 班玛县| 日照市| 来安县| 韩城市| 嘉定区| 岑巩县| 丰县| 巫溪县| 新田县| 万山特区| 扎赉特旗| 宜良县| 临西县| 阿巴嘎旗| 崇左市| 科尔| 高阳县| 屏东县|