一,题目:(谷歌笔试)

n支队伍比赛,分别编号为0,1,2……n-1,已知它们之间的实力对比关系,存储在一个二维数组w[n][n]中,w[i][j] 的值代表编号为i,j的队伍中更强的一支。所以w[i][j]=i 或者j,现在给出它们的出场顺序,并存储在数组order[n]中,比如order[n] = {4,3,5,8,1......},那么第一轮比赛就是 4对3, 5对8。.......胜者晋级,败者淘汰,同一轮淘汰的所有队伍排名不再细分,即可以随便排,下一轮由上一轮的胜者按照顺序,再依次两两比,比如可能是4对5,直至出现第一名

编程实现,给出二维数组w,一维数组order 和 用于输出比赛名次的数组result[n],求出result(特别注意:result输出的是从第一名到最后一名)

二,分析

这里有一个简单的做法,就是:查看每一行重复数字个数,如果重复数字个数为n则排名为1,重复数字个数n-1则排名为2。所以重复数字个数为i个,则排名为n+1-i;给出order[],则相应的result很容易得出。这里值得注意的是:order仅仅为出场顺序,跟result没有任何关系。

对应源码为:

#include <iostream> #include <stack> using namespace std; /*order 顺序为pk顺序*/ int main() { int w[6][6] = { //排名 重复数 0,1,2,3,0,0, //4 3 1,1,2,1,1,1, //2 5 2,2,2,2,2,2, //1 6 3,1,2,3,3,3, //3 4 0,1,2,3,4,5, //6 1 0,1,2,3,5,5 //5 2 }; int order[6] = {1,3,4,2,0,5}; int weight[6];//2,3,6,1,4,5 int result[6]; int max; for(int i=0;i<6;i++) { max=0; int temp[6]={0,0,0,0,0,0}; for(int j=0;j<6;j++) { temp[w[i][j]]++; } for(int k=0;k<6;k++)//获取排名 { if(max<temp[k]) max=temp[k];//获取最大重复数 } weight[i]=7-max;//排名 } int n=6; int count=6; int count_left=0; int flag; while(count) { count_left=0; for(int i=0;i<6;i=i+2) { if(weight[order[i]]>weight[order[i+1]]) { result[--count]=order[i];//排名在后面的直接 order[count_left++]=order[i+1];//排名在前面的 } else { result[--count]=order[i+1]; order[count_left++]=order[i]; } if(i!=0) { order[count_left++]=order[i+1]; } if(count==0) result[0]=order[0]; } // cout<<"count "<<count<<endl; } //result[0]=weight[order[i]]; for(int i=0;i<6;i++) { cout<<result[i]<<endl; } return 0; }

上述源码中,明显的缺点就是,通过记录重复个数来判断哪个优先级高。这里完全可以根据两个元素行与列交叉处值来判断。

改进后源码:

#include <iostream> #include <stack> using namespace std; /*order 顺序为pk顺序*/ int main() { int w[6][6] = { //排名 重复数 0,1,2,3,0,0, //4 3 1,1,2,1,1,1, //2 5 2,2,2,2,2,2, //1 6 3,1,2,3,3,3, //3 4 0,1,2,3,4,5, //6 1 0,1,2,3,5,5 //5 2 }; int order[6] = {1,3,4,2,0,5}; int result[6]; int count=6; int count_left=0; int flag; while(count) { count_left=0; for(int i=0;i<6;i=i+2) { int nWiner = *((int*)w + 6 * order[i] + order[i+1]);//取两个数交叉的数 if(nWiner==order[i+1]) { result[--count]=order[i];//排名在后面的直接 order[count_left++]=order[i+1];//排名在前面的 } else{ result[--count]=order[i+1]; order[count_left++]=order[i]; } if(i!=0) { order[count_left++]=order[i+1]; } if(count==0) result[0]=order[0]; } } for(int i=0;i<6;i++) { cout<<result[i]<<endl; } return 0; }

三,源码(利用模板list)

#include <stdio.h> #include <list> #include <iostream> using namespace std; void raceResult(int** w, int* order, int* result, int n) { list<int> winer; int count = n; while(n) { winer.push_front(order[--n]); } int resultNum = count - 1; int nFirst, nSecond; int round = 1; while(winer.size() > 1) { //一轮开始 cout<<endl<<"round "<<round++<<endl; list<int>::iterator it = winer.begin(); while (it != winer.end()) { nFirst = *it; if (++it == winer.end()) { //轮空 cout<<nFirst<<" rest this round"<<endl; } else { nSecond = *it; //已经向后移动 int nWiner = *((int*)w + count * nFirst + nSecond);//取两个数交叉的数 if (nWiner == nFirst) { it = winer.erase(it); result[resultNum--] = nSecond; cout<<nFirst<<" kick out "<<nSecond<<endl; } else { it = winer.erase(--it); result[resultNum--] = nFirst; ++it; cout<<nSecond<<" kick out "<<nFirst<<endl; } } } } if (winer.size() == 1) { result[0] = winer.front(); } cout<<endl<<"final result: "; int nPlace = 0; while(nPlace < count) { :cout<<endl<<result[nPlace++]; } } void test() { //team 2>team 1>team 3>team 0>team 4>team 5 int w[6][6] = { 0,1,2,3,0,0, 1,1,2,1,1,1, 2,2,2,2,2,2, 3,1,2,3,3,3, 0,1,2,3,4,5 }; int order[6] = {1,3,4,2,0,5}; int result[6] = {-1}; raceResult((int**)w, order, result, 6); getchar(); } //自己加上主函数,测试了下,结果竟正确.. int main() { test(); return 0; }

///

round 1
1 kick out 3
2 kick out 4
0 kick out 5

round 2
2 kick out 1
0 rest this round

round 3
2 kick out 0

final result:
2
0
1
5
4
3
/

转载于:https://www.cnblogs.com/JPAORM/archive/2012/04/24/2510003.html

【100题】第三十六 比赛淘汰问题(谷歌笔试)相关推荐

  1. 【100题】第十六题(层序打印树的节点)

    一,题目:输入一颗二元树,从上往下按层打印树的每个结点,同一层中按照从左往右的顺序打印. 例如输入        8      /    \     6    10    / \     /  \ 5 ...

  2. 【100题】三十五 求一个矩阵中最大的二维矩阵(元素和最大)

    一,题目: 求一个矩阵中最大的二维矩阵(元素和最大).如: 1 2 0 3 4 2 3 4 5 1 1 1 5 3 0 中最大的是: 4 5 5 3 要求:(1)写出算法;(2)分析时间复杂度;(3) ...

  3. 深度学习核心技术精讲100篇(三十六)-EdgeRec:边缘计算在淘宝推荐系统中的大规模应用

    前言 在全面进入无线的时代,为了解决信息负载的问题,越来越多的推荐场景得到兴起,尤其是以列表推荐形式为主的信息流推荐.以手淘信息流为例,进入猜你喜欢场景的用户,兴趣常常是不明确的,用户浏览时往往没有明 ...

  4. AUTOSAR从入门到精通100讲(三十六)-AUTOSAR 通信服务两步走-CanSM概念-配置及代码分析

    CanSM概念 AUTOSAR CanSM模块的分享分为CanSM模块概念详解和CanSM模块配置及代码分析,具体的项目实战请关注本号的后续文章,本篇为CanSM模块的概念详解篇. 1 Introdu ...

  5. AUTOSAR从入门到精通100讲(三十六)-CAN总线错误处理

    一 背景 写这篇文章是因为我看到网上介绍CAN总线错误处理的文章,清一色的都是生搬照抄教科书或是数据文档的内容,特别是国内很难找到一些有价值的内容,这让一些真正有需要的人很苦恼,包括我自己.这篇不打算 ...

  6. 第七章第三十六题(游戏:八皇后问题)(Game: Eight Queens)

    第七章第三十六题(游戏:八皇后问题)(Game: Eight Queens) ***7.36(游戏:八皇后问题)经典的八皇后难题是要将八个皇后放在棋盘上,任何两个皇后都不能互相攻击(即没有两个皇后是在 ...

  7. 推荐系统三十六式——学习笔记(三)

    由于工作需要,开始学习推荐算法,参考[极客时间]->[刑无刀大牛]的[推荐系统三十六式],学习并整理. 3 原理篇之紧邻推荐 3.1 协同过滤 要说提到推荐系统中,什么算法最名满天下,我想一定是 ...

  8. 程序员编程艺术第三十六~三十七章、搜索智能提示suggestion,附近点搜索

    第三十六~三十七章.搜索智能提示suggestion,附近地点搜索 作者:July.致谢:caopengcs.胡果果. 时间:二零一三年九月七日. 题记 写博的近三年,整理了太多太多的笔试面试题,如微 ...

  9. -考研第三十六周总结-

    -考研第三十六周总结- [本周完成]✅ 上周计划表中所有内容 2011.2016年的数学卷子十七堂课的微分方程部分和泰勒部分 手机背单词每天 英语写16年英语卷子+讲解课程+细做了14年的阅读 政治看 ...

最新文章

  1. (字符串的处理4.7.22)POJ 3337 Expression Evaluator(解析C风格的字符串)
  2. win7配置计算机失败怎么办,电脑win7配置失败 还原更新 怎么处理 不要说重装
  3. (七)webStorage使用实例——webStorage作为简易数据库来使用
  4. 音视频技术开发周刊 | 230
  5. Spring Boot 2.X 来临,本文将带你起飞
  6. python基础语法_字符串编码
  7. 计算机进程调度论文,计算机操作系统小论文Linux进程调度.doc
  8. [FFmpeg] 编译官方例子
  9. 《流畅的Python》读书笔记——Python一等函数
  10. 第二章 吸取jQuery之选择器和包装集
  11. Cause: java.io.IOException: Could not find resource com/itheima/po/CustomerMapper.xml
  12. 5.12 利用图层蒙版制作图像合成特效 [原创Ps教程]
  13. 一些有意思的函数(连载中)
  14. Android 4.0 UI设计规范
  15. EXCEL中删除灰色边框的方法
  16. html帮助文档怎么翻译,HTML文档,HTML document,音标,读音,翻译,英文例句,英语词典
  17. 磁力搜 For magnetW常见问题
  18. JumpServer开源堡垒机完成龙芯架构兼容性认证
  19. ultraos win10启动盘_ultraiso制作u盘启动盘教程图文详解
  20. perp系列之六:perp工作截屏

热门文章

  1. ABySS非root权限安装
  2. 【PyQt】分析承载界面
  3. springboot文件上传下载实战 ——文件上传、下载、在线打开、删除
  4. 《Algorithms》Comparable 实现希尔排序
  5. shell条件检查原理:command echo ‘success‘ || echo ‘error‘
  6. OracleOraDb11g_home1TNSListener 服务启动后停止 某些服务在未由其他服务或程序使用时将自动停止
  7. php redis 封装类,php redis封装类
  8. as3位图绘制器(矢量器):as3potrace
  9. flex4的新数据类型ArrayList
  10. python常用模块用法_python笔记之常用模块用法分析