魔術師發牌問題

           魔術師利用一副牌中的13張黑牌,預先將他們排好後疊放在一起,牌面朝下。對觀衆說:“我不看牌,只數數就可以猜到每張牌是什麼,我大聲數數,你們聽,不信?現場演示。”魔術師將最上面的那張牌數爲1,把他翻過來正好是黑桃A,將黑桃A放在桌子上,第二次數1,2,將第一張牌放在這些牌的下面,將第二張牌翻過來,正好是黑桃2,也將它放在桌子上這樣依次進行將13張牌全部翻出,準確無誤。


#include<stdio.h>
#include<malloc.h>
	struct node
	{
		 int data;     //elementype表示一種數據類型,可能是int/char等等
		 struct node *next;   //next 指針,用於鏈表結構指向下一個節點
	};
	typedef struct node node; //重定義struct node類型爲node

	node* Creat(int n);
	void Magician(node *head);
	int main()
	{
		int n = 13;
		node*head = Creat(13);
		node*p = NULL;
		p = head;
		Magician(head);
		while(n)
		{
			printf("%d->",p->data);
			p = p->next;
			n--;
		}
		return 0;
	}
	node* Creat(int n)
	{
		node *p = NULL;
		node *head;
		head =(node *)malloc(sizeof(node));
		p = head;
		node *s;
		int i = 1;
		if(0 != n)
		{
			while(i <= n)
			{
				s = (node*)malloc(sizeof(node));
				s->data = 0;
				p->next = s;
				p = s;
				i++;
			}
			s->next = head->next;
		}
		free(head);
		return s->next;
	}

	void Magician(node *head)
{
	node* p;
	int j;
	int Count = 2;

	p = head;
	p->data = 1;
	while(1)
	{
		for(j = 0;j < Count;j++)
		{
			p = p->next;
			if(p->data != 0)
			{
				j--;
			}
		}
		if(p->data == 0)
		{
		p->data = Count;
		Count++;
		if(Count == 14)
		{
			break;
		}
		}
	}
}

運行結果:


發佈了41 篇原創文章 · 獲贊 3 · 訪問量 2萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章