hdu 5232 Shaking hands 水题

本文介绍了一道关于计算派对上香槟总量的问题。通过输入朋友间的相识矩阵,算法计算出总共需要准备的香槟数量。核心在于统计已相识朋友间的握手次数,并据此计算所需的香槟杯数。

Shaking hands

Time Limit: 20 Sec  Memory Limit: 256 MB

题目连接

http://acm.hdu.edu.cn/showproblem.php?pid=5232

Description

Today is Gorwin’s birthday, so she holds a party and invites her friends to participate. She will invite n friends, for convenience, Gorwin numbers them from 1 to n. Some of them have known each other, But some of them have not. Those friends whose have known each other will shake hands with each other, and drink one cup of champagne. Gorwin wants to know how many cups of champagne she should prepare. Can you help her?

Input

Multiple test cases (about 30), the first line of each case contains an integer n which indicates Gorwin will invite n friends to her party.

Next n lines will give a n*n matrix, if a[i][j] is 1, then friend i and friend j have known each other, otherwise they have not known each other.

Please process to the end of file.

[Technical Specification]

All input entries are integers.

1<=n<=30

0<=a[i][j]<=1

a[i][i]=0;

a[i][j]=a[j][i] for i!=j

 

 

Output

For each case, output an integer which denotes total cups of champagne Gorwin should prepare in a single line.

Sample Input

2 0 0 0 0 3 0 0 1 0 0 0 1 0 0

Sample Output

4
8

HINT

 For the second case, Gorwin will shake hands with all her friends, then Gorwin drink three cups of champagne, each friends drink one cup. Friend 1 and friend 3 know each other,every of them drinks one cup again. So the total cups is 3+3+2=8.

题意

今天是Gorwin的生日,所以她举办了一个派对并邀请她的朋友来参加。她将邀请n个朋友,为了方便,Gorwin把他们从1到n标号。他们之中有一些人已经相互认识,有一些人不认识对方。相互认识的朋友见面之后会握手然后喝一杯香槟。Gorwin想要知道要准备多少杯香槟。你能帮助她吗?

 

题解:

给你的图里面有多少个1,ans就加多少

然后再加n,再乘2就好了

代码:

 

//qscqesze
#include <cstdio>
#include <cmath>
#include <cstring>
#include <ctime>
#include <iostream>
#include <algorithm>
#include <set>
#include <vector>
#include <sstream>
#include <queue>
#include <typeinfo>
#include <fstream>
#include <map>
#include <stack>
typedef long long ll;
using namespace std;
//freopen("D.in","r",stdin);
//freopen("D.out","w",stdout);
#define sspeed ios_base::sync_with_stdio(0);cin.tie(0)
#define maxn 200001
#define mod 10007
#define eps 1e-9
int Num;
char CH[20];
//const int inf=0x7fffffff;   //нчоч╢С
const int inf=0x3f3f3f3f;
/*

inline void P(int x)
{
    Num=0;if(!x){putchar('0');puts("");return;}
    while(x>0)CH[++Num]=x%10,x/=10;
    while(Num)putchar(CH[Num--]+48);
    puts("");
}
*/
inline ll read()
{
    ll x=0,f=1;char ch=getchar();
    while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
    while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}
    return x*f;
}
inline void P(int x)
{
    Num=0;if(!x){putchar('0');puts("");return;}
    while(x>0)CH[++Num]=x%10,x/=10;
    while(Num)putchar(CH[Num--]+48);
    puts("");
}
//**************************************************************************************

int g[300][300];
int main()
{
    //freopen("test.txt","r",stdin);
    int n;
    while(scanf("%d",&n)!=EOF)
    {
        memset(g,0,sizeof(g));
        for(int i=0;i<n;i++)
            for(int j=0;j<n;j++)
                g[i][j]=read();
        int ans=0;
        for(int i=0;i<n;i++)
        {
            for(int j=0;j<n;j++)
            {
                if(g[i][j])
                {
                    ans++;
                    g[i][j]=0;
                    g[j][i]=0;
                }
            }
        }
        cout<<(ans+n)*2<<endl;
    }
}

 

转载于:https://www.cnblogs.com/qscqesze/p/4524900.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值