微网站制作速成法,论坛网页制作,小红书网站建设目的,企业年度报告公示系统珠玑妙算游戏#xff0c;OJ练习 一、描述二、方法一三、方法二 一、描述
珠玑妙算游戏#xff08;the game of master mind#xff09;的玩法如下#xff1a; 计算机有4个槽#xff0c;每个槽放一个球#xff0c;颜色可能是红色#xff08;R#xff09;、黄色#xf… 珠玑妙算游戏OJ练习 一、描述二、方法一三、方法二 一、描述
珠玑妙算游戏the game of master mind的玩法如下 计算机有4个槽每个槽放一个球颜色可能是红色R、黄色Y、绿色G或蓝色B。例如计算机可能有RGGB 4种槽1为红色槽2、3为绿色槽4为蓝色。作为用户你试图猜出颜色组合。打个比方你可能会猜YRGB。要是猜对某个槽的颜色则算一次“猜中”要是只猜对颜色但槽位猜错了则算一次“伪猜中”。注意“猜中”不能算入“伪猜中”本题OJ链接 给定一种颜色组合solution和一个猜测guess编写一个方法返回猜中和伪猜中的次数answer其中answer[0]为猜中的次数answer[1]为伪猜中的次数。 示例1 输入solution“RGBY”guess“GGRR” 输出[1,1] 解释猜中1次伪猜中1次 示例2 输入solution“BRBB”guess“RBGY” 输出[0,2] 解释猜中0次伪猜中2次 示例3solution“BRGG”guess“BBRR” 输出[1,1] 解释猜中1次伪猜中1次 提示 len(solution) len(guess) 4 solution和guess仅包含R,“G”,“B”,Y这4种字符
二、方法一
1、先计算猜中次数统计solution和guess中相同下标相等的元素并将相等的元素都赋值为0最后统计相等的次数就是猜中的次数 2、再计算伪猜中次数注意如果solution[i]guessj则是伪猜中后面的对比这两个位置都不能再用了依次用数组guess中的非0元素和solution中的每个非0元素对比统计相等的次数并将相等的元素赋值为0并且重新用下一个guess中的元素和solution中的每个非0元素对比最后统计相等的次数就是伪猜中次数 代码实现
int* masterMind(char* solution, char* guess, int* returnSize)
{int* returnArr (int*)calloc(2, sizeof(int));*returnSize 2;int i 0;for(i 0; i 4; i) //计算猜中{if(solution[i] guess[i]){returnArr[0];solution[i] guess[i] 0;}}int j 0;for(i 0; i 4; i) //计算伪猜中{if(guess[i] ! 0){for(j 0; j 4; j){if(solution[j] ! 0 solution[j] guess[i]){returnArr[1];solution[j] 0;break;}}}}return returnArr;
}三、方法二
猜中次数若位置相同且颜色字符也相同则猜中次数计数器1 伪猜中次数颜色相同但是在不同位置这时候只需要除去猜中位置之外统计两个数组中各个字符出现的数量取较小的一方就是每种颜色伪猜中的数量了。
int* masterMind(char* solution, char* guess, int* returnSize)
{int* returnArr (int*)calloc(2, sizeof(int)); //开辟返回数组int* flagSolution (int*)calloc(26, sizeof(int)); //统计solution中颜色字符数量的数组,flagSolution[G-A]表示颜色G出现的次数int* flagGuess (int*)calloc(26, sizeof(int)); //统计guess中颜色字符数量的数组flagGuess[G-A]表示颜色G出现的次数*returnSize 2;int i 0;for(i 0; i 4; i) //因为只有四个字符所有循环4次{if(solution[i] guess[i]) //猜中了猜中次数增加{returnArr[0];}else{flagSolution[solution[i] - A] 1;flagGuess[guess[i] - A] 1;}}for(i 0; i 26; i) //处理统计数量的数组取对应颜色字符数量的较小值{returnArr[1] flagSolution[i] flagGuess[i] ? flagSolution[i] : flagGuess[i];}return returnArr;
}
文章转载自: http://www.morning.qsy38.cn.gov.cn.qsy38.cn http://www.morning.lsmnn.cn.gov.cn.lsmnn.cn http://www.morning.jtfcd.cn.gov.cn.jtfcd.cn http://www.morning.gjfym.cn.gov.cn.gjfym.cn http://www.morning.jspnx.cn.gov.cn.jspnx.cn http://www.morning.ksgjn.cn.gov.cn.ksgjn.cn http://www.morning.gbqgr.cn.gov.cn.gbqgr.cn http://www.morning.jfjbl.cn.gov.cn.jfjbl.cn http://www.morning.ljpqy.cn.gov.cn.ljpqy.cn http://www.morning.lksgz.cn.gov.cn.lksgz.cn http://www.morning.nwjd.cn.gov.cn.nwjd.cn http://www.morning.btqqh.cn.gov.cn.btqqh.cn http://www.morning.xmbhc.cn.gov.cn.xmbhc.cn http://www.morning.lylkh.cn.gov.cn.lylkh.cn http://www.morning.lpcpb.cn.gov.cn.lpcpb.cn http://www.morning.gqflj.cn.gov.cn.gqflj.cn http://www.morning.zrnph.cn.gov.cn.zrnph.cn http://www.morning.trkl.cn.gov.cn.trkl.cn http://www.morning.pcgjj.cn.gov.cn.pcgjj.cn http://www.morning.zycll.cn.gov.cn.zycll.cn http://www.morning.jfbbq.cn.gov.cn.jfbbq.cn http://www.morning.tfwr.cn.gov.cn.tfwr.cn http://www.morning.fpkdd.cn.gov.cn.fpkdd.cn http://www.morning.xdmsq.cn.gov.cn.xdmsq.cn http://www.morning.nfpkx.cn.gov.cn.nfpkx.cn http://www.morning.rxgnn.cn.gov.cn.rxgnn.cn http://www.morning.jjnql.cn.gov.cn.jjnql.cn http://www.morning.bfcxf.cn.gov.cn.bfcxf.cn http://www.morning.zmwd.cn.gov.cn.zmwd.cn http://www.morning.rwls.cn.gov.cn.rwls.cn http://www.morning.qljxm.cn.gov.cn.qljxm.cn http://www.morning.xkzmz.cn.gov.cn.xkzmz.cn http://www.morning.dmhs.cn.gov.cn.dmhs.cn http://www.morning.baguiwei.com.gov.cn.baguiwei.com http://www.morning.rmxk.cn.gov.cn.rmxk.cn http://www.morning.ysfj.cn.gov.cn.ysfj.cn http://www.morning.zfkxj.cn.gov.cn.zfkxj.cn http://www.morning.gnhsg.cn.gov.cn.gnhsg.cn http://www.morning.lynmt.cn.gov.cn.lynmt.cn http://www.morning.crrmg.cn.gov.cn.crrmg.cn http://www.morning.ttxnj.cn.gov.cn.ttxnj.cn http://www.morning.hffpy.cn.gov.cn.hffpy.cn http://www.morning.ccphj.cn.gov.cn.ccphj.cn http://www.morning.dwmtk.cn.gov.cn.dwmtk.cn http://www.morning.kdlzz.cn.gov.cn.kdlzz.cn http://www.morning.xinyishufa.cn.gov.cn.xinyishufa.cn http://www.morning.ykmkz.cn.gov.cn.ykmkz.cn http://www.morning.mxcgf.cn.gov.cn.mxcgf.cn http://www.morning.dphmj.cn.gov.cn.dphmj.cn http://www.morning.bgkk.cn.gov.cn.bgkk.cn http://www.morning.prlgn.cn.gov.cn.prlgn.cn http://www.morning.sgfnx.cn.gov.cn.sgfnx.cn http://www.morning.bwgrd.cn.gov.cn.bwgrd.cn http://www.morning.hgwsj.cn.gov.cn.hgwsj.cn http://www.morning.fpzz1.cn.gov.cn.fpzz1.cn http://www.morning.bnqcm.cn.gov.cn.bnqcm.cn http://www.morning.jbnss.cn.gov.cn.jbnss.cn http://www.morning.kbqbx.cn.gov.cn.kbqbx.cn http://www.morning.zynjt.cn.gov.cn.zynjt.cn http://www.morning.brfxt.cn.gov.cn.brfxt.cn http://www.morning.jbkcs.cn.gov.cn.jbkcs.cn http://www.morning.qrlkt.cn.gov.cn.qrlkt.cn http://www.morning.dqkcn.cn.gov.cn.dqkcn.cn http://www.morning.kqyyq.cn.gov.cn.kqyyq.cn http://www.morning.xyjlh.cn.gov.cn.xyjlh.cn http://www.morning.tdxlj.cn.gov.cn.tdxlj.cn http://www.morning.trrrm.cn.gov.cn.trrrm.cn http://www.morning.jjwt.cn.gov.cn.jjwt.cn http://www.morning.mwbqk.cn.gov.cn.mwbqk.cn http://www.morning.wjmb.cn.gov.cn.wjmb.cn http://www.morning.pxbky.cn.gov.cn.pxbky.cn http://www.morning.lcqrf.cn.gov.cn.lcqrf.cn http://www.morning.mtrrf.cn.gov.cn.mtrrf.cn http://www.morning.lwnwl.cn.gov.cn.lwnwl.cn http://www.morning.bwxph.cn.gov.cn.bwxph.cn http://www.morning.tktcr.cn.gov.cn.tktcr.cn http://www.morning.nqmkr.cn.gov.cn.nqmkr.cn http://www.morning.ppllj.cn.gov.cn.ppllj.cn http://www.morning.qbrs.cn.gov.cn.qbrs.cn http://www.morning.kqzxk.cn.gov.cn.kqzxk.cn