华为上机题,循环报数

题目我有点记不清了,就是先输入一串这样的字符串,每个字符之间以空格隔开:3 a b c d e f g h i

第一个数字m,后面的字符每数m个,就输出,输出后从序列中删除,然后循环输出:

c f i d h e b g a。

题目有个提示是运用循环链表。


#include <cstdio>
#include <string>
#include <malloc.h>
using namespace std;

typedef struct list
{
	char c;
	list *next;
}List;

int main()
{
	char input[100];
	int i,length,total = 0;
	List *head, *p, *q;
	head = (List*)malloc(sizeof(List));
	p = q = head;
	gets(input);
	length = strlen(input);
	head->c = input[0];
	for(i = 2;i<length;i+=2)
	{
		p = (List*)malloc(sizeof(List));
		p->c = input[i];
		q->next = p;
		q = p;
		total++;
	}
	p->next = head->next;

	/*p = head->next;
	for(i = 0;i<total;i++)
	{
		printf("%c ",p->c);
		p = p->next;
	}*/

	p = head->next;
	while(total!=1)
	{
		for(i=1;i<(head->c)-'0'-1;i++)
		{
			p = p->next;
		}
		printf("%c",p->next->c);
		q = p->next;
		p->next = p->next->next;
		free(q);
		p = p->next;
		total--;
		printf(" ");
	}
	printf("%c",p->c);
	return 0;
}


猜你喜欢

转载自blog.csdn.net/qugename/article/details/21987797