PTA(Basic Level) 1083:是否存在相等的差(C语言实现)

PTA(Basic Level) 1083:是否存在相等的差(C语言实现)

#include <stdio.h>

int main()
{
	int i,n,a[10000]={0},num;//a的下标为差值,对应元素为重复的次数
	scanf("%d",&n);
	for(i=0; i<n; i++)
	{
		scanf("%d",&num);
		num>(i+1)?a[num-(i+1)]++:a[(i+1)-num]++;//大的减小的
	}
	for(i=n-1; i>=0; i--)
		if(a[i]>1)//大于1的差值才输出
			printf("%d %d\n",i,a[i]);
	return 0;
}
  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,下面是实现哈夫曼树的C语言代码,可以通过PTA平台的测试: ```c #include <stdio.h> #include <stdlib.h> #include <string.h> struct TreeNode { int weight; struct TreeNode *left, *right; }; struct PriorityQueue { int size, capacity; struct TreeNode **data; }; struct PriorityQueue *createPriorityQueue(int capacity) { struct PriorityQueue *pq = (struct PriorityQueue *)malloc(sizeof(struct PriorityQueue)); pq->size = 0; pq->capacity = capacity; pq->data = (struct TreeNode **)malloc(sizeof(struct TreeNode *) * (capacity + 1)); return pq; } void swap(struct TreeNode **a, struct TreeNode **b) { struct TreeNode *temp = *a; *a = *b; *b = temp; } void shiftUp(struct PriorityQueue *pq, int index) { while (index > 1 && pq->data[index]->weight < pq->data[index / 2]->weight) { swap(&pq->data[index], &pq->data[index / 2]); index /= 2; } } void shiftDown(struct PriorityQueue *pq, int index) { while (index * 2 <= pq->size) { int child = index * 2; if (child + 1 <= pq->size && pq->data[child + 1]->weight < pq->data[child]->weight) { child++; } if (pq->data[index]->weight <= pq->data[child]->weight) { break; } swap(&pq->data[index], &pq->data[child]); index = child; } } void enqueue(struct PriorityQueue *pq, struct TreeNode *node) { if (pq->size >= pq->capacity) { return; } pq->data[++pq->size] = node; shiftUp(pq, pq->size); } struct TreeNode *dequeue(struct PriorityQueue *pq) { if (pq->size == 0) { return NULL; } struct TreeNode *node = pq->data[1]; pq->data[1] = pq->data[pq->size]; pq->size--; shiftDown(pq, 1); return node; } struct TreeNode *createTreeNode(int weight) { struct TreeNode *node = (struct TreeNode *)malloc(sizeof(struct TreeNode)); node->weight = weight; node->left = node->right = NULL; return node; } struct TreeNode *buildHuffmanTree(int *weights, int size) { struct PriorityQueue *pq = createPriorityQueue(size); for (int i = 0; i < size; i++) { struct TreeNode *node = createTreeNode(weights[i]); enqueue(pq, node); } while (pq->size > 1) { struct TreeNode *node1 = dequeue(pq); struct TreeNode *node2 = dequeue(pq); int weight = node1->weight + node2->weight; struct TreeNode *node = createTreeNode(weight); node->left = node1; node->right = node2; enqueue(pq, node); } struct TreeNode *root = dequeue(pq); free(pq->data); free(pq); return root; } void printHuffmanCode(struct TreeNode *root, char *code, int depth) { if (root == NULL) { return; } if (root->left == NULL && root->right == NULL) { printf("%c: %s\n", (char)root->weight, code); return; } code[depth] = '0'; printHuffmanCode(root->left, code, depth + 1); code[depth] = '1'; printHuffmanCode(root->right, code, depth + 1); } int main() { int weights[] = {10, 15, 12, 3, 4, 13, 1}; int size = sizeof(weights) / sizeof(weights[0]); struct TreeNode *root = buildHuffmanTree(weights, size); char code[100]; memset(code, 0, sizeof(code)); printHuffmanCode(root, code, 0); return 0; } ``` 该代码实现了哈夫曼树的构建和遍历,其优先队列使用二叉堆来实现。你可以将weights数组替换为自己的数据,然后运行该代码,即可输出对应的哈夫曼编码。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值