Project Euler 054~056

关于啥是Project Euler 详见 https://projecteuler.net/about
观前声明:
这是个人兴趣使然的对自己入坑pe的记录,仅提供思路和部分代码;各个方面肯定都是有着优化与提升空间的,甚至在许多大佬看来这应该是初学者的浅薄而未经剪枝的丑码,如果能帮到有兴趣的人自是最好,也欢迎能醍醐灌顶的深度讨论。
大佬看到了笑笑就行,还请轻喷。
带着恶意,抬杠者...俺也喷不过您,也不能拿您咋样...毕竟这只是我个人兴趣使然的行为或者说是学习记录分享。 (说是记录,但因为是早先写的所以其实是在各种意义上公开处刑和吐槽自己 并尝试补救优化)
语言是c++,用的VS平台
(都忘记上次更新是啥时了...)

Poker hands
Problem 54
In the card game poker, a hand consists of five cards and are ranked, from lowest to highest, in the following way:
High Card: Highest value card.
One Pair: Two cards of the same value.
Two Pairs: Two different pairs.
Three of a Kind: Three cards of the same value.
Straight: All cards are consecutive values.
Flush: All cards of the same suit.
Full House: Three of a kind and a pair.
Four of a Kind: Four cards of the same value.
Straight Flush: All cards are consecutive values of same suit.
Royal Flush: Ten, Jack, Queen, King, Ace, in same suit.
The cards are valued in the order:
2, 3, 4, 5, 6, 7, 8, 9, 10, Jack, Queen, King, Ace.
If two players have the same ranked hands then the rank made up of the highest value wins; for example, a pair of eights beats a pair of fives (see example 1 below). But if two ranks tie, for example, both players have a pair of queens, then highest cards in each hand are compared (see example 4 below); if the highest cards tie then the next highest cards are compared, and so on.
Consider the following five hands dealt to two players:

The file, poker.txt, contains one-thousand random hands dealt to two players. Each line of the file contains ten cards (separated by a single space): the first five are Player 1's cards and the last five are Player 2's cards. You can assume that all hands are valid (no invalid characters or repeated cards), each player's hand is in no specific order, and in each hand there is a clear winner.
How many hands does Player 1 win?
题目大意:
在扑克游戏中,一局牌由五张牌组成,组成的牌的大小由低到高如下:
High Card: 最高值的牌.
One Pair: 两张面值一样的牌.
Two Pairs: 两个值不同的One Pair.
Three of a Kind: 三张面值一样的牌.
Straight: 所有的牌面值为连续数值.
Flush: 所有的牌花色相同.
Full House: Three of a Kind 加一个One Pair.
Four of a Kind: 四张牌面值相同.
Straight Flush: 所有的牌花色相同并且为连续数值.
Royal Flush: 10,J,Q,K和A,并且为相同花色。
牌的面值大小排序如下:
2, 3, 4, 5, 6, 7, 8, 9, 10, Jack, Queen, King, Ace.
如果两个玩家的牌具有同样的排序(上面介绍的几种),那么他们牌的大小由手中最大的牌决定。
例如,一对8比一对5大(见下面例一);
但是如果两个玩家都用一对Q,那么他们手中最大的牌就用来比较大小(见下面例四);
如果他们最高面值的牌也相等,那么就用次高面值的牌比较,以此类推。
(例子见英文)
文件 poker.txt (https://projecteuler.net/project/resources/p054_poker.txt)包含一千局随机牌。
每一行包含十张牌(用空格分隔);前五张是玩家1的牌,后五张是玩家2的牌。
所有的牌都是合理的(没有非法字符或者重复的牌)。每个玩家的牌没有顺序,并且每一局都有明确的输赢。
其中玩家1能赢多少局?
这是当时我做到的最恶心的题 没有之一
题本身不难 仅仅只用考虑清楚判断条件而已
但是,但是..要考虑完所有情况,就很难受了 我记得我写这题的时候非常酸爽,直到现在都不愿再面对.纯属恶心人,我写的这些个判断没有任何技巧 至今也不想优化.
分为2个函数.checklevel函数判断手牌等级,judge函数仲裁2者输赢,需要细写的基本就是手牌同级后的其他判断。
可以在全局先用string数组存储好1000对手牌 然后慢慢写
实在不想细说这个我直接贴代码了 毕竟真的没啥技巧.

简单说明下:
这里先用sx sy数组分别存储手牌的数字和字母部分,便于之后的判断;
scv工具字符串为判断面值和数相同面值的牌的个数提供方便;
其他部分我代码上//后都解释了是啥等级我就懒得说了(节能);

基本上也没啥好解释的 这个函数前面几行与checklevel函数是一样的,后面都在考虑不同等级的2对手牌等级相等的情况下进行别的比较(写了300+行 wsl)
这道题不论是题还是我(主要是我)都显得很笨拙.建议入土
ans:376

Lychrel numbers
Problem 55
If we take 47, reverse and add, 47 + 74 = 121, which is palindromic.
Not all numbers produce palindromes so quickly. For example,
349 + 943 = 1292,
1292 + 2921 = 4213
4213 + 3124 = 7337
That is, 349 took three iterations to arrive at a palindrome.
Although no one has proved it yet, it is thought that some numbers, like 196, never produce a palindrome. A number that never forms a palindrome through the reverse and add process is called a Lychrel number. Due to the theoretical nature of these numbers, and for the purpose of this problem, we shall assume that a number is Lychrel until proven otherwise. In addition you are given that for every number below ten-thousand, it will either (i) become a palindrome in less than fifty iterations, or, (ii) no one, with all the computing power that exists, has managed so far to map it to a palindrome. In fact, 10677 is the first number to be shown to require over fifty iterations before producing a palindrome: 4668731596684224866951378664 (53 iterations, 28-digits).
Surprisingly, there are palindromic numbers that are themselves Lychrel numbers; the first example is 4994.
How many Lychrel numbers are there below ten-thousand?
NOTE: Wording was modified slightly on 24 April 2007 to emphasise the theoretical nature of Lychrel numbers.
本题新知识:利克瑞尔数..
47与自己的倒置74相加得到121是个回文数;
349需要经过3次这样的迭代才能得到回文数:
349 + 943 = 1292,
1292 + 2921 = 4213
4213 + 3124 = 7337
不能形成回文数的数被称为利克瑞尔数(Lychrel numbers)
此外我们还知道:
对于每个一万以下的数字,这个数如果不能在50次迭代以内得到一个回文,那么就算用尽现有的所有运算能力也永远不会得到。10677是第一个需要50次以上迭代得到回文的数,它可以通过53次迭代得到一个28位的回文:4668731596684224866951378664。
令人惊奇的是,有一些回文数本身也是Lychrel数,第一个例子是4994。
问10000以下有多少Lychrel数
当时刚写完54,所以55就自暴自弃的暴搜了 (经典“规模不大,BF也很快”)
基本就是利用大整数的加法,写几个函数:判断数是否是回文数;得到数的逆序;将数本身与逆序相加得到新的数并存进数组;循环上述判断50次迭代内这个数能不能形成回文数
对10000以下每个数进行检验(可以通过一些数论知识进行剪枝优化. 但 我懒)
直接上代码了:

没啥技巧可言的暴搜.
ans:249

Powerful digit sum
Problem 56
A googol (10^100) is a massive number: one followed by one-hundred zeros; 100^100 is almost unimaginably large: one followed by two-hundred zeros. Despite their size, the sum of the digits in each number is only 1.
Considering natural numbers of the form, a,b, where a, b < 100, what is the maximum digital sum?
10^100,1后带100个0,100^100,1后带200个0,它们的每个位上的数相加后只有1
问当a,b<100时,所有a^b中,每个位上的数相加后,最大值是多少。
大整数乘法回忆复健题.搬搬以前的砖即可。
(因为写过大整数乘法,所以就懒得用更为优化的快速幂一类.

while循环内即为大整数乘法;(这个函数的时间复杂度毫无优化,因为每次指数运算中每次都要进行o^(n)的大整数乘法运算)但无所谓 a b<100时还是很快。
ans:972

随缘更新后续
暑假余额一月不到.哭.