poj3684 题目链接:http://poj.org/problem?id=3684
一下代买只有样例通过了,没有别的数据。 找不到出错的原因,怎么办呢?精度吗??
原因查明:先排序,最后总体加上 2*R*i。为什么不先加上2*R*i,然后排序呢??
2 1 10 10 100 2 10 10 100
//#include <bits/stdc++.h>
#include <map>
#include <set>
#include<iostream>
#include <stdio.h>
#include<cstring>
#include <cmath>
#include <algorithm>
#include <iomanip>
#define LL long long int
using namespace std;
const int MM=100;
double height[MM];
const double g=10.0;
double solve(int H,int T){
double t= sqrt( 2.0*H /g );
int num= (int)( T/t );
if( num%2==0) { return H-1.0/2 * g * pow( T - (num)*t , 2) ; }
else { return H-1.0/2 * g * pow( (num+1)*t - T , 2) ; }
}
int main()
{
//freopen("r.in", "r", stdin);
// freopen("r.out", "w", stdout);
int test;
cin >> test;
while(test--){
int N, H , R, T;
cin >> N >> H >> R >> T;
memset(height, 0 ,sizeof(height));
for(int i=0;i<N;i++){
//height[i] = solve( H + 2*0.01*R*i, T-i); // 第i个小球只花了 T-i 时间
height[i] = solve( H , T-i) + 2*0.01*R*i;
}
sort(height+0, height+N);
for(int i=0;i<N;i++){
cout << fixed << setprecision(2) << height[i] << " ";
//printf("%.2f%c",height[i], i+1==N ? '\n' :' ');
}
cout << endl;
//用了两种方法输出,poj 未通过。
}
return 0;
}
AC代码,通过了!!
//#include <bits/stdc++.h>
#include <map>
#include <set>
#include<iostream>
#include <stdio.h>
#include<cstring>
#include <cmath>
#include <algorithm>
#include <iomanip>
#define LL long long int
using namespace std;
const int MM=100;
double height[MM];
const double g=10.0;
double solve(int H,int T){
if(T<0) return H;
double t= sqrt( 2.0*H /g );
int num= (int)( T/t );
if( num%2==0) { return H-1.0/2 * g * pow( T - num*t , 2) ; }
else { return H-1.0/2 * g * pow( (num+1)*t - T , 2) ; }
}
int main()
{
//freopen("r.in", "r", stdin);
// freopen("r.out", "w", stdout);
int test;
cin >> test;
while(test--){
int N, H , R, T;
cin >> N >> H >> R >> T;
memset(height, 0 ,sizeof(height));
for(int i=0;i<N;i++){
//height[i] = solve( H + 2*0.01*R*i, T-i); // 第i个小球只花了 T-i 时间
height[i] = solve( H , T-i);
//height[i] = solve( H , T-i) + height[i] + 2*0.01*R*i ;
//为什么先加上2*0.01*R*i 然后排序就无法通过呢?
}
sort(height+0, height+N);
for(int i=0;i<N;i++){
cout << fixed << setprecision(2) << height[i] + 2*0.01*R*i << " ";
//printf("%.2f%c",height[i], i+1==N ? '\n' :' ');
}
cout << endl;
//用了两种方法输出,poj 未通过。
//结论:poj会忽略掉末尾空格,忽略掉最后的空行。
}
return 0;
}