UVA-3708-Graveyard

来源:互联网 发布:新浪微博淘宝版入口 编辑:程序博客网 时间:2024/05/21 12:36

我的代码:模拟搜索并移动:

#include<stdio.h>#include<iostream>#include<math.h>using namespace std;int main(){    int n,m;    while(cin>>n>>m)    {        double before[1005],after[2005],ji = 10000.0/n,ans = 0;        for(int i = 0;i < n;i++) before[i] = i * ji;        ji = 10000.0/(n + m);        for(int i = 0;i < n + m;i++) after[i] = i * ji;        for(int i = 0;i < n;i++)            for(int j = 0;j < m + n - 1;j++)            if(after[j] <= before[i] && after[j + 1] >= before[i])            {                if(before[i] - after[j] >= after[j + 1] - before[i]) ans += after[j + 1] - before[i];                else ans += before[i] - after[j];            }        printf("%.4lf\n",ans);    }    return 0;}
//刘汝佳的代码,妙!
#include <stdio.h>#include<iostream>#include<math.h>using namespace std;int main(){    int n,m;    while(cin>>n>>m)    {        double ans = 0;        for(int i = 1;i < n;i++)        {            double pos = 1.0*(m + n) * i / n;            ans += fabs(pos - floor(pos + 0.5)) / (m + n) *10000.0;        }        printf("%.4lf\n",ans);    }    return 0;}




0 0
原创粉丝点击