c++敢死队问题代码(为大一而生)
有M個敢死隊員要炸掉敵人的一碉堡,誰都不想去,排長決定用輪回數數的辦法來決定哪個戰士去執行任務。如果前一個戰士沒完成任務,則要再派一個戰士上去。現給每個戰士編一個號,大家圍坐成一圈,隨便從某一個戰士開始計數,當數到5時,對應的戰士就去執行任務,且此戰士不再參加下一輪計數。如果此戰士沒完成任務,再從下一個戰士開始數數,被數到第5時,此戰士接著去執行任務。以此類推,直到任務完成為止。
排長是不愿意去的,假設排長為1號,請你設計一程序,求出從第幾號戰士開始計數才能讓排長最后一個留下來而不去執行任務。
要求:至少采用三種不同的數據結構的方法實現。
1.數組方法
#include<stdio.h>
struct some{
?? ?int num;};
?? ?
void search(int *b,int M){
?? ?int i,j=0,p=0;
?? ?while(M-p>1){
?? ? ? for(i=0;i<M;i++) ?
?? ??? ?if(*(b+i)!=0){
?? ??? ??? ?j++;
?? ??? ??? ?if(j%5==0){
?? ??? ??? ?p++; ?
?? ??? ??? ?*(b+i)=0;}
?? ??? ?} ?
?? ?}
?? ?for(i=0;i<M;i++) ? ??
?? ?if(*(b+i)!=0){
?? ??? ?int a=*(b+i);
?? ??? ?int b=M-a+2;
?? ??? ??? ?printf("從第%d個人開始計數",b%M);}
}
int main(){
?? ?int M,i,q=1;
?? ?struct some a[100];?
?? ?printf("請輸入戰士人數M:");
?? ?scanf("%d",&M);
?? ?for(i=0;i<M;i++,q++)
?? ?a[i].num=q;
?? ?search(&a[0].num,M);
?? ?return 0;?
}
2.循環單鏈表
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<malloc.h>
typedef struct node{
?? ?int data;
?? ?struct node *next ;
}LNode;
LNode *Creat(int n){
?? ?LNode *s,*q,*T;
?? ?int i;
?? ?if(n!=0){
?? ??? ?T=q=(LNode *)malloc(sizeof(LNode));
?? ??? ?q->data=1;
?? ??? ?for(i=2;i<=n;i++){
?? ??? ??? ?s=(LNode*)malloc(sizeof(LNode));
?? ??? ??? ?q->next=s;
?? ??? ??? ?q->next->data=i;
?? ??? ??? ?q=q->next;
?? ??? ?}?? ?
?? ??? ?q->next=T;
?? ?}
?? ?return T;}?
Delete (LNode *T){?
?? ?LNode *a;
?? ?int i;
?? ?while(T->next!=T){
?? ??? ?for(i=1;i<4;i++)
?? ??? ??? ?T=T->next;
?? ??? ?a=T->next;
?? ??? ?T->next=a->next;
?? ??? ?free(a);
?? ??? ?T=T->next ;
?? ?}
?? ?printf("\n");
?? ?return (T->data );
}
int main(){
?? ?int s,i,count=0;
?? ?LNode *p;
?? ?int z ,y,e=1,c=1,M;
?? ??? ?printf("請輸入敢死隊的人數:");
?? ??? ?scanf("%d",&M);
?? ??? ?if(M<1){
?? ??? ??? ?printf("輸入有誤重新輸入\n1");
?? ??? ?}
?? ??? ??? ?else{
?? ??? ??? ?p=Creat(M);
?? ??? ??? ?y=Delete(p);
?? ??? ??? ?z=M-y+2;
?? ??? ??? ?if(z%M==0)
?? ??? ??? ??? ?printf("從第%d號開始計數\n",z);
?? ??? ??? ?else
?? ??? ??? ??? ?printf("從第%d號開始計數\n",(M-y+2)%M);}}
3.循環隊列
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<malloc.h>
#define QueueSize 1000
typedef struct{
?? ?int data[QueueSize];
?? ?int front;
?? ?int rear;
?? ?int count;?
}SqQueue;
void Initial(SqQueue *Q)
{
?? ?Q->front=Q->rear=0;
?? ?Q->count=0;
}
int IsEmpty(SqQueue *Q)
{
?? ?return Q->front==Q->rear;
}
int Full(SqQueue *Q)
{
?? ?return Q->rear==QueueSize-1+Q->front;
}
void EnQueue(SqQueue *Q,int x)
{
?? ?if (Full(Q))
?? ?{
?? ??? ?printf("隊列上溢");?
?? ?}
?? ?Q->data[Q->rear]=x;?
?? ?Q->rear=(Q->rear+1)%QueueSize;?
?? ?Q->count ++;?
}
int DeQueue(SqQueue *Q)
{
?? ?int t;
?? ?if(IsEmpty(Q))
?? ?{
?? ??? ?printf("隊列為空");?
?? ?}
?? ?t=Q->data[Q->front];
?? ?Q->front=(Q->front+1)%QueueSize;?
?? ?Q->count--;?
?? ?return t;
}
int main()
{
?? ?int i,count=0,z,y,e=1,c=1,M,a,j;
?? ?SqQueue s;
?? ??? ?printf("請輸入敢死隊的人數 :");
?? ??? ?scanf("%d",&M);
?? ??? ?for(a=1;a<=M;a++)
?? ??? ??? ?{
?? ??? ??? ??? ?Initial(&s);
?? ??? ??? ??? ?for(i=1;i<=M;i++)
?? ??? ??? ??? ?{
?? ??? ??? ??? ??? ?EnQueue(&s,i);
?? ??? ??? ??? ?}
?? ??? ??? ??? ?for(i=1;i<a;i++)
?? ??? ??? ??? ?{
?? ??? ??? ??? ??? ?j=DeQueue(&s);
?? ??? ??? ??? ??? ?EnQueue(&s,j);
?? ??? ??? ??? ?}
?? ??? ??? ??? ?count=1;
?? ??? ??? ??? ?while(count<M)
?? ??? ??? ??? ?{
?? ??? ??? ??? ??? ?for(i=1;i<5;i++)
?? ??? ??? ??? ??? ?{
?? ??? ??? ??? ??? ??? ?j=DeQueue(&s);
?? ??? ??? ??? ??? ??? ?EnQueue(&s,j);
?? ??? ??? ??? ??? ?}
?? ??? ??? ??? ?j=DeQueue(&s);
?? ??? ??? ??? ?count++;
?? ??? ??? ??? ?}
?? ??? ??? ??? ?if(s.data[s.front]==1)
?? ??? ??? ??? ??? ?break;
?? ??? ??? ?}
?? ??? ??? ?printf("從第%d號開始計數能讓排長最后一個留下。",a);
}
4。三合一版本
#include<stdio.h>
#include<stdlib.h>
//敢死隊員結構體
struct person{
? ? int flag;//0表示去執行任務
};
int array() {
? ? int M, i;
? ? struct person persons[100];//敢死隊員數組,敢死隊員編號為數組下標加1
? ? printf("請輸入敢死隊人數: ");
? ? scanf("%d", &M);
? ? for (i = 0; i < M; i++){
? ? ? ? persons[i].flag = 1;
? ? }
? ? int cur=0;//當前去執行任務的人數
? ? int count=0;//計數
? ? //假設從1號開始計數
? ? for (i = 0; cur<M-1; i++) {
? ? ? ? //從頭開始計數
? ? ? ? if(i==M){
? ? ? ? ? ? i=0;
? ? ? ? }
? ? ? ? //如果當前隊員沒有去執行任務
? ? ? ? if(persons[i].flag==1){
? ? ? ? ? ? count++;
? ? ? ? ? ? //每次計數5次,令當前隊員去執行任務
? ? ? ? ? ? if(count%5==0){
? ? ? ? ? ? ? ? cur++;
? ? ? ? ? ? ? ? persons[i].flag=0;
? ? ? ? ? ? }
? ? ? ? }
? ? }
? ? int result=0;
? ? //數組中只剩下一人沒去執行任務
? ? for (i = 0; i < M; i++){
? ? ? ? if(persons[i].flag==1){
? ? ? ? ? ? result -= i;
? ? ? ? ? ? if(result<0){
? ? ? ? ? ? ? ? result += M;
? ? ? ? ? ? }
? ? ? ? ? ? break;
? ? ? ? }
? ? }
? ? printf("從第%d個人開始計數",result+1);
? ? return 0;
}
?//鏈表節點表示隊員
?struct node {
? ? int data;//隊員編號
? ? struct node *next;//指向下一個編號隊員
};
//假設從1號開始計數,返回最后一個留下的隊員編號
int fun(node *head, int M) {
? ? node *temp;
? ? int count;//計數
? ? int cur=0;//當前執行任務人數
? ? while (cur < M - 1) {
? ? ? ? for (count = 1; count < 4; count++)
? ? ? ? ? ? head = head->next;
? ? ? ? cur++;
? ? ? ? //刪除當前節點
? ? ? ? temp = head->next;
? ? ? ? head->next = temp->next;
? ? ? ? free(temp);
? ? ? ? head = head->next;
? ? }
? ? return head->data;
}
int cycle_single_list() {
? ? node *head;//鏈表頭結點
? ? int M;
? ? printf("請輸入敢死隊的人數:");
? ? scanf("%d", &M);
? ? //創建鏈表
? ? node *newNode, *pre;
? ? int i;
? ? head = pre = (node *) malloc(sizeof(node));
? ? pre->data = 1;
? ? for (i = 2; i <= M; i++) {
? ? ? ? newNode = (node *) malloc(sizeof(node));
? ? ? ? pre->next = newNode;
? ? ? ? pre->next->data = i;
? ? ? ? pre = pre->next;
? ? }
? ? pre->next = head;
? ? int id = fun(head, M);
? ? int result = 1 - (id - 1);
? ? if(result<1){
? ? ? ? result+=M;
? ? }
? ? printf("從第%d號開始計數",result);
}
//隊列
typedef struct {
? ? int array[1000];//循環隊列數組
? ? int size;//循環隊列大小
? ? int front;//循環隊列頭部位置
? ? int back;//循環隊列尾部位置
} queue;
//入隊
void push(queue *a, int data) {
? ? a->array[a->back] = data;
? ? a->back = (a->back + 1) % 1000;
? ? a->size++;
}
//獲取隊首元素
int front(queue *a){
? ? return a->array[a->front];
}
//出隊
void pop(queue *a) {
? ? a->front = (a->front + 1) % 1000;
? ? a->size--;
}
//假設從1號開始計數,返回最后一個留下的隊員編號
int fun(queue *a) {
? ? int count;//計數
? ? while (a->size>1){
? ? ? ? for (count = 0; count < 4; count++) {
? ? ? ? ? ? int first= front(a);
? ? ? ? ? ? pop(a);
? ? ? ? ? ? push(a,first);
? ? ? ? }
? ? ? ? pop(a);
? ? }
? ? return front(a);
}
int cycle_queue() {
? ? int M;
? ? queue q;
? ? printf("請輸入敢死隊的人數 :");
? ? scanf("%d", &M);
? ? //初始化隊列
? ? q.front=q.back=q.size=0;
? ? //創建隊列
? ? int i;
? ? for (i = 0; i < M; i++) {
? ? ? ? push(&q,i+1);
? ? }
? ? int id = fun(&q);
? ? int result = 1 - (id - 1);
? ? if(result<1){
? ? ? ? result+=M;
? ? }
? ? printf("從第%d號開始計數",result);
}
int main() {
? ? int num;
? ? while (1) {
? ? ? ? printf("1.數組實現\n");
? ? ? ? printf("2.循環單鏈表實現\n");
? ? ? ? printf("3.循環隊列實現\n");
? ? ? ? scanf("%d", &num);
? ? ? ? if (num == 0) {
? ? ? ? ? ? printf("輸入錯誤,請重試\n");
? ? ? ? } else if(num == 1) {
? ? ? ? ? ? return array();
? ? ? ? } else if(num == 2) {
? ? ? ? ? ? return cycle_single_list();
? ? ? ? } else if(num == 3) {
? ? ? ? ? ? return cycle_queue();
? ? ? ? }
? ? }
}
總結
以上是生活随笔為你收集整理的c++敢死队问题代码(为大一而生)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 因特网中的域名服务器系统负责全网IP,因
- 下一篇: 持续不定期更新:CFDC++之拟一维喷管