2018刑侦科推理试题c语言,2018年刑侦科推理试题(C++)(示例代码)
上学期看到2018年刑侦科推理试题,直觉告诉我可以嵌套N层for循环来解答,但后面还是用组合算法穷举出所有组合算到答案,嵌套太深不好搞啊。
从 0b00000000000000000000 自增到 0b11111111111111111111,每2bit表示一道题的答案,即: 0b00为A,0b01为B,0b10为C,0b11为D,
利用位移操作从左边高位依次读取每2bit的数,判断后不符合题意的组合就扔掉,最终肯定有一个组合答案符合。
下面我自己的解法
//
// Created by sys234 on 4/3/2018.
//
#include
#include
#include
#include
using namespace std;
const unsigned int A = 0;
const unsigned int B = 1;
const unsigned int C = 2;
const unsigned int D = 3;
vector getAnswerLine(unsigned int num) {
vector answer;
for (unsigned int j = 0; j < 20; j += 2) {
//offset 2bit
unsigned int x = ((num << (32 - 2 - j)) >> 30);
answer.push_back(x);
}
return answer;
}
bool isTheSame(int qnum, int qans) {
return qnum == qans;
}
bool notTheSame(int qnum, int qans) {
return qnum != qans;
}
bool isNear(int qans1, int qans2) {
return abs(qans1 - qans2) == 1;
}
unsigned int getAnsAppearTimes(const vector &alls, int max = -1, bool retAsTimes = false) {
map stat;
for (auto item : alls) {
int val = item;
++stat[val];
}
int minAns = 0;//supposing A
int qTimes = 0;
auto pair1 = stat.find(0);
minAns = 0;
qTimes = pair1->second;
for (int i = 1; i < 4; ++i) {
pair1 = stat.find(i);
if (qTimes > pair1->second && -1 == max) {
minAns = pair1->first;
qTimes = pair1->second;
continue;
}
if (qTimes < pair1->second && -1 != max) {
minAns = pair1->first;
qTimes = pair1->second;
continue;
}
}
if (retAsTimes == false) {
return minAns;
}
return qTimes;
}
unsigned int getAnsNumTimes(const vector &alls, int max = -1) {
return getAnsAppearTimes(alls, max, true);
}
char echoAns(int n) {
switch (n) {
case 0:
return ‘A‘;
case 1:
return ‘B‘;
case 2:
return ‘C‘;
case 3:
return ‘D‘;
default:
return ‘ ‘;
}
}
vector getAnswerLine(unsigned int num);
void combination() {
unsigned int ansMax = 0b11111111111111111111;//note: 20/2=10 ,2bit presents a answer;A=>00,B=>01,C=>10,D=>11
unsigned int ansMin = 0b0;
unsigned int i;
for (i = 0; i <= ansMax; ++i) {
vector one = getAnswerLine(i);
unsigned int q1 = one[0], q2 = one[1], q3 = one[2], q4 = one[3], q5 = one[4], q6 = one[5], q7 = one[6], q8 = one[7], q9 = one[8], q10 = one[9];
if (q1 == q2 && q1 == q3 && q1 == q4 && q1 == q5 && q1 == q6 && q1 == q7 && q1 == q8 && q1 == q9 && q1 == q10) {
//excluding all the same answer
continue;
}
if (isTheSame(q2, A) && notTheSame(q5, C)
|| isTheSame(q2, B) && notTheSame(q5, D)
|| isTheSame(q2, C) && notTheSame(q5, A)
|| isTheSame(q2, D) && notTheSame(q5, B)) {
continue;
//excluding 2nd
}
if (isTheSame(q3, A) && isTheSame(q3, q6)
|| isTheSame(q3, A) && isTheSame(q3, q2)
|| isTheSame(q3, A) && isTheSame(q3, q4)
//
|| isTheSame(q3, B) && isTheSame(q3, q6)
|| isTheSame(q3, B) && isTheSame(q6, q2)
|| isTheSame(q3, B) && isTheSame(q6, q4)
//
|| isTheSame(q3, C) && isTheSame(q3, q2)
|| isTheSame(q3, C) && isTheSame(q6, q2)
|| isTheSame(q3, C) && isTheSame(q2, q4)
//
|| isTheSame(q3, D) && isTheSame(q3, q4)
|| isTheSame(q3, D) && isTheSame(q6, q4)
|| isTheSame(q3, D) && isTheSame(q2, q4)
) {
continue;
//excluding 3rd
}
if (isTheSame(q4, A) && notTheSame(q1, q5)
|| isTheSame(q4, B) && notTheSame(q2, q7)
|| isTheSame(q4, C) && notTheSame(q1, q9)
|| isTheSame(q4, D) && notTheSame(q6, q10)) {
continue;
//excluding4
}
if (isTheSame(q5, A) && notTheSame(q8, A)
|| isTheSame(q5, B) && notTheSame(q4, B)
|| isTheSame(q5, C) && notTheSame(q9, C)
|| isTheSame(q5, D) && notTheSame(q7, D)) {
continue;
//excluding 5
}
if (isTheSame(q6, A) && (notTheSame(q2, q8) || notTheSame(q4, q8))
|| isTheSame(q6, B) && (notTheSame(q1, q8) || notTheSame(q6, q8))
|| isTheSame(q6, C) && (notTheSame(q3, q8) || notTheSame(q10, q8))
|| isTheSame(q6, D) && (notTheSame(q5, q8) || notTheSame(q9, q8))
) {
continue;
//excluding 6
}
int selectedLeastAlpha = getAnsAppearTimes(one, -1);
if (isTheSame(q7, A) && notTheSame(selectedLeastAlpha, C)
|| isTheSame(q7, B) && notTheSame(selectedLeastAlpha, B)
|| isTheSame(q7, C) && notTheSame(selectedLeastAlpha, A)
|| isTheSame(q7, D) && notTheSame(selectedLeastAlpha, D)
) {
continue;
//excluding 7
}
if (isTheSame(q8, A) && isNear(q1, q7)
|| isTheSame(q8, B) && isNear(q1, q5)
|| isTheSame(q8, C) && isNear(q1, q2)
|| isTheSame(q8, D) && isNear(q1, q10)
) {
continue;
//excluding 8
}
if (isTheSame(q9, A) && isTheSame(q1, q6) && isTheSame(q6, q5)
|| isTheSame(q9, A) && notTheSame(q1, q6) && notTheSame(q6, q5)
//
|| isTheSame(q9, B) && isTheSame(q1, q6) && isTheSame(q10, q5)
|| isTheSame(q9, B) && notTheSame(q1, q6) && notTheSame(q10, q5)
//
|| isTheSame(q9, C) && isTheSame(q1, q6) && isTheSame(q2, q5)
|| isTheSame(q9, C) && notTheSame(q1, q6) && notTheSame(q2, q5)
//
|| isTheSame(q9, D) && isTheSame(q1, q6) && isTheSame(q9, q5)
|| isTheSame(q9, D) && notTheSame(q1, q6) && notTheSame(q9, q5)
//
) {
continue;
//excluding 9th
}
int optionsCountMax = getAnsNumTimes(one, 1);
int optionsCountMin = getAnsNumTimes(one, -1);
int diff = optionsCountMax - optionsCountMin;
if (isTheSame(q10, A) && 3 != diff
|| isTheSame(q10, B) && 2 != diff
|| isTheSame(q10, C) && 4 != diff
|| isTheSame(q10, D) && 1 != diff
) {
continue;
//exclusing 10th
}
//print answer
for (int lastAns : one) {
cout << echoAns(lastAns)<
}
cout << endl;
}
}
int main() {
std::cout << clock() << std::endl;
combination();
std::cout << clock() << std::endl;
return 0;
}
解题中少了几个过滤条件,打印除算出好几行答案,差点以为我解法写错了,然后搜索到其他网友的答案,提有意思了,有嵌套多层for循环生成组合的。
有用4进制组合答案,多种语言好多技巧,学习了...
最终把缺少过滤条件判断加上去就OK了。
2018刑侦科推理试题c语言,2018年刑侦科推理试题(C++)(示例代码)相关推荐
- java springcloud面试题_JAVA语言之springboot+springcloud相关面试题
本文主要向大家介绍了JAVA语言之springboot+springcloud相关面试题,通过具体的内容向大家展示,希望对大家学习JAVA语言有所帮助. 什么是springboot 用来简化sprin ...
- c语言二级上机考试模拟试题,C语言省二级考试上机模拟试题库
<C语言省二级考试上机模拟试题库>由会员分享,可在线阅读,更多相关<C语言省二级考试上机模拟试题库(27页珍藏版)>请在人人文库网上搜索. 1.C语言省二级考试上机模拟试题库试 ...
- 牛客网阶乘怪物C语言,【题集】牛客网·2018年全国多校算法寒假训练营练习比赛(第二场)(示例代码)...
时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 32768K,其他语言65536K 64bit IO Format: %lld 题目描述 小鱼儿吐泡泡,嘟嘟嘟冒出来.小鱼儿会吐出两种泡 ...
- 北邮信通复试题c语言,北邮计算机考研复试题的C语言解答精选.pdf
北北邮邮计计算算机机考考研研复复试试题题的的C语语言言解解答答精精选选 二二进进制制数数 题目 题目描述: 大家都知道 数据在计算机里中存储是以二进制的形式存储的. 有一天 小明学了C语言之后 他想知 ...
- 神州数码笔试题C语言,神州数码程序类笔试题分享
神州程序类笔试题分享: 神州数码笔试1)description of the students table: sid_id number start_date date end_date date w ...
- c语言生成随机坐标,C语言 文件的随机读写详解及示例代码
前面介绍的文件读写函数都是顺序读写,即读写文件只能从头开始,依次读写各个数据.但在实际开发中经常需要读写文件的中间部分,要解决这个问题,就得先移动文件内部的位置指针,再进行读写.这种读写方式称为随机读 ...
- 找出递增数组中所有相加为m的组合c语言,组合(1-m中选n个数)(示例代码)
#include int a[100],b[100]={0},m,n; bool p(int k)//定义一个bool类型函数 ,用来判断 { if(b[k]==1) { return false; ...
- random函数 c语言,C/C++ 中rand()和random()函数(示例代码)
nt rand(void); 返回一个随机数0 ~ pow(2, sizeof(int))-1 long int random(void); 返回一个随机数0 ~ pow(2, sizeof(long ...
- C语言分支循环结构1,c语言——运算符、分支结构、循环结构(示例代码)
一.运算符 关系运算符 关系运算符含 义数学中的表示 < 小于 < <= 小于或等于 ≤ > 大于 > >= 大于或等于 ≥ == 等于 = != 不等于 ≠ 条 ...
最新文章
- 【转载】WinCE OAL架构分析
- moa 35 批量删除
- 基于Xml 的IOC 容器-向容器注册
- html5游戏制作入门系列教程(三)
- java线程锁标记_java 线程 中断标志位
- 面向机器学习的特征工程 七、非线性特征提取和模型堆叠
- 不要有思维的惯性, 做每件事情之前, 都【确认好要做什么】!
- Spring设计模式之装饰器模式
- csdn 积分/c币获取方法
- 百度和知乎哪个引流效果好?知乎和百度的有什么区别?
- lcms质谱仪_常用质谱的优缺点_液质联用(LCMS)仪器社区_仪器信息网论坛
- 超链接 qq群一键添加
- 数据挖掘总结之消极学习与积极学习
- Spark性能调优案例-优化spark估计表大小失败 和 小表关联 走 broadcast join
- 求最长回文子串——C++ (动态规划+暴力解法)
- MCS-51单片机常用汇编指令大全
- iis怎么安装php,IIS下如何安装php扩展
- linux系统挂载u盘步骤和命令,如何在linux中挂载U盘/硬盘
- python 菜鸟联盟快递查询_快递单号查询接口-极兔速递
- [转载]每周问问你的团队这10个问题