Donghua oj- Advanced Topics in 27 questions

Here Insert Picture Description

27 Joseph Central

Author: ZhouMingLiang time limit: 10S chapters: one-dimensional array

Problem Description :

On one occasion, the company held a year-end party obviously. The climax of the year-end party never want to be the last link. To increase the activity of the company, did not follow the traditional way of lottery draw, but had a game: step by step to eliminate people, while the last remaining people will get prizes.

This game is as follows: First of all the company's employees in a circle, and then determine a number out of X, and then they start from one of them, from 1 count, when the count to X, that person would be eliminated, then the next person and then from 1 count, has been so repeated until the last remaining person, that person is the final grand prize winner.

For example, the company has five people, out of a number set to 2, the beginning of five people arranged in a circle, are numbered: 1,2,3,4,5;
First, from the number of people began to count 1, count 2 after elimination number 2, so that only four of: 1,3,4,5;
then begins to count the number of person 3, to the number 2, number 4 out, so that only three individuals: 1,3, 5;
then begins to count the number of 5 people, the number 2, the number 1 out, so that only two individuals: 3,5;
final number from 3 people start counting, the count of 2, 5-out number, the last number for that person to get 3 of the final award. (Note: The above sequence is eliminated 3. 2415)

Because obviously very luck to worse, and finally the second to be eliminated, missed the awards, very depressed. He wanted to know the whole process that they have been eliminated, so he would like to ask you a favor, to help him write a program, obviously the number of people he's told you, and put that number also eliminated tell you that you can program according to two DETAILED sequence number of people out of the calculated, i.e. the number of people out of the output sequence.

Obviously the question boils down to: give you a firm number N and eliminated a number X, way out of your program simulation described above, the output of the number of people out of order. Enter a description:

你写的程序要求从标准输入设备中读入测试数据作为你所写程序的输入数据。标准输入设备中有多组测试数据,每组测试数据仅一行,每组测试数据有两个整数N(1<N<100)和X(0<X<10),N表示公司的人数,X表示淘汰数,两个整数用一个空格隔开。每组测试数据与其后一组测试数据之间没有任何空行,第一组测试数据前面以及最后一组测试数据后面也都没有任何空行。
输出说明 :

对于每一组测试数据,你写的程序要求计算出一组相应的运算结果,并将这一组运算结果作为你所写程序的输出数据依次写入到标准输出设备中。每组运算结果为N个整数,即淘汰人的编号的顺序,每个数之间用一个空格隔开。每组运算结果单独形成一行数据,其行首和行尾都没有任何空格,每组运算结果与其后一组运算结果之间没有任何空行,第一组运算结果前面以及最后一组运算结果后面也都没有任何空行。
注:通常,显示屏为标准输出设备。 输入范例 : 5 2 5 6 99 1 输出范例 : 2 4 1 5 3 1 3 2 5 4 1
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50
51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73
74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96
97 98 99

代码:

/*
	约瑟夫环 
*/ 

#include<stdio.h>
#define MAX_SIZE 102

typedef struct Person {
	int num;// 人员编号
	struct Person* next;
} Person;

int main() {
	int N = 0, X = 0;
	int i = 0;
	Person* head = (Person* )malloc(sizeof(Person));
	Person* person = NULL;// 用于新建结点 
	Person* rear = head;// rear始终指向链表尾部 
	Person* p = NULL;// 工作指针 
	Person* pre = NULL;// 工作指针前驱 
	Person* temp = NULL;// 用于暂存被删除结点 
	int outCount = 0;// 出环人员数量 
	int count = 0; 
	head -> next = NULL;
	
	while (scanf("%d%d", &N, &X) != EOF) {
		head -> next = NULL;// 每次开始前重置数据 
		rear = head;
		outCount = 0;
		
		for (i = 1; i <= N; i++) {// 建立链表 
			person = (Person* )malloc(sizeof(Person));
			person -> num = i;
			person -> next = NULL;
			rear -> next = person;
			rear = person; 
		}/* 建表过程没问题*/
		rear -> next = head -> next;// 构成循环链表
		
		p = head -> next;
		pre = head; 
		count = 1;// 开始计数 
		while (1) {
			if (count == X) {
				temp = p;
				if (outCount == 0) 
					printf("%d", temp -> num);
				else 
					printf(" %d", temp -> num);
				pre -> next = p -> next;// 删除结点 
				p = p -> next; 
				free(temp); 
				count = 1;// 重置计数 
				if (++outCount == N) {
					printf("\n");
					break;
				}
			}
			else {
				count++;
				pre = p;
				p = p -> next;
			}
		} 
	}
	
	return 0;
}

Here Insert Picture Description
做的过程中忘记重置了,结果后面死循环……

Published 44 original articles · won praise 6 · views 7549

Guess you like

Origin blog.csdn.net/qq_41409120/article/details/104501538