pat(A) 1063. Set Similarity(STL)

代码:

#include<cstdio>
#include<cstring>
#include<set>
using namespace std;

set<int> st[55];

int main()
{
    int n,m,k;
    while(scanf("%d",&n)==1)
    {
        double dp[100][100];
        for(int i=0; i<55; i++)
        {
            for(int j=0; j<55; j++)
            {
                dp[i][j]=-1.0;
            }
        }
        for(int i=1; i<=n; i++)
        {
            st[i].clear();
            scanf("%d",&m);
            for(int j=0; j<m; j++)
            {
                int x;
                scanf("%d",&x);
                st[i].insert(x);
            }
        }
        scanf("%d",&k);
        while(k--)
        {
            int x,y;
            char c='%';
            scanf("%d%d",&x,&y);
            if(st[x].size()==0&&st[y].size()!=0)
            {
                printf("0.0");
                printf("%c\n",c);
                continue;
            }
            if(st[x].size()!=0&&st[y].size()==0)
            {
                printf("0.0");
                printf("%c\n",c);
                continue;
            }
            if(x==y)
            {
                printf("100.0");
                printf("%c\n",c);
                continue;
            }
            set<int>::iterator it;
            int cnt=0;
            //printf("%lf\n",dp[x][y]);
            if(dp[x][y]==-1.0)
            {
                //printf("-----\n");
                for(it=st[x].begin(); it!=st[x].end(); it++)
                {

                    if(st[y].find(*it)!=st[y].end())
                        cnt++;
                }
                dp[x][y]=cnt*1.0/(st[x].size()+st[y].size()-cnt);
            }

            //printf("%d\n",cnt);
            printf("%.1lf",dp[x][y]*100);
            //char c='%';
            printf("%c",c);
            printf("\n");
        }
    }
    return 0;
}

版权声明:本文为博主原创文章,未经博主允许不得转载。

时间: 2024-08-27 20:21:55

pat(A) 1063. Set Similarity(STL)的相关文章

PAT 甲级 1063 Set Similarity (25 分) (新学,set的使用,printf 输出%,要%%)

1063 Set Similarity (25 分)   Given two sets of integers, the similarity of the sets is defined to be /, where N?c?? is the number of distinct common numbers shared by the two sets, and N?t?? is the total number of distinct numbers in the two sets. Yo

PAT Advanced 1063 Set Similarity (25分)(STL)

Given two sets of integers, the similarity of the sets is defined to be /, where N?c?? is the number of distinct common numbers shared by the two sets, and N?t?? is the total number of distinct numbers in the two sets. Your job is to calculate the si

PAT:1063. Set Similarity (25) AC

#include<stdio.h> #include<set> using namespace std; set<int> str[51]; //最大51个集合 void compare(int s1,int s2) { int different=str[s1].size(),same=0; //初始化并集元素为s1元素个数,交集元素0个 for(set<int>::iterator it=str[s2].begin() ; it!=str[s2].end

PAT 1063. Set Similarity (25)

1063. Set Similarity (25) Given two sets of integers, the similarity of the sets is defined to be Nc/Nt*100%, where Nc is the number of distinct common numbers shared by the two sets, and Nt is the total number of distinct numbers in the two sets. Yo

PAT 1063. Set Similarity

Given two sets of integers, the similarity of the sets is defined to be Nc/Nt*100%, where Nc is the number of distinct common numbers shared by the two sets, and Nt is the total number of distinct numbers in the two sets. Your job is to calculate the

PAT (Advanced Level) 1063. Set Similarity (25)

读入之后先排序. 询问的时候可以o(m)效率得到答案. #include<cstdio> #include<cstring> #include<cmath> #include<queue> #include<vector> #include<string> #include<stack> #include<map> #include<algorithm> using namespace std; s

1063. Set Similarity (25)遍历map

Given two sets of integers, the similarity of the sets is defined to be Nc/Nt*100%, where Nc is the number of distinct common numbers shared by the two sets, and Nt is the total number of distinct numbers in the two sets. Your job is to calculate the

1063. Set Similarity

Given two sets of integers, the similarity of the sets is defined to be Nc/Nt*100%, where Nc is the number of distinct common numbers shared by the two sets, and Nt is the total number of distinct numbers in the two sets. Your job is to calculate the

1063. Set Similarity (25)

题目如下: Given two sets of integers, the similarity of the sets is defined to be Nc/Nt*100%, where Nc is the number of distinct common numbers shared by the two sets, and Nt is the total number of distinct numbers in the two sets. Your job is to calcula