我遇到了问题。我正在学习c ++。我拿了一个扑克背景; 目标是从字符串长度获得某个手牌范围;该字符串表示扑克游戏中的所有起手牌,顺序较强。
string hand = "AA,KK,QQ,JJ,AKs,AKo,AQs,AQo,TT,AJs,ATs,AJo,KQs,KJs,KTs,QJs,ATo,QTs,JTs,A9s,A9o,KQo,A8s,A8o,A7s,A7o,A6s,A6o,A5s,A5o,A4s,99,A4o,A3s,A2s,KJo,J9s,T9s,Q9s,QJo,KTo,Q9o,88,77,66,QTo,A3o,A2o,JTo,K9s,K8s,K7s,K6s,K5s,K4s,K3s,K2s,Q8s,Q7s,Q6s,Q5s,K9o,J8s,T8s,98s,97s,87s,86s,76s,96s,75s,65s,64s,J9o,T9o,55,54s,53s,52s,K8o,43s,32s,42s,J7s,T7s,K7o,44,33,22,Q4s,Q3s,Q2s,J6s,J5s,T6s,T5s,J4s,K6o,Q8o,J8o,T8o,98o,97o,87o,85s,K5o,K4o,K3o,K2o,95s,74s,76o,65o,54o,84s,94s,Q7o,J7o,T7o,Q6o,J3s,T4s,J2s,Q5o,T3s,T2s,Q4o,J6o,86o,T6o,96o,93s,Q3o,74o,84o,75o,64o,T2o,94o,53o,93o,63o,43o,92o,73o,83o,52o,82o,42o,62o,72o,J5o,63s,92s,73s,Q2o,J4o,83s,85o,82s,T5o,95o,J3o,62s,T4o,J2o,72s,T3o,32o";
字符串手的长度为662 ;要获得范围,我们需要排除起手牌的百分比并排除结束牌的百分比。
因此,如果我需要最后10%的牌,我将排除90%。 如果我需要30%到60%之间,我将排除前29%和39%。
这很困难,因为字符串不能在任何地方剪切,好的输出应该是例如72o,J5o,63s,92s,73s,Q2o,J4o,83s,
有或没有逗号到最后,这不是最重要的。
我尝试创建一个这样的子字符串:
int startpos = 0;
int stoppos= (662 * 7) / 100;
string str2 = hand.substr(startpos, stoppos);
cout << str2 << endl;
但这不是范围问题的答案。它只得到第一个X%,而且剪切很糟糕,输出为:AA,KK,QQ,JJ,A
应该是:AA,KK,QQ,JJ,AKs
我花了很多时间在这上面。我愿意接受建议,指导,甚至解决方案..
此致 GUI
答案 0 :(得分:4)
这是一种方法:取你的字符串,将其转换为序列,丢弃你不感兴趣的序列部分,然后将其转换回字符串。
所以我在Python中如何做到这一点:
start, stop = 30, 60
hands = 'AA,KK,QQ,JJ,AKs,AKo,AQs,AQo,TT,AJs,ATs,AJo,KQs,KJs,KTs,QJs,\
ATo,QTs,JTs,A9s,A9o,KQo,A8s,A8o,A7s,A7o,A6s,A6o,A5s,A5o,A4s,99,A4o,A3s,A2s,\
KJo,J9s,T9s,Q9s,QJo,KTo,Q9o,88,77,66,QTo,A3o,A2o,JTo,K9s,K8s,K7s,K6s,K5s,K4s,\
K3s,K2s,Q8s,Q7s,Q6s,Q5s,K9o,J8s,T8s,98s,97s,87s,86s,76s,96s,75s,65s,64s,J9o,\
T9o,55,54s,53s,52s,K8o,43s,32s,42s,J7s,T7s,K7o,44,33,22,Q4s,Q3s,Q2s,J6s,J5s,\
T6s,T5s,J4s,K6o,Q8o,J8o,T8o,98o,97o,87o,85s,K5o,K4o,K3o,K2o,95s,74s,76o,65o,\
54o,84s,94s,Q7o,J7o,T7o,Q6o,J3s,T4s,J2s,Q5o,T3s,T2s,Q4o,J6o,86o,T6o,96o,93s,\
Q3o,74o,84o,75o,64o,T2o,94o,53o,93o,63o,43o,92o,73o,83o,52o,82o,42o,62o,72o,\
J5o,63s,92s,73s,Q2o,J4o,83s,85o,82s,T5o,95o,J3o,62s,T4o,J2o,72s,T3o,32o'
hands = hands.split(',')
hands = hands[start * len(hands) / 100 : stop * len(hands) / 100]
print ','.join(hands)
在两个Stackoverflow线程here和here的帮助下,我将Python代码翻译成(当然不是惯用语)C ++:
#include <algorithm>
#include <iostream>
#include <sstream>
#include <string>
#include <vector>
using namespace std;
vector<string> &split(const string &s, char delim, vector<string> &elems) {
stringstream ss(s);
string item;
while (getline(ss, item, delim)) {
elems.push_back(item);
}
return elems;
}
vector<string> split(const string &s, char delim) {
vector<string> elems;
split(s, delim, elems);
return elems;
}
int main() {
// percentage markers to keep, 30% -- 60% here
int start = 30, stop = 60;
string hands = "AA,KK,QQ,JJ,AKs,AKo,AQs,AQo,TT,AJs,ATs,AJo,KQs,KJs,KTs,QJs,\
ATo,QTs,JTs,A9s,A9o,KQo,A8s,A8o,A7s,A7o,A6s,A6o,A5s,A5o,A4s,99,A4o,A3s,A2s,\
KJo,J9s,T9s,Q9s,QJo,KTo,Q9o,88,77,66,QTo,A3o,A2o,JTo,K9s,K8s,K7s,K6s,K5s,K4s,\
K3s,K2s,Q8s,Q7s,Q6s,Q5s,K9o,J8s,T8s,98s,97s,87s,86s,76s,96s,75s,65s,64s,J9o,\
T9o,55,54s,53s,52s,K8o,43s,32s,42s,J7s,T7s,K7o,44,33,22,Q4s,Q3s,Q2s,J6s,J5s,\
T6s,T5s,J4s,K6o,Q8o,J8o,T8o,98o,97o,87o,85s,K5o,K4o,K3o,K2o,95s,74s,76o,65o,\
54o,84s,94s,Q7o,J7o,T7o,Q6o,J3s,T4s,J2s,Q5o,T3s,T2s,Q4o,J6o,86o,T6o,96o,93s,\
Q3o,74o,84o,75o,64o,T2o,94o,53o,93o,63o,43o,92o,73o,83o,52o,82o,42o,62o,72o,\
J5o,63s,92s,73s,Q2o,J4o,83s,85o,82s,T5o,95o,J3o,62s,T4o,J2o,72s,T3o,32o";
vector<string> hands_ = split(hands, ',');
start = start * hands_.size() / 100;
stop = stop * hands_.size() / 100;
stringstream buffer;
for (size_t i = start; i < stop; ++i) {
if(i != start)
buffer << ',';
buffer << hands_[i];
}
string output = buffer.str();
cout << output << endl;
return 0;
}
答案 1 :(得分:2)
另一种方法:找到我们感兴趣的子字符串的索引,并提取该子字符串。但不会短得多。
#include <algorithm>
#include <iostream>
#include <string>
using namespace std;
int main() {
// percentage markers to keep, 30% -- 60% here
int start = 30, stop = 60;
string hands = "AA,KK,QQ,JJ,AKs,AKo,AQs,AQo,TT,AJs,ATs,AJo,KQs,KJs,KTs,QJs,\
ATo,QTs,JTs,A9s,A9o,KQo,A8s,A8o,A7s,A7o,A6s,A6o,A5s,A5o,A4s,99,A4o,A3s,A2s,\
KJo,J9s,T9s,Q9s,QJo,KTo,Q9o,88,77,66,QTo,A3o,A2o,JTo,K9s,K8s,K7s,K6s,K5s,K4s,\
K3s,K2s,Q8s,Q7s,Q6s,Q5s,K9o,J8s,T8s,98s,97s,87s,86s,76s,96s,75s,65s,64s,J9o,\
T9o,55,54s,53s,52s,K8o,43s,32s,42s,J7s,T7s,K7o,44,33,22,Q4s,Q3s,Q2s,J6s,J5s,\
T6s,T5s,J4s,K6o,Q8o,J8o,T8o,98o,97o,87o,85s,K5o,K4o,K3o,K2o,95s,74s,76o,65o,\
54o,84s,94s,Q7o,J7o,T7o,Q6o,J3s,T4s,J2s,Q5o,T3s,T2s,Q4o,J6o,86o,T6o,96o,93s,\
Q3o,74o,84o,75o,64o,T2o,94o,53o,93o,63o,43o,92o,73o,83o,52o,82o,42o,62o,72o,\
J5o,63s,92s,73s,Q2o,J4o,83s,85o,82s,T5o,95o,J3o,62s,T4o,J2o,72s,T3o,32o";
int nhands = count(hands.begin(), hands.end(), ',');
start = start * nhands / 100; // percentage -> comma count
stop = stop * nhands / 100; // percentage -> comma count
int start_ = start, stop_ = stop; // copies
int i = 0, substr_start, substr_end;
for (i = 0; i < hands.length() && start_ > 0; ++i) {
if (hands[i] == ',')
start_--;
}
substr_start = i;
stop_ -= start - 1; // already counted 'start' commas
for (; i < hands.length() && stop_ > 0; ++i) {
if (hands[i] == ',')
stop_--;
}
substr_end = i;
cout << hands.substr(substr_start, substr_end - substr_start - 1) << endl;
return 0;
}
答案 2 :(得分:1)
我只想指出字符串的百分比与指针的百分比不同。
不同的手或多或少比其他人更常见。具体而言,Offsuit指针(例如AKo)比适合的版本(AK)发生的可能性高三倍,并且成对的可能性是手上的一半。这是因为从卡片中拉出卡片的组合。
对于对,每个等级有C(4,2)= 6种可能的组合。
对于合适的手,只有套装会有所不同,因此有4种可能的组合。
对于非法手套,第一张卡可以有四种套装,但第二种卡只有三套(否则它适合),所以有12种组合。
为了获得适当的百分比,就手的可能性而言,你需要相应地对手进行加权。