可以先把所有数字放在第一层,遍历每一层,将“倍数”移到下一层
#include<stdio.h>
#include<malloc.h>
const char *intoutputformat = "%4d";
//定义一个整数链表,用于存放每层数字
typedef struct integerlist {
int num;
struct integerlist *next;
} integerList;
//定义金字塔,形式为链表的链表
typedef struct intpyramid {
integerList *list;
struct intpyramid * nextfloor;
} intPyramid;
//释放链表内存的函数
void deletelist(integerList **intlist) {
integerList *p = *intlist, *q;
while (p) {
q = p->next;
free(p);
p = q;
}
*intlist = NULL;
}
//创建不大于n的正整数的升序链表
void creatlist(integerList **intlist, int n) {
int i;
integerList *p;
if (*intlist) { //如果intlist已经指向一个链表,则将对应内存释放
deletelist(intlist);
}
if (n < 1)return;
p = *intlist = (integerList*)malloc(sizeof(integerList));
for (i = 1; i < n; i++) {
p->num = i;
p->next = (integerList*)malloc(sizeof(integerList));
p = p->next;
}
p->num = n;
p->next = NULL;
}
//释放金字塔内存
void deletepyramid(intPyramid**phead) {
intPyramid *p = *phead, *q;
while (p) {
q = p->nextfloor;
deletelist(&p->list);
free(p);
p = q;
}
*phead = NULL;
}
//根据升序链表创建金字塔
void creatpyramid(intPyramid**phead, integerList*intlist) {
intPyramid *p;
integerList *plist, *qlist, *rlist, *nextfloortail;
if (*phead) { //如果intlist已经指向一个金字塔,则将对应内存释放
deletepyramid(phead);
}
if (!intlist)return;
p = *phead = (intPyramid*)malloc(sizeof(intPyramid));
p->list = intlist;
while (1) {
p->nextfloor = (intPyramid*)malloc(sizeof(intPyramid));
nextfloortail = p->nextfloor->list = NULL;
//rlist对应的num作为除数,遍历p指向的金字塔的一层
for (rlist = p->list; rlist; rlist = rlist->next) {
plist = rlist;
qlist = plist->next;
while (qlist) {
if (qlist->num % rlist->num == 0) {
//条件为真时把qlist指向的节点移到下一层
if (nextfloortail) {
nextfloortail->next = qlist;
nextfloortail = qlist;
} else {
nextfloortail = p->nextfloor->list = qlist;
}
plist->next = qlist->next;
qlist->next = NULL;
} else {
plist = qlist;
}
qlist = plist->next;
}
}
if (p->nextfloor->list) { //如果下一层有数字,则循环,否则退出循环
p = p->nextfloor;
} else {
free(p->nextfloor);
p->nextfloor = NULL;
break;
}
}
}
//输出链表的函数
void intlist_output(integerList* p) {
while (p) {
printf(intoutputformat, p->num);
p = p->next;
}
}
//输出金字塔的函数
void intpyramid_output(intPyramid* p) {
while (p) {
intlist_output(p->list);
putchar('\n');
p = p->nextfloor;
}
}
//在链表中查找数字,找到时返回是第几个,未找到则返回链表数字个数的相反数
int intlist_search(integerList* p, int num) {
int index = 0;
while (p && p->num != num) {
p = p->next;
index++;
}
if (p) {
return index + 1;
} else {
return -index;
}
}
//在金字塔中查找数字,找到时返回是第几个,未找到则返回金字塔数字个数的相反数
int intpyramid_search(intPyramid*p, int num) {
int index = 0, r;
while (p) {
r = intlist_search(p->list, num);
if (r > 0) {
return index + r;
}
index -= r;
p = p->nextfloor;
}
return -index;
}
int main() {
integerList*intlist=NULL;
intPyramid*phead=NULL;
int n;
char c;
printf("输入信息:\n");
scanf("%d", &n);
creatlist(&intlist, n);
creatpyramid(&phead, intlist);
intpyramid_output(phead);
printf("输出排名:\n");
while((c=getchar())==','){
scanf("%d",&n);
n=intpyramid_search(phead,n);
printf(intoutputformat,n);
putchar(',');
}
return 0;
}
ok
ok2
试试可以使用以下代码来构建正整数金字塔:
#include <stdio.h>
int main() {
int n;
printf("Enter a positive integer: ");
scanf("%d", &n);
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= i; j++) {
printf("%d ", j);
}
printf("\n");
}
return 0;
}
输入的正整数 n 表示金字塔的层数。外层循环遍历每一层,内层循环按顺序输出 1 到 i 的数字,每个数字之间用空格分隔。最后在每一层的末尾输出换行符来实现换行。
输入:
5
输出:
1
1 2
1 2 3
1 2 3 4
1 2 3 4 5
这段代码实现了您所述的要求,即相同层数的数字从左往右、从小到大排列,并且每个数尽可能放到更下方。
为了实现上述需求,我们需要定义一个数字金字塔的结构体,并使用链表来存储数字金字塔的各个结点。
数字金字塔结构体定义如下:
struct Pyramid {
int value;
int level;
int index;
struct Pyramid *next;
};
其中,value
表示当前结点的数字值,level
表示当前结点所在的层数,index
表示当前结点在该层的序号,next
指向下一个结点。
接下来,我们可以编写建立链表的函数create_pyramid_list
,这个函数需要接收一个正整数n作为参数,并返回建立的链表的头指针。在这个函数中,在这个函数中,我们需要按照数字金字塔构造规则构造出数字金字塔。
struct Pyramid *create_pyramid_list(int n) {
// 初始化第0层
struct Pyramid *head = (struct Pyramid *)malloc(sizeof(struct Pyramid));
head->value = 1;
head->level = 0;
head->index = 1;
head->next = NULL;
struct Pyramid *current = head;
for (int i = 2; i <= n; i++) {
// 如果i满足条件,则将i放在current的下方一层
if (i > current->value && i % current->value == 0) {
struct Pyramid *new_node = (struct Pyramid *)malloc(sizeof(struct Pyramid));
new_node->value = i;
new_node->level = current->level + 1;
new_node->index = 1;
new_node->next = NULL;
current->next = new_node;
current = new_node;
} else {
// 否则将i放在current的同一层,并从左向右从小到大排列
struct Pyramid *node = head;
while (node->next != NULL && node->next->level == current->level) {
node = node->next;
}
struct Pyramid *new_node = (struct Pyramid *)malloc(sizeof(struct Pyramid));
new_node->value = i;
new_node->level = current->level;
new_node->index = node->index + 1;
new_node->next = node->next;
node->next = new_node;
}
}
return head;
}
然后我们可以编写查找3个数字在数字金字塔中的序号的函数find_index
。这个函数需要接收一个数字金字塔的头指针和3个数字作为参数,并输出这3个数字在数字金字塔中的序号。
函数实现如下:
void find_index(struct Pyramid *head, int num1, int num2, int num3) {
struct Pyramid *node = head;
while (node != NULL) {
if (node->value == num1) {
printf("数字%d的序号为%d\n", num1, node->index);
}
if (node->value == num2) {
printf("数字%d的序号为%d\n", num2, node->index);
}
if (node->value == num3) {
printf("数字%d的序号为%d\n", num3, node->index);
}
node = node->next;
}
}
最后,我们可以编写一个函数实现数字金字塔的分层输出。这个函数需要接收一个数字金字塔的头指针作为参数,并输出数字金字塔的每一层。
函数实现如下:
void print_pyramid(struct Pyramid *head) {
int current_level = 0;
struct Pyramid *node = head;
while (node != NULL) {
if (node->level != current_level) {
printf("\n");
current_level = node->level;
}
printf("%d ", node->value);
node = node->next;
}
printf("\n");
}
完整的代码如下:
#include <stdio.h>
#include <stdlib.h>
struct Pyramid {
int value;
int level;
int index;
struct Pyramid *next;
};
struct Pyramid *create_pyramid_list(int n) {
// 初始化第0层
struct Pyramid *head = (struct Pyramid *)malloc(sizeof(struct Pyramid));
head->value = 1;
head->level = 0;
head->index = 1;
head->next = NULL;
struct Pyramid *current = head;
for (int i = 2; i <= n; i++) {
// 如果i满足条件,则将i放在current的下方一层
if (i > current->value && i % current->value == 0) {
struct Pyramid *new_node = (struct Pyramid *)malloc(sizeof(struct Pyramid));
new_node->value = i;
new_node->level = current->level + 1;
new_node->index = 1;
new_node->next = NULL;
current->next = new_node;
current = new_node;
} else {
// 否则将i放在current的同一层,并从左向右从小到大排列
struct Pyramid *node = head;
while (node->next != NULL && node->next->level == current->level) {
node = node->next;
}
struct Pyramid *new_node = (struct Pyramid *)malloc(sizeof(struct Pyramid));
new_node->value = i;
new_node->level = current->level;
new_node->index = node->index + 1;
new_node->next = node->next;
node->next = new_node;
}
}
return head;
}
void find_index(struct Pyramid *head, int num1, int num2, int num3) {
struct Pyramid *node = head;
while (node != NULL) {
if (node->value == num1) {
printf("数字%d的序号为%d\n", num1, node->index);
} else if (node->value == num2) {
printf("数字%d的序号为%d\n", num2, node->index);
} else if (node->value == num3) {
printf("数字%d的序号为%d\n", num3, node->index);
}
node = node->next;
}
}
void print_pyramid(struct Pyramid *head) {
struct Pyramid *node = head;
// 记录每一层的最大编号
int max_index[100] = {0};
while (node != NULL) {
max_index[node->level] = node->index;
node = node->next;
}
// 按层数从上到下遍历
for (int i = 0; i < 100; i++) {
if (max_index[i] == 0) {
break;
}
// 按编号从左到右遍历
for (int j = 1; j <= max_index[i]; j++) {
node = head;
while (node != NULL) {
if (node->level == i && node->index == j) {
printf("%d ", node->value);
break;
}
node = node->next;
}
}
printf("\n");
}
}
int main() {
int n;
printf("请输入n的值:");
scanf("%d", &n);
struct Pyramid *head = create_pyramid_list(n);
int num1, num2, num3;
printf("请输入3个不大于n的正整数:");
scanf("%d%d%d", &num1, &num2, &num3);
find_index(head, num1, num2, num3);
printf("\n");
printf("数字金字塔如下:\n");
print_pyramid(head);
return 0;
}
你这个问题最大的难度就是,每个数字要把数放在尽量下方,例如4,就要放第三层,而不是第二层