打印斐波那契數列c程序

#include <stdio.h>
 int f(int n)
 {
     int a;
     if(n==1||n==2)
          a=1;
     else 
        a=f(n-1)+f(n-2);
     return a ;
 }
 

int main()
 {
     int n, i;
     printf("please input the month n:\n");
     scanf("%d",&n);
     for (i = 1; i<=n; i++)
     {
     
        printf(" %d ",f(i));
     }
     printf("\n");
} 

#include <stdio.h>
//打印斐波那契數列:關於斐波那契數列:每個數字都是其前兩個數字之和
int main()
{
	int n, i, un1, un2, un;
	for(n = 2;n<3;)
	{
		printf("please input number\n");//請輸入你想要返回多少個斐波那契數字
		scanf("%d", &n);
		if (n<3) printf("input error\n");
		printf("result is \n");
		printf(" 1 1 ");//
		un=un2=1;
		for(i=3;i<=n;i++)
		{
			un1 = un2;
			un2 = un;
			un = un1+un2;
			printf(i%10?"%d":"%d", un);
			printf("  ");
			if (i%10 == 0) printf("\n");
		}
		printf("\n");
	}
}
#include <stdio.h>
//打印斐波那契數列:關於斐波那契數列:每個數字都是其前兩個數字之和
int main()
{
	int n, i, un1, un2, un;
	for(n = 2;n<3;)
	{
		printf("please input number\n");//請輸入你想要返回多少個斐波那契數字
		scanf("%d", &n);
		if (n<3) printf("input error\n");
		printf("result is \n");
		printf(" 1 1 ");//
		un=un2=1;
		for(i=3;i<=n;i++)
		{
			un1 = un2;
			un2 = un;
			un = un1+un2;
			printf(i%10?"%d":"%d", un);
			printf("  ");
			if (i%10 == 0) printf("\n");
		}
		printf("\n");
	}
}


關於二叉樹:

http://blog.csdn.net/liuzhanchen1987/article/details/7324935

關於赫夫曼樹:

http://www.cnblogs.com/younes/archive/2010/08/09/1796049.html

關於滿二叉樹和完全二叉樹:

http://www.cnblogs.com/ly0311/archive/2012/09/16/2687868.html


iostream是c++的頭文件,需要使用g++指令而不是gcc
//哈夫曼樹算法
#include<iostream>
using namespace std;
const int n=5;
const int m=2*n-1;
const int float_max=20;
typedef int datatype;
typedef struct 
{
	float weight;			//定義權重
	int parent;				//定義雙親在向量中的下標
	int lchild,rchild;		//定義左右子樹
}	nodetype;				//結點類型
typedef nodetype hftree[m]; //哈夫曼樹類型,數組從0號單元開始使用
hftree T;					//哈夫曼樹向量

//哈夫曼樹的構造
void huffman(hftree T)
{
	int i,j,p1,p2;
	float small1,small2;
	for(i=0;i<n;i++)        //初始化
	{
		T[i].parent=-1;		//無雙親,即爲根結點,尚未合併過
		T[i].lchild=T[i].rchild=-1;//左右孩子指針置爲-1
	}
	for(i=0;i<n;i++)
	{
		cin>>T[i].weight;   //輸入n個葉子的權
	}
	for(i=n;i<m;i++)		//進行n-1次合併,產生n-1個新結點
	{
		p1=p2=-1;
		small1=small2=float_max;	//float_max爲float類型的最大值
		for(j=0;j<=i-1;j++)
		{
			if(T[j].parent!=-1) continue;//不考慮已合併過的點
			if(T[j].weight<small1)			//修改最小權和次小權及位置
			{
				small2=small1;
				small1=T[j].weight;
				p2=p1;
				p1=j;
			}
			else if(T[j].weight<small2)		//修改次小權及位置
			{
				small2=T[j].weight;
				p2=j;
			}
		}
		T[p1].parent=T[p2].parent=i;		//新根
		T[i].parent=-1;
		T[i].lchild=p1;
		T[i].rchild=p2;
		T[i].weight=small1+small2;			//新結點的權值爲最小權與次小權之和
	}
}

int main()
{
	hftree T;
	cout<<"          歡迎測試!!!!   "<<endl;
	cout<<"----------測試開始-----------"<<endl;
	cout<<"首先調用哈夫曼樹構造的函數:huffman"<<endl;
	cout<<"按下標順序輸入葉子的權重:"<<endl;
	cout<<" 0  1  2  3  4  5  6  7  8 "<<endl;
	huffman(T);
	cout<<"輸出合併後的哈夫曼樹的所有結點:"<<endl;
	cout<<" 0  1  2  3  4  5  6  7  8 "<<endl;
	for(int i=0;i<m;i++)
	{ 
		cout<<T[i].weight<<"  ";
	}
	system("pause");
	return 0;
}


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章