HDU Problem b

版权声明:本文为博主原创文章,转载请标明原博客。 https://blog.csdn.net/sdau_fangshifeng/article/details/80317228
Problem Description
Eddy begins to like painting pictures recently ,he is sure of himself to become a painter.Every day Eddy draws pictures in his small room, and he usually puts out his newest pictures to let his friends appreciate. but the result it can be imagined, the friends are not interested in his picture.Eddy feels very puzzled,in order to change all friends 's view to his technical of painting pictures ,so Eddy creates a problem for the his friends of you.<br>Problem descriptions as follows: Given you some coordinates pionts on a drawing paper, every point links with the ink with the straight line, causes all points finally to link in the same place. How many distants does your duty discover the shortest length which the ink draws?<br>
 

Input
The first line contains 0 < n <= 100, the number of point. For each point, a line follows; each following line contains two real numbers indicating the (x,y) coordinates of the point. <br><br>Input contains multiple test cases. Process to the end of file.<br>
 

Output
Your program prints a single real number to two decimal places: the minimum total length of ink lines that can connect all the points. <br>
 

Sample Input
3
1.0 1.0
2.0 2.0
2.0 4.0
 

Sample Output
3.41
 

题目大意:

在直角坐标系中,给你n的点的坐标

问:将这些点全部连接的最短距离,

输入,第一行表示几个点,接下来每一行有点的x,y坐标(实数)

输出:保留两位小数,

思路:kruskal算法,

计算时最好都用double类型数据

code:

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
using namespace std;
const int maxn=0xfffff;
struct point1//点
{
    double x,y;
} a[101];
struct point//kruskal所需的数据
{
    //point x,y;
    int x,y;
    double v;
} s[10001];
int f[10001];
double dis(point1 p,point1 q)//计算距离
{
    double xx=p.x-q.x;
    double yy=p.y-q.y;
    return sqrt(xx*xx+yy*yy);
}
int Find(int x)
{
    if(f[x]!=x)
        f[x]=Find(f[x]);
    return f[x];
}
void Union(int x,int y)
{
    int p=Find(x);
    int q=Find(y);
    if(p!=q)
        f[p]=q;
}
bool cmp(point p,point q)
{
    return p.v<q.v;
}
int main()
{
    int n;
    while(scanf("%d",&n)!=EOF)//注意多组数据
    {
        int cnt=0;
        double ans=0;
        int k=0;
        for(int i=1; i<=n; ++i)
        {
            cin>>a[i].x>>a[i].y;
        }
        for(int i=1; i<=n; ++i)
        {
            for(int j=1; j<=n; ++j)
            {
                if(i!=j)
                {
                    s[++cnt].x=i;
                    s[cnt].y=j;
                    s[cnt].v=dis(a[i],a[j]);
                }
            }
        }
        sort(s+1,s+cnt+1,cmp);
        for(int i=1; i<=cnt; ++i)
            f[i]=i;
        for(int i=1; i<=cnt; ++i)
        {
            if(Find(s[i].x)!=Find(s[i].y))
            {
                Union(s[i].x,s[i].y);
                ans+=s[i].v;
                ++k;
            }
            //if(k==n-1)
                //break;
        }
        printf("%0.2lf\n",ans);
    }
}
//完美的代码








hdu 1002 A + B Problem II 菜鸟求助

06-02

Problem DescriptionrnI have a very simple problem for you. Given two integers A and B, your job is to calculate the Sum of A + B.rnrn rnrnInputrnThe first line of the input contains an integer T(1<=T<=20) which means the number of test cases. Then T lines follow, each line consists of two positive integers, A and B. Notice that the integers are very large, that means you should not process them by using 32-bit integer. You may assume the length of each integer will not exceed 1000.rnrn rnrnOutputrnFor each test case, you should output two lines. The first line is "Case #:", # means the number of the test case. The second line is the an equation "A + B = Sum", Sum means the result of A + B. Note there are some spaces int the equation. Output a blank line between two test cases.rnrn rnrnSample Inputrn2rn1 2rn112233445566778899 998877665544332211rn rnrnSample OutputrnCase 1:rn1 + 2 = 3rnrnCase 2:rn112233445566778899 + 998877665544332211 = 1111111111111111110rnrnrn[code=c]rn#includern#includernrnint main()rnrn int n,i=0,j;rn scanf("%d",&n);rn while(ilb?la:lb;rn for(j=1;j<=la;j++)rn sum[j]=a[j-1]-'0';rn if(la>=lb) rn rn for(j=q;lb>0;j--,lb--)rn sum[j]+=(b[lb-1]-'0'); rn rn else rn rn for(j=q;la>0;j--,lb--,la--)rn sum[j]+=(b[lb-1]-'0'+a[la-1]-'0');rn for(j;lb>0;lb--,j--)rn sum[j]=(b[lb-1]-'0');rn rn for(j=q+1;j>0;j--)rn rn if(sum[j]>=10)rn rn sum[j-1]++;rn sum[j]-=10;rn rn rn printf("Case %d:\n%s + %s = ",i,a,b);rn if(sum[0]!=0)printf("%d",sum[0]);rn for(j=1;j

没有更多推荐了,返回首页

私密
私密原因:
请选择设置私密原因
  • 广告
  • 抄袭
  • 版权
  • 政治
  • 色情
  • 无意义
  • 其他
其他原因:
120
出错啦
系统繁忙,请稍后再试