PTA linear table 7-1 Josephus problem (by Yan) (100 points) Output the number of each person in the order of listing

 

 

                              7-1 Josephus question (by Yan) (100 points)

N people numbered 1, 2, ..., n sit around a round table in a clockwise direction, each holding a password (a positive integer). At the beginning, choose a positive integer m as the upper limit of the number of reports, starting from the first person in a clockwise direction from 1 to report the number, stop reporting the number when you report to m, and the person who reports to m will be listed and his password As the new value of m, the next person in his clockwise direction starts counting from 1 again, and the person who counts to m goes out again; and so on, until all the people around the round table go out. It is required to output the numbers of n individuals in the order listed.

Input format:

Enter two integers in the first line, indicating the number of people n and the initial password m in sequence, separated by spaces. On the second line, enter n integers in turn, representing the passwords of n individuals, separated by spaces.

Output format:

Output the number of each person in listed order, separated by spaces.

Input sample:

Here is a set of inputs. E.g:

7 20
3 1 7 2 4 8 4 

Sample output:

The corresponding output is given here. E.g:

6 1 4 7 2 3 5 

My knowledge

My summary of PTA:
                                (1).PTA requires very high output format

                                (2). If the answer can be output normally in the compiler, the answer is wrong on the PTA, then you should try to add a getchar statement after the scanf statement or check whether it is your own code. Some places are really not considered .

Code part (If you don’t understand that, please leave a message below and I will reply as soon as possible. If there is a problem with the code, please point it out)

code show as below: 

#include <stdio.h>
#include <malloc.h>
typedef struct set{
    int password;
	int num;
    struct set * next;
}Node;
int main (void)
{   int n,m;
	int i,j = 1,k=1;
	int flag = 1;
	int a[1000];
    Node head,*phead,*p0,*p1;
	phead = NULL;
	scanf("%d %d",&n,&m);
	getchar();
	//创建链表的部分
	//创建的是一个循环链表
	for (i = 0;i<n;i++){
		if(phead == NULL){
			phead = &head;
			phead->num = j;//存编号
			p0 = phead;
			phead->next = phead;
			
		}
		else{
			p1 = (Node*)malloc(sizeof(Node));//动态创建一个结构体
			p1->next = p0;
			phead->next = p1;
			phead = p1;
			phead->num = j;
		}
		scanf("%d",&phead->password);
		j++;//更新编号
	}
	i = 0;
	//思路,循环到编号num处,则将其编号存入一个数组中
	//然后把编号的值改为0,继续进行循环
	//退出条件:数组的元素个数和链表的个数相等
	while(p0){
		if(p0->num == 0){
			p0 = p0->next;
			continue;
		}
		if(k == m && p0->num!= 0){
			a[i] = p0->num;
			m = p0->password;
			p0->num = 0;
			i++;
			k = 0;
			
		}
		k++;
		if(i == (j - 1))
			break;
		p0 = p0->next;
	}
	//输出部分
	for(j = 0;j<i;j++)
		printf("%d ",a[j]);
}

 

              

This is the end of this sharing, thank you! !

Guess you like

Origin blog.csdn.net/L_Z_jay/article/details/108751478