#include <stdio.h>
void mysort(int *a, int size)
{
int i, j, k, t
for (i = 0i <size - 1++i)
{
k = i
for (j = ij <size++j)
{
if (a[k] >a[j])
k = j
}
if (k != i)
{
t = a[i]
a[i] = a[k]
a[k] = t
}
}
}
void main()
{
int num, a[4], max, min
do
{
scanf("%d", &num)/* 输入最初的4位数 */
} while (num <1000 || num >9999)
while (num)
{
/* 将4位数分解成4个独立的数字,并保存在a数组中 */
a[0] = num % 10
num /= 10
a[1] = num % 10
num /= 10
a[2] = num % 10
num /= 10
a[3] = num
/* 排序数组元素 */
mysort(a, 4)
/* 从数组的4个元素中产生最大数字和最小数字 */
max = a[3] * 1000 + a[2] * 100 + a[1] * 10 + a[0]
min = a[0] * 1000 + a[1] * 100 + a[2] * 10 + a[3]
/* 产生新的4位数 */
num = max - min
printf("%d\n", num)
if (num == 6174)
break
}
printf("The magic number is: %d\n", num)
}
#include "stdio.h"#include "ctype.h"
#include "malloc.h"
#include "stdlib.h"
#define M 100
#define ZERO 0
#define SUCC 1
#define DEFT 0
#define MIN -1
#define MAX 2001
typedef int valuetype
typedef struct Bnode
{
valuetype data
int layer
struct Bnode *Lson,*Rson
}Bnode,*Bptr
void writeT(Bptr root)
{
int first=0,last=1
Bptr p,q[M]
if(root->data==MIN)p=root->Rson
else p=root->Lson
if(p==NULL)
{ printf(" 当前二叉树为空,没有结点。\n")return}
printf(" 当前二叉树的结点为:\n")
printf(" 层号 当前结点 左儿子右儿子\n")
p->layer=1
q[0]=p
while(first!=last)
{
p=q[first++]
printf("%6d%10d ",p->layer,p->data)
if(p->Lson==NULL)printf("%12c",'\040')
else
{
printf("%12d",p->Lson->data)
p->Lson->layer=p->layer+1
q[last++]=p->Lson
}
if(p->Rson!=NULL)
{
printf("%12d",p->Rson->data)
p->Rson->layer=p->layer+1
q[last++]=p->Rson
}
printf("\n")
}
}
void inorder(Bptr p)
{
if(!p)return
inorder(p->Lson)
printf("%5d",p->data)
inorder(p->Rson)
}
void sortT(Bptr root)
{
if(root->data==MIN) inorder(root->Rson)
else inorder(root->Rson)
printf("\n")
}
Bptr search (valuetype x,Bptr p)
{
while (p!=NULL)
{
if(x==p->data)return p
if(x<p->data) p=p->Lson
else p=p->Rson
}
return NULL
}
void searchT(Bptr root)
{
int x
printf("请输入要查找的结点值x>0,x=")
scanf("%d",&x)
if(search(x,root)==NULL)printf("数中没有%d!\n",x)
else printf("%d 已经找到!\n",x)
}
void insert(valuetype x,Bptr &root)
{
Bptr f,p
f=NULLp=root
while(p!=NULL)
{
if(x<p->data)f=p,p=p->Lson
else f=p,p=p->Rson
}
p=new Bnode
p->data=xp->Lson=p->Rson=NULL
if(f==NULL)root=p
else
if(x<=f->data)f->Lson=p
else f->Rson=p
}
void insertT(Bptr p)
{
int x
printf("请输入要插入的结点的值x>0,x=")
scanf("%d",&x)
insert(x,p)
printf("%d已经被插入了\n",x)
}
Bptr creatST()
{
Bptr root valuetype x
root =NULL
printf(" 构造初始检索树,请输入元素序列,元素个数不得超过%d,要求:\n",M)
printf("序列以%d或%d开始,以0结束,元素值均为小于%d的正整数\n",MIN,MAX,MAX)
scanf("%d",&x)
while(x!=ZERO)
{
insert(x,root)
scanf("%d",&x)
}
return root
}
int deleteST(valuetype x,Bptr root)
{
Bptr f,p,s,r
for (p=root)
{
if(p==NULL)return DEFT
if (x==p->data)break
if(x<p->data)
{
f=pp=p->Rson
}
else
{
f=pp=p->Rson
}
}
if (p->Rson==NULL)
{
if(p==f->Lson)
f->Lson=p->Rson
else
f->Rson=p->Lson
free (p)
return SUCC
}
s=p->Lson
if (s->Rson==NULL)
{
p->data=s->data
p->Lson=s->Lson
free (s)
return SUCC
}
r=s->Rson
while (r->Rson!=NULL)
{
s=r
r=r->Rson
}
p->data=r->data
s->Rson=r->Lson
free (r)
return SUCC
}
void deleteT(Bptr root)
{
int x
printf("请输入要删除的结点值x>0,x=")
scanf("%d",&x)
if(deleteST(x,root))
printf("%d 已经被删除!\n",x)
else
printf(" %d不在树中,无法删除!\n",x)
}
char getalpha()
{
char c
while(1)
{
c=getchar()
if(isalpha(c))
return c
}
}
void treeT(Bptr root)
{
char c
printf(" 对检索树可以进行下列 *** 作:\n")
while (1)
{
printf("请输入 *** 作码:查找F/f 插入I/i 删除D/d 显示P/p 结点排序S/s 终止E/e\nC=")
c=getalpha()
switch(c)
{
case 'f':
case 'F': searchT(root)break
case 'p':
case 'P': writeT(root)break
case 'i':
case 'I': insertT(root)break
case 'd':
case 'D': deleteT(root)break
case 's':
case 'S': sortT(root)break
case 'e':
case 'E': writeT(root)return
default:printf("输入的 *** 作码不正确,请重新输入!\n")
continue
}
}
}
void main()
{
Bptr root
root=creatST()
treeT(root)
printf("程序结束,再见!\n")
}
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)