真尴尬1180

结束的标志是两个小于0的数……
被套路了……..

#include <set>
#include <queue>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
const int maxn = 10005;
const int maxm = 10005;
struct Edge
{
    int from,to,next;
};
Edge e[maxm];
int head[maxn],edgeNum = 0,uset[maxn],du[maxn];

void addEdge(int from,int to)
{
    e[edgeNum].from = from; e[edgeNum].to = to; e[edgeNum].next = head[from];
    head[from] = edgeNum; edgeNum++;
}
int Find(int x)
{
    if(x == uset[x]) return x;
    else return uset[x] = Find(uset[x]);
}
int bfs(int rt)
{
    queue<int> q;
    q.push(rt);
    int num = 1;
    while(!q.empty())
    {
        int fa = q.front(); q.pop();
        for(int j = head[fa]; j != 0; j = e[j].next)
        {
            q.push(e[j].to);
            num++;
        }
    }
    return num;
}

int main()
{
    //freopen("input.txt","r",stdin);
    int from,to,cas = 1;
    while(scanf("%d%d" ,&from,&to) != EOF)
    {
        if(from <= 0 || to <= 0) break;
        if(from == 0 && to == 0)
        {
            printf("Case %d is a tree.\n",cas++); continue;
        }
        memset(head,0,sizeof(head));
        memset(du,0,sizeof(du));
        edgeNum = 1;
        set<int> myset;
        myset.insert(from);
        myset.insert(to);
        addEdge(from,to);
        du[to]++;
        while(scanf("%d%d" ,&from,&to))
        {
            if(from == 0 && to == 0) break;
            myset.insert(from);
            myset.insert(to);
            addEdge(from,to);
            du[to]++;
        }
        for(int i = 0; i < maxn; i++) uset[i] = i;
        bool OK = true; //认为不存在重边,环
        for(int i = 1; i < edgeNum; i++)
        {
            int x = Find(e[i].from);
            int y = Find(e[i].to);
            if(x != y) uset[x] = y;
            else
            {
                OK = false; break;
            }
        }
        if(!OK) //存在重边,环
        {
            printf("Case %d is not a tree.\n",cas++); continue;
        }
        int num = 0; //连通分量个数
        for(set<int>::iterator itr = myset.begin(); itr != myset.end(); ++itr)
        {
            int x = *itr;
            if(uset[x] == x) num++;
            if(num >= 2) break;
        }
        if(num != 1)
        {
            printf("Case %d is not a tree.\n",cas++); continue;
        }
        else //只有一个连通分量
        {
            int rt = -1;
            for(set<int>::iterator itr = myset.begin(); itr != myset.end(); ++itr)
            {
                int x = *itr;
                if(du[x] == 0)
                {
                    rt = x; break;
                }
            }
            if(rt == -1) //没能找到根
            {
                printf("Case %d is not a tree.\n",cas++); continue;
            }
            else
            {
                int cnt = bfs(rt);
                if(cnt != myset.size())
                {
                    printf("Case %d is not a tree.\n",cas++);
                }
                else
                {
                    printf("Case %d is a tree.\n",cas++);
                }
            }
        }
    }
    return 0;
}
### 使用C语言实现电梯逻辑编程 以下是基于C语言编写的简单电梯逻辑程序,它实现了基本的楼层请求功能以及上下移动控制: #### 代码示例 ```c #include <stdio.h> #define MAX_FLOORS 10 int main() { int currentFloor = 1; // 当前所在楼层,默认为1楼 int targetFloor; char direction; printf("当前电梯位于 %d 层\n", currentFloor); while (1) { // 循环等待输入指令 printf("请输入目标楼层(1-%d),或者按 'q' 退出:", MAX_FLOORS); if (scanf("%d", &targetFloor)) { if (targetFloor >= 1 && targetFloor <= MAX_FLOORS) { if (currentFloor < targetFloor) { direction = '↑'; // 上升方向 } else if (currentFloor > targetFloor) { direction = '↓'; // 下降方向 } else { direction = '='; // 不动 } printf("电梯正在%s向第%d层...\n", (direction == '=') ? "保持" : ((direction == '↑') ? "上升" : "下降"), targetFloor); for (int i = currentFloor; i != targetFloor;) { if (i < targetFloor) { i++; printf("到达:%d 层\n", i); } else if (i > targetFloor) { i--; printf("到达:%d 层\n", i); } } currentFloor = targetFloor; // 更新当前位置 printf("已抵达第%d层。\n", currentFloor); } else { printf("错误:楼层超出范围,请重新输入!\n"); } } else { getchar(); // 清除缓冲区中的字符 break; // 如果不是有效整数,则退出循环 } } printf("感谢使用本电梯系统!再见。\n"); return 0; } ``` 此代码展示了如何通过简单的条件判断来模拟电梯的行为。用户可以指定目标楼层,电梯会自动计算并显示移动过程。 --- ### 模拟社交尴尬场景的C语言代码 以下是一个简单的例子,用于模拟两个人之间的对话可能产生的尴尬时刻。假设两人轮流发言,当一方重复另一方的话语时,触发尴尬提示。 #### 代码示例 ```c #include <stdio.h> #include <string.h> void detectEmbarrassment(const char* personA, const char* personB) { if (strcmp(personA, personB) == 0) { printf("\n尴尬场面发生!双方说了相同的话。\n"); } } int main() { char aMessage[100], bMessage[100]; printf("欢迎来到社交对话模拟器!\n"); do { printf("\nPerson A 发言:"); fgets(aMessage, sizeof(aMessage), stdin); // 获取 Person A 的消息 aMessage[strcspn(aMessage, "\n")] = '\0'; printf("Person B 发言:"); fgets(bMessage, sizeof(bMessage), stdin); // 获取 Person B 的消息 bMessage[strcspn(bMessage, "\n")] = '\0'; detectEmbarrassment(aMessage, bMessage); // 检测是否尴尬 if (strcmp(aMessage, "exit") == 0 || strcmp(bMessage, "exit") == 0) { break; // 输入 exit 结束程序 } } while (1); printf("结束对话模拟,谢谢参与!\n"); return 0; } ``` 这段代码允许两个虚拟角色交替发言,并检测是否存在相同的言论引发尴尬局面。如果任意一方输入 `exit`,则终止程序。 --- ### 总结 上述两段代码分别演示了 **电梯逻辑** 和 **社交尴尬场景** 的模拟方式。前者利用循环和条件语句完成楼层间的切换操作[^1];后者借助字符串比较函数识别潜在的社会互动问题[^2]。 对于更复杂的业务需求,建议进一步扩展算法框架,例如引入优先级队列管理多个乘客请求或增强自然语言处理模块分析人类交流模式。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值