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

G++过不了,C++过了,附代码

Posted by zxj1015 at 2015-04-21 00:00:03 on Problem 1650
#include <iostream>
#include <stdio.h>
#include <vector>
#include <set>
#include <algorithm>
#include <cmath>
#include <string.h>

using namespace std;

double abs(double a, double b) {
  return a > b ? a - b : b - a;
}

int Gcd(int a, int b) {
  if (a < b) {
    swap(a, b);
  }
  if (b == 0) {
    return a;
  }
  return Gcd(b, a % b);
}

int main() {
  freopen("/Users/zxj/Desktop/poj_input.txt", "r", stdin);
  double A;
  int L;
  scanf("%lf %d", &A, &L);
  int D = 0;
  int N = 0;
  double min_value = 1000000000;
  for (int i = 1; i <= L; ++i) {
    int N1 = i * A;
    int N2 = i * A + 1;
    if (N1 > L ) {
      N1 = L - 1;
      N2 = L;
    } else if (N2 > L) {
      N2 = L;
    }
    if (abs(A, N1 * 1.0f / i) < min_value) {
      min_value = abs(A, N1 * 1.0f / i);
      D = i;
      N = N1;
    }
    if (abs(A, N2 * 1.0f / i) < min_value) {
      min_value = abs(A, N2 * 1.0f / i);
      D = i;
      N = N2;
    }
  }
  int gcd = Gcd(N, D);
  N /= gcd;
  D /= gcd;
  printf("%d %d\n", N, D);
  return 0;
}

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