首页 > 代码库 > Timus 1104. Don’t Ask Woman about Her Age题解
Timus 1104. Don’t Ask Woman about Her Age题解
Mrs Little likes digits most of all. Every year she tries to make the best number of the year. She tries to become more and more intelligent and every year studies a new digit. And the number she makes is written in numeric system which base equals to her age. To make her life more beautiful she writes only numbers that are divisible by her age minus one. Mrs Little wants to hold her age in secret.
You are given a number consisting of digits 0, …, 9 and Latin letters A, …, Z, where A equals 10, B equals 11 etc. Your task is to find the minimal number k satisfying the following condition: the given number, written in k-based system is divisible by k?1.
Input
Input consists of one string containing no more than 106 digits or uppercase Latin letters.
Output
Output the only number k, or "No solution." if for all 2 ≤ k ≤ 36 condition written above can‘t be satisfied. By the way, you should write your answer in decimal system.
Sample
input | output |
---|---|
A1A | 22 |
本题主要考两个知识点:
1 能除尽k base数中k-1的数,必然各位加起来也能除尽k-1
2 一个数的base必然大于一个位的数(不能等于)
尤其是第一个知识点,不知道的话程序就会变得很复杂或者是甚至解不出来。
还有注意特例的时候:全零
int maxKNum(string s, int &k) { int sum = 0, a = 0; for (int i = 0; i < s.size(); i++) { if (s[i] >= ‘A‘) a = s[i] - ‘A‘ + 10; else a = s[i] - ‘0‘; k = max(k, a); sum += a; } return sum; } void DonAskWomanaboutHerAge1104() { string s; cin>>s; int k = 0; int sum = maxKNum(s, k); if (0 == k) k++;//特例:全零的时候 for ( ; k < 36; k++) { if (sum % (k) == 0) { cout<<k+1; return; } } cout<<"No solution."; }
Timus 1104. Don’t Ask Woman about Her Age题解
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。