#include <stdio.h>
#include <stdlib.h>
#define MAX_SIZE 100
//设置登录窗口
void Setwindowstyle()
{
system("title 蜗牛爬树");
system("color f3");
system("mode con cols=50 lines=20");}
// 定义蜗牛信息的结构体struct Snail
{
int ni; // 爬行速度 ni
int mi; // 滑行速度 mi
int height; // 当前高度
int time; // 爬完整棵树所需的时间 };
// 定义链表结点
struct Node {
struct Snail snail;
struct Node* next;};
// 定义顺序栈
struct Stack {
int top;
int data[MAX_SIZE];};
// 创建链表
struct Node* createList(int n) {
struct Node* head = NULL;
struct Node* p = NULL;
int i;
for (i = 0; i < n; i++) {
struct Snail snail;
printf("输入第 %d 只蜗牛的爬行速度 ni 和滑行速度 mi: ", i + 1);
scanf("%d%d", &snail.ni, &snail.mi);
snail.height = 0;
snail.time = 0;
struct Node* node = (struct Node*)malloc(sizeof(struct Node));
node->snail = snail;
node->next = NULL;
if (head == NULL)
//链表为空,将新节点作为头结点
{
head = node;
p = node;
}
else {
p->next = node;
p = node;
}
}
return head;}
// 初始化顺序栈
void initStack(struct Stack* stack) {
stack->top = -1;}
// 判断栈是否为空
int isStackEmpty(struct Stack* stack) {
return stack->top == -1;}
// 判断栈是否已满
int isStackFull(struct Stack* stack) {
return stack->top == MAX_SIZE - 1;}
//入栈
void push(struct Stack* stack, int x) {
if (isStackFull(stack)) {
printf("栈已满!\n");
return; }
stack->data[++(stack->top)] = x;}
//出栈int pop(struct Stack* stack) {
if (isStackEmpty(stack)) {
printf("栈为空!\n");
return -1; }
return stack->data[(stack->top)--];
}
void menu() {
system("color f3");
printf("\t\t------------------------蜗牛爬树------------------------\n\n");
printf("\t\t*************************1.开爬*************************\n\n");
printf("\t\t***************2.输出每一米线被爬过的次数***************\n\n");
printf("\t\t****************3.输出每只蜗牛的爬行时间****************\n\n");
printf("\t\t*************4.输出爬的最快的蜗牛的爬行规律*************\n\n");
printf("\t\t*************************0.退出*************************\n\n");
printf("\t\t--------------------------------------------------------\n\n");}
void Frequency(struct Stack * stack, int h) {
// 统计树的每一米线都有多少次蜗牛爬过
printf("\t\t******************每一米线被爬过的次数******************\n\n");
int count[MAX_SIZE] = { 0 };
while (!isStackEmpty(stack)) {
int x = pop(stack);
count[x]++; }
int i; // 输出每一米线被爬过的次数
for (i = 1; i <= h; i++) {
printf("\t\t第 %d 米线被爬过的次数: %d\n\n", i, count[i]);
}
printf("\n");}
void FastestSnail(struct Node* head) {
// 找出哪只蜗牛爬得最快
int minTime = -1; struct Node* p = head; while (p != NULL) { struct Snail snail = p->snail; if (minTime == -1 || snail.time < minTime) { minTime = snail.time; } p = p->next; } // 输出爬得最快的蜗牛的爬行速度规律 printf("\t\t*****************爬的最快的蜗牛的爬行规律*****************\n\n"); p = head; while (p != NULL) { struct Snail snail = p->snail; if (snail.time == minTime) { printf("\t\t爬得最快的蜗牛的爬行速度规律: ni=%d, mi=%d\n\n", snail.ni, snail.mi); } p = p->next; }}void PrintTime(struct Node* head) { //输出每只蜗牛的爬行时间 struct Node* p = head; int k = 0; printf("\t\t*******************每只蜗牛的爬行时间*******************\n\n"); while (p != NULL) { k++; struct Snail snail = p->snail; printf("\t\t第 %d 只蜗牛的爬行时间:%d\n\n", k, snail.time); p = p->next; }}void InitCrawl(struct Node* head,struct Stack* stack,int h) { struct Node* p = head; while (p != NULL) { while (p->snail.height < h) { p->snail.time++; p->snail.height += p->snail.ni; //爬行 push(stack, p->snail.height); //压入栈中 if (p->snail.height >= h) { break; } p->snail.height -= p->snail.mi; //滑行 push(stack, p->snail.height); //压入栈中 } if (p->snail.height < 0) //位置小于0,将位置设置为0 { p->snail.height = 0; } if (p->snail.height >= h) //位置大于h,将位置设置为h { p->snail.height = h; } p = p->next; } printf("\t\t咯吱咯吱......蜗牛已爬完!\n"); return;}int main() { Setwindowstyle(); system("color f3"); printf("**************************************************\n"); printf("* 初始化 *\n"); printf("**************************************************\n"); int h, n; printf("输入高度 h 和蜗牛个数 n :"); scanf("%d%d", &h, &n); // 创建链表 struct Node* head = createList(n); // 初始化顺序栈 struct Stack stack; initStack(&stack); system("mode con cols=84 lines=80"); menu(); while (1) { int select = 0; printf("请输入你的选择:"); scanf("%d", &select); switch (select) { case 1: //开爬 InitCrawl(head, &stack, h); break; case 2: // 统计树的每一米线都有多少次蜗牛爬过 Frequency(&stack, h); break; case 3: //输出每只蜗牛的爬行时间 PrintTime(head); break; case 4: // 输出爬得最快的蜗牛的爬行速度规律 FastestSnail(head); break; case 0: exit(1); default: printf("非法的输入,请重新选择!\n"); break; } } return 0;}
望采纳!!点击该回答右侧的“采纳”按钮即可采纳!
参考代码:
#include<stdio.h>
#include<string.h>
#include<algorithm>
#include<iostream>
#include<math.h>
#define ll long long
using namespace std;
#define N 10010
struct w
{
int a,c,d,e;
char b;
} s[N],q[N];
bool cmp1(w x,w y)
{
return x.a<y.a;
}
bool cmp2(w x,w y)
{
return x.d<y.d;
}
bool cmp3(w x,w y)
{
return x.c<y.c;
}
int main()
{
int t,tot=1;
scanf("%d",&t);
while(t--)
{
int L,T,n;
scanf("%d %d %d",&L,&T,&n);//L表示木棍,T表示时间,n表示蚂蚁的数目
for(int i=0; i<n; i++)
{
scanf("%d %c",&s[i].a,&s[i].b);//输入顺序
getchar();
q[i].a=s[i].a;
q[i].b=s[i].b;
q[i].c=s[i].c=i;
}
sort(q,q+n,cmp1);//每个蚂蚁所在相对的位置不变
for(int i=0; i<n; i++) //求出最终各个蚂蚁的状态
{
if(s[i].b=='R')//朝向右
{
s[i].d=s[i].a+T;
}
else
{
s[i].d=s[i].a-T;
}
}
sort(s,s+n,cmp2);//蚂蚁最终位置从左到右
for(int i=0; i<n; i++)
q[i].e=0;
for(int i=0; i<n; i++)
{
q[i].a=s[i].d;
q[i].b=s[i].b;
if(i!=0)
{
if(q[i].a==q[i-1].a)
{
q[i].e=1;
q[i-1].e=1;
}
}
}
sort(q,q+n,cmp3);//输入顺序
printf("Case #%d:\n",tot++);
for(int i=0; i<n; i++)
{
if(q[i].a<0||q[i].a>L)
printf("Fell off\n");
else
{
if(q[i].e==1)
printf("%d Turning\n",q[i].a);
else
printf("%d %c\n",q[i].a,q[i].b);
}
}
//if(t)
printf("\n");
}
return 0;
}