#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<conio.h>a
#include<graphics.h>
#define MAXVALUE 200 //权值的最大值
#define MAXBIT 30 //最大的编码位数
#define MAXNODE 30 //初始的最大节点数
struct haffnode
{
char data;
int weight;
int flag;
int parent;
int leftchild;
int rightchild;
};
struct haffcode
{
int bit[MAXNODE];
int start;
char data;
int weight;
};
void pprintf(struct haffcode haffcode[], int n);//输出函数
void haffmantree(int weight[], int n, struct haffnode hafftree[], char data[]);//建立哈夫曼树
void haffmancode(struct haffnode hafftree[], int n, struct haffcode haffcode[]);//求哈弗曼编码
void test(struct haffcode haffcode[], int n);//测试函数
void end();//结束界面函数
void haffmantree(int weight[], int n, struct haffnode hafftree[], char data[])
{
int i, j, m1, m2, x1, x2;
for (i = 0; i < 2 * n - 1; i++)//哈夫曼树初始化,n个叶节点共有2n-1个节点
{
if (i < n)
{
hafftree[i].data = data[i];
hafftree[i].weight = weight[i];//叶节点
}
else
{
hafftree[i].weight = 0;//费叶节点
hafftree[i].data = '\0';
}
hafftree[i].parent = 0;
hafftree[i].leftchild = -1;
hafftree[i].rightchild = -1;
}
for (i = 0; i < n - 1; i++)
{
m1 = m2 = MAXVALUE;
x1 = x2 = 0;
for (j = 0; j < n; j++)
{
if (hafftree[j].weight < m1&&hafftree[j].flag == 0)
{
m2 = m1;
x2 = x1;
m1 = hafftree[j].weight;
x1 = j;
}
else if (hafftree[j].weight < m2&&hafftree[j].flag == 0)
{
m2 = hafftree[j].weight;
x2 = j;
}
}
hafftree[x1].parent = n + i;
hafftree[x2].parent = n + i;
hafftree[x1].flag = 1;
hafftree[x2].flag = 1;
hafftree[n + i].weight = hafftree[x1].weight + hafftree[x2].weight;
hafftree[n + i].leftchild = x1;
hafftree[n + i].rightchild = x2;
}
}
void haffmancode(struct haffnode hafftree[], int n, struct haffcode haffcode[])
{
int i, j, child, parent;
struct haffcode newcode;
struct haffcode* cd;
cd = &newcode;
for (i = 0; i < n; i++)
{
cd->start = MAXBIT - 1;
cd->weight = hafftree[i].weight;
cd->data=hafftree[i].data;
child = i;
parent = hafftree[child].parent;
while (parent != 0)
{
if (hafftree[parent].leftchild == child)
cd->bit[cd->start] = 0;
else
cd->bit[cd->start] = 1;
cd->start--;
child = parent;
parent = hafftree[child].parent;
}
for (j = cd->start + 1; j < MAXBIT; j++)
haffcode[i].bit[j] = cd->bit[j];
haffcode[i].data = cd->data;
haffcode[i].start = cd->start;
haffcode[i].weight = cd->weight;
}
}
void pprintf(struct haffcode myhaffcode[], int n)
{
int i, j, count = 0;
system("clear");//清屏
for (i = 0; i < n; i++)
{
textcolor(YELLOW);//TEXTCOLOR是graphics.h中的函数,只有TC支持,VC++不支持,要在VC中写图形,得用MFC。
cprintf("字符=%c", myhaffcode[i].data);
printf(" ");
textcolor(YELLOW);
cprintf("weight=%3d", myhaffcode[i].weight);
printf(" ");
textcolor(YELLOW);
cprintf("haffcode=");
for (j = myhaffcode[i].start + 1; j<MAXBIT; j++)
cprintf("%d", myhaffcode[i].bit[j]);
printf("\n");
count++;
if (count == 21)
getchar();
}
}
void test(struct haffcode haffcode[], int n)
{
int i, j, k, s;
char sstring[MAXNODE];
struct haffcode newhaffcode[MAXNODE];
j = 0;
system("clear");
textcolor(YELLOW);
cprintf("请输入哈夫曼编码测试数据,在此建议为this programme is my favorite");
printf("\n");
cprintf("注意小写,空格由大写字母T代替,并且字符数少于27.\n");
scanf("%s", sstring);
if (strlen(sstring) >= MAXNODE)
{
printf("you input the data number>=MAXNODE");
exit(1);
}
for (i = 0; i < strlen(sstring); i++)
{
for (j = 0; j < MAXBIT; j++)
if (sstring[i]==haffcode[i].data)
{
k = j;
break;
}
if (k<0 || k>MAXNODE - 1)
{
printf("在系统中找不到与第%d个字符相匹配的编码\n", i + 1);
continue;
}
newhaffcode[i].start = haffcode[k].start;
newhaffcode[i].weight = haffcode[k].weight;
newhaffcode[i].data = haffcode[k].data;
for (s = haffcode[k].start + 1; s<MAXBIT; s++)
newhaffcode[i].bit[s] = haffcode[k].bit[s];
}
pprintf(newhaffcode, strlen(sstring));
}
void end()
{
int driver, mode;
driver = VGA;//vga是video graphics array(视频图形阵列适配器)的缩写,是C语言所支持的一种显示器适配器.
//VGAHI是vga的一种显示模式, 为640*480的高分辨率显示方式.
mode = VGAHI;
initgraph(&driver, &mode, " ");
setlinestyle(0, 0, 2);
setfillstyle(1, 9);
bar(120, 60, 520, 80);
setfillstyle(1, 9);
bar(90, 100, 550, 350);
moveto(121, 65);
settextstyle(5, 0, 6);
setcolor(7);
outtext("This programme is designed by Dou Zheren");
settextstyle(3, 0, 3);
setcolor(7);
moveto(150, 200);
outtext("thank you use this programme.");
moveto(100, 300);
settextstyle(3, 0, 2);
setcolor(7);
outtext("please press anykey to end this programme.");
}
void main()
{
int i, j, n = 27;
int driver = VGA, mode = VGAHI;
char ch;
int weight[27] = { 186, 64, 13, 22, 32, 103, 21, 15, 47,
57, 1, 5, 32, 20, 57, 63, 15, 1, 48,
51, 80, 23, 8, 18, 1, 16, 1 };
char data[28] = { 'T', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h',
'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q',
'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z' };
struct haffnode newhaffnode[2 * MAXNODE - 1];
struct haffcode newcode[MAXNODE];
struct haffnode *myhafftree = newhaffnode;
struct haffcode *myhaffcode = newcode;
if (n>MAXNODE)
{
printf("you input the haffnode > MAXNODE,so you input the data is wrong");
printf("\n");
exit(1);
}
system("clear");
textcolor(YELLOW);
cprintf("WELCOME!这是一个求哈夫曼编码的问题");
printf("\n");
cprintf("即对所有的字母进行编码后,在根据用户的需要,对用户的要求进行编码。");
printf("\n");
cprintf("注意:本程序只支持小写字母,空格用大写字母T代替! ");
printf("\n");
getch();
textcolor(YELLOW);
cprintf("Ready?Enter,if you want to begin!\n");
printf("\n");
getch();
cprintf("Now,开始演示哈夫曼编码.");
getch();
haffmantree(weight, n, myhafftree, data);
haffmancode(myhafftree, n, myhaffcode);
pprintf(myhaffcode, n);
system("clear");
printf("若执行自定义编译,请输入y继续。否则程序将结束.");
if ((ch = getch()) == 'y' || ch == 'Y')
test(myhaffcode, n);
getchar();
system("clear");
end();
getch();
exit(1);
}
//倒推法
#include<stdio.h>
void main()
{
int k;
float d, dl;
float oil[10], dis[10];
int i;
printf("NO,distance(k,m)\toil(L)\n");
k = 1;
d = 500;
dis[1] = 500;
oil[1] = 500;
do
{
k = k + 1;
d = d + 500 / (2 * k - 1);
dis[k] = d;
oil[k] = oil[k - 1] + 500;
} while (!(d >= 1000));
dis[k] = 1000;
dl = 1000 - dis[k - 1];
oil[k] = dl*(2 * k + 1) + oil[k - 1];
for (i = 0; i < k; i++)
printf("%d\t%f\t%f\t\n", i, 1000 - dis[k - i], oil[k - i]);
}
//字串核对
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
void table(char*);//建立前进表
int search(int, char*, char*);//搜寻关键字
void substring(char*, char*, int, int);//取出字符串
int skip[256];
int main(void)
{
char str_input[80];
char str_key[80];
char tmp[80] = { '\0' };
int m, n, p;
printf("请输入字符串:");
gets(str_key);
m = strlen(str_input);//计算字符长度
n = strlen(str_key);
table(str_key);
p = search(n - 1,str_input, str_key);
while (p != -1)
{
substring(str_input, tmp, p, m);
printf("%s\n", tmp);
p = search(p + n + 1, str_input, str_key);
}
printf("\n");
return 0;
}
void table(char*key)
{
int k, n;
n = strlen(key);
for (k = 0; k <= 255; k++)
skip[k] = n;
for (k = 0; k < n - 1; k++)
skip[key[k]] = n - k - 1;
}
int search(int p, char*input, char*key)
{
int i, m, n;
char tmp[80] = { '\0' };
m = strlen(input);
n = strlen(key);
while (p < m)
{
substring(input, tmp, p - n + 1, p);
if (!strcmp(tmp, key))//比较两个字符串是否相同
return p - n + 1;
p += skip[input[p]];
}
return -1;
}
void substring(char*text, char* tmp, int s, int e)
{
int i, j;
for (i = s, j = 0; i <= e; i++, j++)
tmp[j] = text[i];
tmp[j] = '\0';
}
双色,三色河内塔
#include<stdio.h>
void hanoi(int disks, char source, char temp, char target)
{
if (disks == 1)
{
printf("move disk from %c to %c\n", source, target);
printf("move disk from %c to %c\n", source, target);
}
else
{
hanoi(disks - 1, source, target, temp);
hanoi(1, source, temp, target);
hanoi(disks - 1, temp, source, target);
}
}
void hanoi2colors(int disks)
{
char source = 'A';
char temp = 'B';
char target = 'C';
int i;
for (i = disks / 2; i > 1; i--)
{
hanoi(i - 1, source, temp, target);
printf("move disk from %c to %c\n", source, temp);
printf("move disk from %c to %c\n", source, temp);
hanoi(i - 1, target, temp, source);
printf("move disk from %c to %c\n", temp , target);
}
printf("move disk from %c to %c\n", source, temp);
printf("move disk from %c to %c\n", source, temp);
}
int main()
{
int n;
printf("请输入盘数:");
scanf("%d", &n);
hanoi2colors(n);
return 0;
}
三色
#include<stdio.h>
void hanoi(int disks, char source, char temp, char target) {
if (disks == 1) {
printf("move disk from %c to %c\n", source, target);
printf("move disk from %c to %c\n", source, target);
printf("move disk from %c to %c\n", source, target);
}
else {
hanoi(disks - 1, source, target, temp);
hanoi(1, source, temp, target);
hanoi(disks - 1, temp, source, target);
}
}
void hanoi3colors(int disks) {
char source = 'A';
char temp = 'B';
char target = 'C';
int i;
if (disks == 3) {
printf("move disk from %c to %c\n", source, temp);
printf("move disk from %c to %c\n", source, temp);
printf("move disk from %c to %c\n", source, target);
printf("move disk from %c to %c\n", temp, target);
printf("move disk from %c to %c\n", temp, source);
printf("move disk from %c to %c\n", target, temp);;
}
else {
hanoi(disks / 3 - 1, source, temp, target);
printf("move disk from %c to %c\n", source, temp);
printf("move disk from %c to %c\n", source, temp);
printf("move disk from %c to %c\n", source, temp);
hanoi(disks / 3 - 1, target, temp, source);
printf("move disk from %c to %c\n", temp, target);
printf("move disk from %c to %c\n", temp, target);
printf("move disk from %c to %c\n", temp, target);
hanoi(disks / 3 - 1, source, target, temp);
printf("move disk from %c to %c\n", target, source);
printf("move disk from %c to %c\n", target, source);
hanoi(disks / 3 - 1, temp, source, target);
printf("move disk from %c to %c\n", source, temp);
for (i = disks / 3 - 1; i > 0; i--) {
if (i>1) {
hanoi(i - 1, target, source, temp);
}
printf("move disk from %c to %c\n", target, source);
printf("move disk from %c to %c\n", target, source);
if (i>1) {
hanoi(i - 1, temp, source, target);
}
printf("move disk from %c to %c\n", source, temp);
}
}
}
int main() {
int n;
printf("请输入盘数:");
scanf("%d", &n);
hanoi3colors(n);
getchar();
return 0;
}
背包问题
#include<stdio.h>
#include<stdlib.h>
#define LIMIT 8
#define N 5
#define MIN 1
struct body
{
char name[20];
int size;
int price;
};
typedef struct body object;
int main(void)
{
int item[LIMIT + 1] = { 0 };
int value[LIMIT + 1] = { 0 };
int newvalue, i, s, p;
object a[] = { { "李子", 4, 4500 },
{ "苹果", 5, 5700 },
{ "橘子", 2, 2250 },
{ "草莓", 1, 1100 },
{ "甜瓜", 6, 6700 } };
for (i = 0; i < N; i++) {
for (s = a[i].size; s <= LIMIT; s++) {
p = s - a[i].size;
newvalue = value[p] + a[i].price;
if (newvalue > value[s]) {// 找到阶段最佳解
value[s] = newvalue;
item[s] = i;
}
}
}
printf("物品\t价格\n");
for (i = LIMIT; i >= MIN; i = i - a[item[i]].size) {
printf("%s\t%d\n",
a[item[i]].name, a[item[i]].price);
}
printf("合计\t%d\n", value[LIMIT]);
return 0;
}
//蒙地卡罗法求pi
#include<stdio.h>
#include<stdlib.h>
#include<time.h>
#define N 50000
int main(void)
{
int i, sum = 0;
double x, y;
srand(time(NULL));
for (i = 1; i < N; i++)
{
x = (double)rand() / RAND_MAX;
y = (double)rand() / RAND_MAX;
if ((x*x + y*y) < 1)
sum++;
}
printf("PI=%f\n", (double)4 * sum / N);
return 0;
}
//阶梯问题的递归解法
#include<stdio.h>
#define N 10
int steped[N];
int i = 0;
void steping(int n)
{ //走楼梯
if (n == 0) //走完
{
for (int j = 0; j < i; j++)
{
printf("%d", steped[j]);//打印
}
printf("\n");
}
if (n >= 1)
{ //若剩下的阶梯数大于等于1
steped[i++] = 1; //迈一个阶梯
steping(n - 1); //走剩下的路
i--; //退一个阶梯,寻找其他方法
}
if (n >= 2)
{
steped[i++] = 2;
steping(n - 2);
i--;
}
if (n >= 3)
{
steped[i++] = 3;
steping(n - 3);
i--;
}
}
void main()
{
int n;
n = N;
steping(n);
getchar();
}
代码4
最新推荐文章于 2021-02-13 11:42:20 发布