Online JudgeProblem SetAuthorsOnline ContestsUser
Web Board
Home Page
F.A.Qs
Statistical Charts
Problems
Submit Problem
Online Status
Prob.ID:
Register
Update your info
Authors ranklist
Current Contest
Past Contests
Scheduled Contests
Award Contest
User ID:
Password:
  Register

Re:无语了,G++一直WA,换成C++就过了

Posted by ttwinsun at 2024-02-15 19:26:42 on Problem 2976
In Reply To:无语了,G++一直WA,换成C++就过了 Posted by:b157 at 2017-10-04 00:08:46
> 有大佬可以解释一下为什么吗?
> 附程序
> 
> 
> #include <cstdio>
> #include <iostream>
> #include <algorithm>
> #include <math.h>
> #include <cmath>
> #define mem0(a) memset(a,0,sizeof(a))
> #define meminf(a) memset(a,0x3f,sizeof(a))
> using namespace std;
> typedef long long ll;
> typedef long double ld;
> typedef double db;
> const int maxn=10005,inf=0x3f3f3f3f;  
> const ll llinf=0x3f3f3f3f3f3f3f3f;   
> const db eps=1e-6;
> 
> struct Pack{
> 	db a,b,d;
> };
> Pack p[maxn];
> 
> bool cmp(Pack x,Pack y) {
> 	return x.d<y.d;
> }
> 
> db Dinkelbach(int n,int k) {
> 	db ans=0,l;
> while (true) {
> 		l=ans;
> 	int i; db s0,s1;
> 	for (i=1;i<=n;i++) {
> 		p[i].d=p[i].a-l*p[i].b;
> 	}
> 	sort(p+1,p+n+1,cmp);
> 	s0=s1=0;
> 	for (i=k+1;i<=n;i++) {
> 		s0+=p[i].a;s1+=p[i].b;
> 	}
> 	ans=s0/s1;
> 	if (fabs(l-ans)<eps) return ans;
> }
> }
> 
> int main() {
> 	int n,k;
> 	scanf("%d%d",&n,&k);
> 	while (n||k) {
> 		int i;
> 		ll s1,s0;
> 		s1=s0=0;
> 		for (i=1;i<=n;i++)
> 			scanf("%lf",&p[i].a);
> 		for (i=1;i<=n;i++) 
> 			scanf("%lf",&p[i].b);
> 		db ans=Dinkelbach(n,k);
> 		printf("%.0lf\n",ans*100.0);
> 		scanf("%d%d",&n,&k);
> 	}
> 	return 0;
> }

您好,亲测这个 `printf("%.0lf\n", ans * 100.0);`,在不同的编译器中结果不一样。
用 `printf("%d\n", int(ans * 100 + 0.5));` 就可以。

Followed by:

Post your reply here:
User ID:
Password:
Title:

Content:

Home Page   Go Back  To top


All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator