现在的位置: 首页 > 综合 > 正文

华为编程大赛–出圈问题

2019年04月09日 ⁄ 综合 ⁄ 共 1244字 ⁄ 字号 评论关闭

2、出圈问题(30分)

问题描述

M个人围成一圈报数,数到N1<N<10)的倍数或包含N这个数字时出圈,问剩下的最后一个人在原来的位置是多少?

报数规则:

1、从第一个人开始报数为1,下一个人报数为上一个人报数加1

2、报数的最大值为2000,如果报数超过2000,则下一个人重新从1开始报数

要求实现函数

int OutFunc (unsigned int iTotalNum, unsigned int iKey)

【输入】iTotalNum  
开始报数前的总人数, 0<iTotalNum<65535

       
iKey
    
  
题目中要求的数目N

【输出】无

【返回】剩下的人的原来的位置

示例

输入:iTotalNum =5, 
iKey =3

返回:4

 

输入:iTotalNum =15, 
iKey =3

返回:10

#include <cstdio>
#include <vector>
#include <algorithm>
using namespace std;

typedef struct PERSON
{
	PERSON(){};
	PERSON(unsigned int pos,unsigned int num):pos(pos),num(num){};
	unsigned int pos;
	unsigned int num;
}PSN;

int OutFunc (unsigned int iTotalNum, unsigned int iKey)
{
	vector<PSN> a;
	vector<PSN>::iterator it;
	for(unsigned int i = 0;i<iTotalNum;i++)
	{
		
	
		PSN p(i+1,((i+1)%2000)?((i+1)%2000):2000);
		a.push_back(p);

		
	}
	//printf("%d\n",a.size());
	int tmp;
	while(a.size() > 1)
	{
        tmp = a.size();
		for(it = a.begin();it!=a.end();it++)
	   {
		   if((it->num%iKey == 0) || (it->num%10 == iKey) || ((it->num/10)%10) == iKey ||(it->num/100)%10 == iKey || (it->num/1000)%10 == iKey)
		   {
			   
			   if(it != a.begin())
			   {
			       a.erase(it);
				   it--;
			   }
			   else
			   {
				   a.erase(it);
				   it = &a[tmp-2];
			   }
		   }
		   else
		   {
			   if(it == a.begin())
				   it->num = a.at(tmp-1).num+1;
			   else
			   {
				   it--;
				   unsigned int b = it->num;
				   (++it)->num = ((b+1)%2000)?((b+1)%2000):2000;
			   }
		   }
	   }
	}
 	return a.begin()->pos;
}


int main()
{
	unsigned int iTotalNum = 5;
	unsigned int iKey = 3;
	printf("%u\n",OutFunc(iTotalNum,iKey));
	return 0;
}

抱歉!评论已关闭.