C++ 如何比较C++;
我试图实现公牛和奶牛的游戏,但我有一个逻辑问题。我显式地检查每个数字是否等于相应索引(牛)或其他索引(牛)中的一个数字。我用(4321)检查的值应该是“0头公牛和4头奶牛”,但它却给了我“0头公牛和3头奶牛” 这是我的代码(很抱歉代码重复。我还想知道是否有人建议将此代码变小):C++ 如何比较C++;,c++,C++,我试图实现公牛和奶牛的游戏,但我有一个逻辑问题。我显式地检查每个数字是否等于相应索引(牛)或其他索引(牛)中的一个数字。我用(4321)检查的值应该是“0头公牛和4头奶牛”,但它却给了我“0头公牛和3头奶牛” 这是我的代码(很抱歉代码重复。我还想知道是否有人建议将此代码变小): #包括 #包括 使用名称空间std; int main(){ int值=1234; 智力猜测; int=0; int=0; 猜不透; int firstValue=(猜测%10000)/1000; int secondV
#包括
#包括
使用名称空间std;
int main(){
int值=1234;
智力猜测;
int=0;
int=0;
猜不透;
int firstValue=(猜测%10000)/1000;
int secondValue=(猜测%1000)/100;
int thirdValue=(猜测%100)/10;
int-fourthValue=guess%10;
如果(firstValue==((猜测值%10000)/1000)){bulls+=1;}
如果(firstValue==((猜测值%1000)/100)|
firstValue==((猜测值%100)/10)||
firstValue==(猜测为%10))
{cows+=1;}
如果(secondValue==((猜测值%1000)/100)){bulls+=1;}
else if(secondValue==((猜测值%10000)/1000)||
secondValue==((猜测值%100)/10)||
secondValue==(猜测为%10))
{cows+=1;}
如果(thirdValue==((猜测值%100)/10)){bulls+=1;}
else if(thirdValue==((猜测值%10000)/1000)||
第三个值==((猜测值%1000)/100)||
第三个值==(猜测为%10))
{cows+=1;}
如果(第四个值==(猜测值%10)){bulls+=1;}
else if(第四个值==((猜测值%10000)/1000)||
第四个值==((猜测值%1000)/100)||
第四个值==(猜测值%100)/10)
{cows+=1;}
cout2个你绝对需要掌握的概念:循环和函数。首先创建一些有用的函数
以下是您可以构建程序的功能:
int get_digit(int number, int order)
使用示例测试用例:
get_digit(7895, 0) == 5
get_digit(7895, 1) == 9
get_digit(7895, 2) == 8
get_digit(7895, 3) == 7
has_digit(7895, 1) == false
has_digit(7895, 8) == true
has_digit(7895, 5) == true
has_digit(7895, 0) == false
has_digit(7000, 0) == true
matches_digit(1239, 4269, 0) == true
matches_digit(1239, 4269, 1) == false
matches_digit(1239, 4269, 2) == true
matches_digit(1239, 4269, 2) == false
然后:
使用示例测试用例:
get_digit(7895, 0) == 5
get_digit(7895, 1) == 9
get_digit(7895, 2) == 8
get_digit(7895, 3) == 7
has_digit(7895, 1) == false
has_digit(7895, 8) == true
has_digit(7895, 5) == true
has_digit(7895, 0) == false
has_digit(7000, 0) == true
matches_digit(1239, 4269, 0) == true
matches_digit(1239, 4269, 1) == false
matches_digit(1239, 4269, 2) == true
matches_digit(1239, 4269, 2) == false
然后:
对于测试用例:
get_digit(7895, 0) == 5
get_digit(7895, 1) == 9
get_digit(7895, 2) == 8
get_digit(7895, 3) == 7
has_digit(7895, 1) == false
has_digit(7895, 8) == true
has_digit(7895, 5) == true
has_digit(7895, 0) == false
has_digit(7000, 0) == true
matches_digit(1239, 4269, 0) == true
matches_digit(1239, 4269, 1) == false
matches_digit(1239, 4269, 2) == true
matches_digit(1239, 4269, 2) == false
最后:
int get_cow(int a, int b)
int get_bull(int a, int b)
int main()
这是一种自上而下的设计,自下而上的实现方法。首先,你要考虑全局,找出你需要的小部分(功能)然后从最小、最独立的函数开始执行,然后逐步组合成更高的函数,直到达到main。2个你绝对需要掌握的概念:循环和函数。首先创建一些有用的函数
以下是您可以构建程序的功能:
int get_digit(int number, int order)
使用示例测试用例:
get_digit(7895, 0) == 5
get_digit(7895, 1) == 9
get_digit(7895, 2) == 8
get_digit(7895, 3) == 7
has_digit(7895, 1) == false
has_digit(7895, 8) == true
has_digit(7895, 5) == true
has_digit(7895, 0) == false
has_digit(7000, 0) == true
matches_digit(1239, 4269, 0) == true
matches_digit(1239, 4269, 1) == false
matches_digit(1239, 4269, 2) == true
matches_digit(1239, 4269, 2) == false
然后:
使用示例测试用例:
get_digit(7895, 0) == 5
get_digit(7895, 1) == 9
get_digit(7895, 2) == 8
get_digit(7895, 3) == 7
has_digit(7895, 1) == false
has_digit(7895, 8) == true
has_digit(7895, 5) == true
has_digit(7895, 0) == false
has_digit(7000, 0) == true
matches_digit(1239, 4269, 0) == true
matches_digit(1239, 4269, 1) == false
matches_digit(1239, 4269, 2) == true
matches_digit(1239, 4269, 2) == false
然后:
对于测试用例:
get_digit(7895, 0) == 5
get_digit(7895, 1) == 9
get_digit(7895, 2) == 8
get_digit(7895, 3) == 7
has_digit(7895, 1) == false
has_digit(7895, 8) == true
has_digit(7895, 5) == true
has_digit(7895, 0) == false
has_digit(7000, 0) == true
matches_digit(1239, 4269, 0) == true
matches_digit(1239, 4269, 1) == false
matches_digit(1239, 4269, 2) == true
matches_digit(1239, 4269, 2) == false
最后:
int get_cow(int a, int b)
int get_bull(int a, int b)
int main()
这是一种自上而下的设计,自下而上的实现方法。首先你要考虑全局,找出你需要的小部分(功能),然后从最小最独立的功能开始实现,然后逐步组合成更高的功能,直到你达到主功能
我还想知道是否有人建议将此代码变小
首先,使用std::vector
保留单独的数字:
std::vector<int> split( int v )
{
std::vector<int> r;
while( v ) {
r.push_back( v % 10 );
v /= 10;
}
return r;
}
第二个是计算奶牛和公牛的数量,因此需要进行调整:
cows -= bulls;
我还想知道是否有人建议将此代码变小
首先,使用std::vector
保留单独的数字:
std::vector<int> split( int v )
{
std::vector<int> r;
while( v ) {
r.push_back( v % 10 );
v /= 10;
}
return r;
}
第二个是计算奶牛和公牛的数量,因此需要进行调整:
cows -= bulls;
这是我的解决方案,它是有效的。基本上,我必须创建一个函数来提取每个数字,并将它们存储到一个向量中,该向量可以应用于模型和猜测。然后我使用find()
库的方法,以查看猜测向量中是否存在数字,如果存在,与模型向量相比,在哪个位置。相同位置等于1头公牛,不同位置等于1头奶牛。我将向该程序添加更多内容,例如随机生成模型,并在每一轮后循环程序,以便用户不必重新启动游戏
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
vector<int> getDigits(int modelValue) {
vector<int> vectorValue;
int extractedDigit = 0;
int modulant = 10000;
int divisor = 1000;
for (int i = 0; i < 4; i++) {
extractedDigit = (modelValue % modulant) / divisor;
vectorValue.push_back(extractedDigit);
modulant /= 10;
divisor /= 10;
}return vectorValue;
}
int main() {
int model = 1234;
int guess = 0000;
int bulls = 0;
int cows = 0;
int counter = 1;
cout << "Enter a value to guess: ";
cin >> guess;
vector<int> modelVector = getDigits(model);
vector<int> guessVector = getDigits(guess);
for (int i = 0; i < 4; i++) {
if (find(modelVector.begin(), modelVector.end(), guessVector[i]) != modelVector.end()) {
if (modelVector[i] == guessVector[i]) {bulls += 1;}
else { cows += 1; }
}
}cout << "There are " << bulls << " bulls and " << cows << " cows"<< endl;
}
#包括
#包括
#包括
使用名称空间std;
向量getDigits(int modelValue){
矢量值;
int-extractedDigit=0;
int模数=10000;
整数除数=1000;
对于(int i=0;i<4;i++){
extractedDigit=(模型值%modulant)/除数;
向量值。推回(提取数字);
模数/=10;
除数/=10;
}返回向量值;
}
int main(){
int模型=1234;
int guess=0000;
int=0;
int=0;
int计数器=1;
猜不透;
向量模型向量=getDigits(模型);
向量猜测向量=getDigits(猜测);
对于(int i=0;i<4;i++){
if(find(modelVector.begin()、modelVector.end()、guessVector[i])!=modelVector.end()){
如果(modelVector[i]==guessVector[i]){bulls+=1;}
else{cows+=1;}
}
}cout这是我的解决方案,它是有效的。基本上,我必须创建一个函数来提取每个数字,并将它们存储到一个可以应用于模型和猜测的向量中。然后我使用find()
库的方法,以查看猜测向量中是否存在数字,如果存在,与模型向量相比,在哪个位置。相同位置等于1头公牛,不同位置等于1头奶牛。我将向该程序添加更多内容,例如随机生成模型,并在每一轮后循环程序,以便用户不必重新启动游戏
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
vector<int> getDigits(int modelValue) {
vector<int> vectorValue;
int extractedDigit = 0;
int modulant = 10000;
int divisor = 1000;
for (int i = 0; i < 4; i++) {
extractedDigit = (modelValue % modulant) / divisor;
vectorValue.push_back(extractedDigit);
modulant /= 10;
divisor /= 10;
}return vectorValue;
}
int main() {
int model = 1234;
int guess = 0000;
int bulls = 0;
int cows = 0;
int counter = 1;
cout << "Enter a value to guess: ";
cin >> guess;
vector<int> modelVector = getDigits(model);
vector<int> guessVector = getDigits(guess);
for (int i = 0; i < 4; i++) {
if (find(modelVector.begin(), modelVector.end(), guessVector[i]) != modelVector.end()) {
if (modelVector[i] == guessVector[i]) {bulls += 1;}
else { cows += 1; }
}
}cout << "There are " << bulls << " bulls and " << cows << " cows"<< endl;
}
#包括
#包括
#包括
使用名称空间std;
向量getDigits(int modelValue){
矢量值;
int-extractedDigit=0;
int模数=10000;
整数除数=1000;
对于(int i=0;i<4;i++){
extractedDigit=(模型值%modulant)/除数;
向量值。推回(提取数字);
模数/