HDU——2094.产生冠军、2095.find your present(2)、2096.小明A+B

2094.产生冠军

题目描述

Problem - 2094

Problem Description

有一群人,打乒乓球比赛,两两捉对撕杀,每两个人之间最多打一场比赛。
球赛的规则如下:
如果A打败了B,B又打败了C,而A与C之间没有进行过比赛,那么就认定,A一定能打败C。
如果A打败了B,B又打败了C,而且,C又打败了A,那么A、B、C三者都不可能成为冠军。
根据这个规则,无需循环较量,或许就能确定冠军。你的任务就是面对一群比赛选手,在经过了若干场撕杀之后,确定是否已经实际上产生了冠军。

Input

输入含有一些选手群,每群选手都以一个整数n(n<1000)开头,后跟n对选手的比赛结果,比赛结果以一对选手名字(中间隔一空格)表示,前者战胜后者。如果n为0,则表示输入结束。

Output

对于每个选手群,若你判断出产生了冠军,则在一行中输出“Yes”,否则在一行中输出“No”。

运行代码

#include <map>
#include <string>
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
typedef long long LL;
int In_degree[2000 + 10];
int main() {
    int n;
    while (cin>>n) {
        if (n == 0) break;
        int tot = 0;  // 总点数
        memset(In_degree, 0, sizeof(In_degree));
        map<string, int> mp;
        for (int i = 1; i <= n; i++) {
            string str1, str2;
            cin >> str1 >> str2;
            if (!mp.count(str1))
                mp[str1] = ++tot;
            if (!mp.count(str2))
                mp[str2] = ++tot;
            In_degree[mp[str2]]++;
        }
        int flag = 0;
        for (int i = 1; i <= tot; i++) {
            if (In_degree[i] == 0) {
                flag++;
            }
        }
        if (flag == 1) {
            printf("Yes\n");
        }
        else {
            printf("No\n");
        }
    }
}

代码思路

  1. 输入处理

    • 程序首先读取一个整数n,表示接下来会有n行输入,每行包含两个字符串,表示一条从字符串1到字符串2的有向边。
    • 当输入的n为0时,结束输入。
  2. 数据结构初始化

    • 定义一个全局数组In_degree来记录每个顶点的入度,初始化为0。
    • 使用std::map<string, int> mp来建立字符串到顶点编号的映射,初始为空。
  3. 构建有向图

    • 遍历每一行输入,对于每一对字符串(str1str2),如果它们还没有映射到顶点编号,则在映射表mp中为它们分配新的顶点编号(即映射表的大小加1)。
    • 然后,增加str2所对应的顶点的入度。这表示有一条边指向该顶点。
  4. 判断唯一起点

    • 初始化flag为0,用来记录入度为0的顶点个数。
    • 遍历所有顶点的入度,如果发现某个顶点的入度为0,则flag加1。
    • 最后,根据flag的值判断:
      • 如果flag == 1,说明存在唯一一个入度为0的顶点,即存在唯一的起点,输出"Yes"。
      • 否则,说明要么没有起点(所有顶点都有入度),要么起点不止一个,输出"No"。

2095.find your present(2)

题目描述

Problem - 2095

Problem Description

In the new year party, everybody will get a "special present".Now it's your turn to get your special present, a lot of presents now putting on the desk, and only one of them will be yours.Each present has a card number on it, and your present's card number will be the one that different from all the others, and you can assume that only one number appear odd times.For example, there are 5 present, and their card numbers are 1, 2, 3, 2, 1.so your present will be the one with the card number of 3, because 3 is the number that different from all the others.

Input

The input file will consist of several cases. 
Each case will be presented by an integer n (1<=n<1000000, and n is odd) at first. Following that, n positive integers will be given in a line, all integers will smaller than 2^31. These numbers indicate the card numbers of the presents.n = 0 ends the input.

Output

For each case, output an integer in a line, which is the card number of your present.

运行代码

#include <iostream>
using namespace std;
int find(int n) {
    int num, result = 0;
    for (int i = 0; i < n; i++) {
        scanf_s("%d", &num);
        result ^= num;
    }
    return result;
}
int main() {
    int n;
    while (true) {
        scanf_s("%d", &n);
        if (n == 0)
            break;
        printf("%d\n", find(n));
    }
    return 0;
}

代码思路

  • 输入参数n,表示用户将要输入的整数数量。
  • 初始化两个变量:num用于暂存每次读入的整数,result初始值为0,用于存储最终的异或结果。
  • 使用一个循环,迭代n次,每次迭代:通过scanf_s读取一个整数到num。将当前的numresult进行异或运算,并将结果更新到result中。
  • 循环结束后,返回result作为结果。由于异或运算的性质——任何数与自身异或结果为0,且异或运算满足交换律和结合律,成对出现的数字在异或运算后会相互抵消,最终剩下的结果就是那个唯一出现一次的数字。
  • 初始化一个整数变量n
  • 进入一个无限循环,直到用户输入0作为结束信号。
  • 在每次循环中,先读取用户输入的n,如果n为0,则退出循环;否则,调用find函数处理这n个数,并打印出该函数的返回值,即那个独特的整数。

2096.小明A+B

题目描述

Problem - 2096

Problem Description:小明今年3岁了, 现在他已经能够认识100以内的非负整数, 并且能够进行100以内的非负整数的加法计算.对于大于等于100的整数, 小明仅保留该数的最后两位进行计算, 如果计算结果大于等于100, 那么小明也仅保留计算结果的最后两位.
例如, 对于小明来说:
1) 1234和34是相等的
2) 35+80=15给定非负整数A和B, 你的任务是代表小明计算出A+B的值.

Input:输入数据的第一行为一个正整数T, 表示测试数据的组数. 然后是T组测试数据. 每组测试数据包含两个非负整数A和B(A和B均在int型可表示的范围内).

Output:对于每组测试数据, 输出小明A+B的结果.

运行代码

#include <iostream>
using namespace std;
int add(int a, int b) {
    a = a % 100;
    b = b % 100;
    int result = a + b;
    if (result >= 100) {
        result %= 100;
    }
    return result;
}
int main() {
    int T;
    cin >> T;
    while (T--) {
        int A, B;
        cin >> A >> B;
        cout << add(A, B) << endl;
    }
    return 0;
}

代码思路

  • 输入参数:两个整数ab
  • 功能:实现特殊加法规则。
    1. 首先,通过a = a % 100b = b % 100,确保只保留ab的最后两位数。
    2. 然后,计算这两者的和result = a + b
    3. 判断result是否大于等于100,如果是,则result %= 100,确保结果保持为两位数。
    4. 最后,返回处理后的结果result
  • 读取测试用例数量:首先读取一个整数T,表示接下来有T组测试数据。
  • 循环处理测试用例:使用while (T--)循环,对每组测试数据执行以下操作:
    1. 读取每组数据中的两个整数AB
    2. 调用add(A, B)函数,获取特殊加法的结果。
    3. 输出该结果。
  • 程序结束:所有测试用例处理完毕后,程序返回0,正常结束。
  • 35
    点赞
  • 23
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

筱姌

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值