关于#c++#的问题,请各位专家解答!

img

宴会
有一个文件,行数未知,每行有两个人的人名,中间由空格分隔。这两个人是敌对状
态,彼此不能碰面。此文件名为Enemies.txt。现在有若干张宴会名单,每张名单人数
不超过10人。名单存储在某个文件内,文件名由用户确定,此文件每行都是一个人的
姓名。例如:
Enemies.txt
Joe_SmithJackMiller
lack_JohnsonMary_Wilson
JohnWilliamsMaggie_Moore
Hermia_Anderson Maggie_Moore
Paul Jones Lucinda Taylor
Zoe Brown Hermia_Andersor
HarryDavis NickThomas
Zoe_BrownJoe_Smith
Larry_Jackson Sue_White
宴会名单例子:
A.txt
Joe_Smith
Mary_Wilson
JohnWilliams
Lucinda_Taylor
Sue_White
B.txt
Mary_Wilson
ZoeBrown
lackMiller
Hermia_Anderson
编一个程序,实现对用户输入的每张名单进行判定并且输出名单上最不受欢迎的人
如用户输入A.txt
输出:ok!Joe_Smithisthemostunwelcomeperson.
如用户输入B.txt
输出:no!Zoe_Brownisthemostunwelcomeperson

你题目的解答代码如下:

#include<iostream>
#include<cstring>
using namespace std;

int main()
{
    int n=0,m=0,i,j,max=0,ind=-1;
    char ene[100][2][50];
    char s[100][50];
    int a[100] = {0};
    char filename[100];
    FILE *fp;
    fp = fopen("Enemies.txt", "r");
    while (fscanf(fp,"%s %s",ene[n][0],ene[n][1])>0)
    {
        n++;
    }
    fclose(fp);
    printf("请输入文件名:");
    cin >> filename;
    fp = fopen(filename, "r");
    while (fscanf(fp,"%s",s[m])>0)
    {
        m++;
    }
    fclose(fp);
    for (i = 0; i < n; i++)
    {
        for (j = 0; j < m; j++)
            if (strcmp(s[j],ene[i][0])==0 || strcmp(s[j],ene[i][1])==0)
            {
                a[j]++;
                if (a[j]>max)
                {
                    max = a[j];
                    ind = j;
                }
            }
    }
    cout <<  s[ind] << "是最不受欢迎的人";
    return 0;
}

如有帮助,请点击我的回答下方的【采纳该答案】按钮帮忙采纳下,谢谢!

img

统计敌对表中每个人名出现的次数,用户输入的表中在敌对表中统计次数最大的人名即为最不受欢迎的人