奇安信笔试题目1.结束进程树2.队尾幸运编号
1.结束进程树
BFS就ok了
代码:
#include <iostream>
#include <vector>
#include<queue>
using namespace std;
int main(void)
{
int temp;
vector<int > num;
vector<int > num1;
vector<int > num2;
while (cin >> temp)
num.push_back(temp);
queue<int> mm;
mm.push(num[num.size() - 1]);//要删除哪个进程,
num.pop_back();
int count = 1;
for (int i = 0; i < num.size(); i++)
{
if (i < num.size()/2)
num1.push_back(num[i]);
else
num2.push_back(num[i]);
}
if(find(num2.begin(),num2.end(),mm.front())==num2.end())
{
cout<<0;
return 0;
}
while (!mm.empty())
{
int cnt = mm.front();//输出进程
mm.pop();
for (int i = 0; i < num2.size(); i++)
{
if (num2[i] == cnt)
{
count++;
mm.push(num1[i]);
}
}
}
cout << count;
return 0;
}
2.队尾幸运编号
约瑟夫问题
代码:
#include<iostream>
#include<vector>
using namespace std;
int main()
{
int n;
cin>>n;
if(n<=1)
{
cout<<1;
return 0;
}
int res=0;
vector<int>a(n,1);
for(int i=0;i<n;++i)
a[i]=i+1;
int j=0;
while(a[n-1]!=0)
{
for(int i=0;i<n;++i)
{
if(a[i]==0)
continue;
else
j++;
if(j==5)
{
a[i]=0;
j=0;
res++;
}
}
}
cout<<res;
system("pause");
return 0;
}