Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/131.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 尝试使用数组(Yahtzee)C++;_C++ - Fatal编程技术网

C++ 尝试使用数组(Yahtzee)C++;

C++ 尝试使用数组(Yahtzee)C++;,c++,C++,我试图检查保存的骰子卷(最多可以是5个)是否为3个,因此我试图将骰子卷值相互比较 当然,R的第一个值显示为0,但是在代码运行之后,R的第二个值每次显示为8191,我不完全确定为什么 我也试过使用 r++而不是r+=r+1,但这当然不会改变任何事情 int r = 0; cout << "first value of R is " << r << endl; for(int t = 0; t < 5; t++) {

我试图检查保存的骰子卷(最多可以是5个)是否为3个,因此我试图将骰子卷值相互比较

当然,R的第一个值显示为0,但是在代码运行之后,R的第二个值每次显示为8191,我不完全确定为什么

我也试过使用 r++而不是r+=r+1,但这当然不会改变任何事情

int r = 0;
          cout << "first value of R is " << r << endl;
          for(int t = 0; t < 5; t++) {
            for(int w = 0; w < 5; w++) {
              if(keptDice[t] == keptDice[w] ) {
                r += r + 1;
              }
            }
          }
          cout << "Value of R is " << r << endl;
intr=0;
库特
和写作一样

r = r + r + 1
r每次都在翻倍。有趣的是,它总是比2^n小1

r = 0 + 0 + 1 (1)
r = 1 + 1 + 1 (3)
r = 3 + 3 + 1 (7)
r = 7 + 7 + 1 (15)
r = 15 + 15 + 1 (31)
r = 31 + 31 + 1 (63)
r = 63 + 63 + 1 (127)
r = 127 + 127 + 1 (255)
r = 255 + 255 + 1 (511)
r = 511 + 511 + 1 (1023)
r = 1023 + 1023 + 1 (2047)
r = 2047 + 2047 + 1 (4095)
r = 4095 + 4095 + 1 (8191)
你的程序正在计算13场比赛。对于yahtzee,您可能需要一个计算匹配数的数组,否则将在每个骰子上加倍。例如,如果你有

1 2 3 4 1
它将计算与最后一个模具匹配的前1个模具和与第一个模具匹配的最后一个模具(2个匹配)

更明智的做法是计算您有多少个1,有多少个2,然后存储在一个数组中

int diceCount[6];
for(int num = 1; num <= 6; num++) {
        for(int w = 0; w < 5; w++) {
          int count = 0;
          if(keptDice[w] == num ) {
            count++;
          }
          diceCount[num-1] = count;
        }
int diceCount[6];
for(int num=1;num
和写作一样

r = r + r + 1
r每次都加倍。有趣的是,它总是比2^n小1

r = 0 + 0 + 1 (1)
r = 1 + 1 + 1 (3)
r = 3 + 3 + 1 (7)
r = 7 + 7 + 1 (15)
r = 15 + 15 + 1 (31)
r = 31 + 31 + 1 (63)
r = 63 + 63 + 1 (127)
r = 127 + 127 + 1 (255)
r = 255 + 255 + 1 (511)
r = 511 + 511 + 1 (1023)
r = 1023 + 1023 + 1 (2047)
r = 2047 + 2047 + 1 (4095)
r = 4095 + 4095 + 1 (8191)
您的程序正在计算13个匹配项。对于yahtzee,您可能需要一个计算匹配项的数组,否则您将在每个骰子上加倍。例如,如果

1 2 3 4 1
它将计算与最后一个模具匹配的前1个模具和与第一个模具匹配的最后一个模具(2个匹配)

更明智的做法是计算您有多少个1,有多少个2,然后存储在一个数组中

int diceCount[6];
for(int num = 1; num <= 6; num++) {
        for(int w = 0; w < 5; w++) {
          int count = 0;
          if(keptDice[w] == num ) {
            count++;
          }
          diceCount[num-1] = count;
        }
int diceCount[6];

for(int num=1;num关键是在第二个for循环中,你必须让tor从t(int w=t;…)开始,否则你会将每个骰子与其本身进行比较,这自然是相等的。另外,使用r++而不是r+=r+1,这肯定是错误的,但我认为这只是一个拼写错误

int r = 0;
cout << "first value of R is " << r << endl;
for(int t = 0; t < 5; t++) {
    for(int w = t; w < 5; w++) {
        if(keptDice[t] == keptDice[w] ) {
            r++;
        }
    }
}
cout << "Value of R is " << r << endl;
intr=0;

关键是,在第二个for循环中,你必须从t(int w=t;…)开始,否则你会将每个骰子与其本身进行比较,这自然是相等的。另外,使用r++而不是r+=r+1,这肯定是错误的,但我认为这只是一个拼写错误

int r = 0;
cout << "first value of R is " << r << endl;
for(int t = 0; t < 5; t++) {
    for(int w = t; w < 5; w++) {
        if(keptDice[t] == keptDice[w] ) {
            r++;
        }
    }
}
cout << "Value of R is " << r << endl;
intr=0;

cout基于您的方法,但概括为N类:

int N = 3; // N in [1;5]
bool isNOfAKind = false;
for(int t = 0; t < 6-N; t++) { // skip searches with less elements than N
    int r = 0; // r must be reset for each count
    for(int w = t+1; w < 5; w++) { // avoid comparing to self
        if(keptDice[t] == keptDice[w]) {
            r++;
        }
    }
    // found a solution already? then bail out.
    if(r == N) {
        isNOfAKind = true;
        break;
    }
}
cout << N << " of a kind? " << isNOfAKind << endl;
int N=3;//N在[1;5]中
bool isNOfAKind=false;
对于(int t=0;t<6-N;t++){//跳过元素少于N的搜索
int r=0;//必须为每个计数重置r
对于(intw=t+1;w<5;w++){//避免与self比较
if(keptDice[t]==keptDice[w]){
r++;
}
}
//已经找到解决办法了吗?那就保释吧。
如果(r==N){
isNOfAKind=true;
打破
}
}

cout基于您的方法,但概括为N类:

int N = 3; // N in [1;5]
bool isNOfAKind = false;
for(int t = 0; t < 6-N; t++) { // skip searches with less elements than N
    int r = 0; // r must be reset for each count
    for(int w = t+1; w < 5; w++) { // avoid comparing to self
        if(keptDice[t] == keptDice[w]) {
            r++;
        }
    }
    // found a solution already? then bail out.
    if(r == N) {
        isNOfAKind = true;
        break;
    }
}
cout << N << " of a kind? " << isNOfAKind << endl;
int N=3;//N在[1;5]中
bool isNOfAKind=false;
对于(int t=0;t<6-N;t++){//跳过元素少于N的搜索
int r=0;//必须为每个计数重置r
对于(intw=t+1;w<5;w++){//避免与self比较
if(keptDice[t]==keptDice[w]){
r++;
}
}
//已经找到解决办法了吗?那就保释吧。
如果(r==N){
isNOfAKind=true;
打破
}
}

cout
r+=r+1
相当于
r=r+1
。因此,每个t循环都保证一些w==t.
r+=r+1
应该是
r+=1
,或者
r+1
。但是循环也是错误的,因为
t
w
通常是相等的,而且
keptDice[i]==keptDice[w]
将变得微不足道(毫无意义)正确。不编译。
r+=r+1
相当于
r=r+1
。因此,每个t循环都保证一些w==t。
r+=r+1
应该是
r+=1
r=1
,或者
+r
。但是循环也是错误的,因为
t
w
通常是相等的,而
 keptDice[i]==keptDice[w]
将变得微不足道(而且毫无意义)正确。未编译。感谢帮助!您提供的修改代码有点不正确。您必须将int count置于for循环之外,否则它将无法正常工作。一旦这样做,它将正常工作。感谢帮助!您提供的修改代码有点不正确。您必须将int count置于for l之外哦,否则它就不能正常工作。一旦你这样做了,它就可以正常工作了