google竞赛题SecretSum的C++解法
SecretSum 是本次 google 竞赛中第二轮淘汰赛的一道分值为 500 分竞赛题。事实上,这道题目反而比同轮比赛中的那道 1000 分值的RecurringNumbers 难(RecurringNumbers 的难度水准充其量不过是道初一学生奥数竞赛题)。好了,闲话少叙,来看 SecretSum 的题目吧: 一、竞赛题目 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. 题目的意思大致是这样的:数字0-9可以分别用A-Z中不相同的字母替代,这样就可以用字母组成的字符串代表一个数了。例如"ABA"可以代表: 101, 151, 343, 767, 929。但"ABA"不可以代表555,因为不同的字母必须取不同的数字。此外每个作为开头的字母不取0值。编写一个类 SecretSum,该类中有一个原型为 int countPossible(string num1, string num2, string result)的函数。num1、num2 和 result 就是上面所描述的类型的字符串,其中 result 代表的数为 num1 和 num2 代表的数的和,并且这几个字符串都只含有3个A-Z的字母。返回值为可能取得的组合的总数。 二、我的解题步骤. 2.1 解题框架 根据题意我们很容易看出解此题关键步骤有2个,第一是找出候选的3个数;第二判断传入的3个字符串是否可以代表这3个数 2.2 找出候选的3个数 由于第一个字母不会是0,所以三个字母所代表的数取值范围为100-999。同时由于result为num1与num2的和,当num1和num2确定后,result的 |
凌众科技专业提供服务器租用、服务器托管、企业邮局、虚拟主机等服务,公司网站:http://www.lingzhong.cn 为了给广大客户了解更多的技术信息,本技术文章收集来源于网络,凌众科技尊重文章作者的版权,如果有涉及你的版权有必要删除你的文章,请和我们联系。以上信息与文章正文是不可分割的一部分,如果您要转载本文章,请保留以上信息,谢谢! |