Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1546764
  • 博文数量: 327
  • 博客积分: 10000
  • 博客等级: 上将
  • 技术积分: 3556
  • 用 户 组: 普通用户
  • 注册时间: 2005-04-05 21:28
个人简介

东黑布衣,流浪幽燕。 真诚善良,值得信赖。

文章分类

全部博文(327)

我的朋友

分类: BSD

2005-04-12 09:59:05

题目的大意是X轴上有N个失重的磁力物质,这N个磁力物质的位置恒定不变。现放一个物体,在
这N个磁力物质的引力作用下会在一个平衡的位置停止下来。求这个平衡的位置坐标。
There exist
n magnetic materials in a gravity-free(失重) space. The cent
er of each magnetic
material is the location, i.e. the spatial coordina
te (x,y,z). Coordinates y and z of n m
agnetic materials are the same, a
nd only the coordinates of
x are different. In other words, it is assum
ed the magnetic materials exist in a s
traight line. The location of the
m
agnetic materials is never changed by any external force. Now, when an
object is locat
ed in a random position in the straight in which n magne
tic materials exist, gravitational
force acts from each magnetic materi
al. T
he gravitational force acting from a magnetic material on the obje
ct is obtained by the distance(
d) between the magnetic material and the
object and by the masses of the magnetic material and the object.

 

Formula for calculating the gravitational force acting from a magnetic
material on the object:

F = G*m1*m2/(d*d), G is a positive constant.
                    

     x ---------------------------------------------------

Between the magnetic materials on the left and those on the right, the
object moves in the direction of magnetic materials with bigger force.
It is when that the two gravitational forces become the same, the objec
t stops moving. Find the point where both forces become the same. When
there are n magnetic materials, n-1 points of balance exist.

Pulling force of left magnetic materials  = Pulling force of right magnetic materials

     x ---------------------------------------------------

                    

                Point of balance: where both forces become the same

Note that errors of the coordinate values must be less than 10-9(1e-9).

[Input]

A total of 10 test cases are given. For each test case, the number of m
agnetic materials N is written. In the next line, N x coordinate values,
and N mass values are input sequentially. The number(N) of magnetic mat
erials comes between 2 and 10 (2
N 10).

[Output]

For each test case, print #C in the first line with C being case numb
er. Leave a blank space and print out x coordinate values of the points
of balance in the same line. If the coordinate value has 10 or more dig
its after the decimal point, print only up to 10 digits.

[Input/output example] 

2          <-- test case #1 starts, number of magnetic materials

1 2 1 1    <-- Each magnetic material’s x coordinate values, and mass values (coordinate coordinate mass mass).

2          <-- test case #2 starts, number of magnetic materials

1 2 1 1000

. . .

 

Output

#1 1.5000000000

#2 1.0306534300

. . .

 
///

Real input

10                case数
2 第一个case,两个磁力物质
1 2 1 1  位置 位置 质量 质量 [注:object的质量对结果没有影响]
2
1 2 1 1000
2
457 468 333 321
3
1 2 3 1 2 1
4
2 3 5 7 3 2 7 5
5
3 11 12 19 29 542 661 450 521 366    
6
42 75 88 94 113 144 669 551 355 344 294 155
7
62 86 279 323 363 516 579 810 749 736 297 136 107 52
8
10 34 64 73 93 97 101 122 466 463 441 373 315 292 225 83
10
9 14 38 39 48 73 179 190 207 302 560 497 640 722 437 259 449 470 709 520



Real Output


#1 1.5000000000
#2 1.0306534300
#3 462.5504629633
#4 1.4060952085 2.5939047915
#5 2.5328594461 3.7271944335 6.0999536409
#6 6.3428568767 11.5477377494 15.9641592998 24.9267991615
#7 57.8805685415 81.8651598883 91.0573691382 105.0835650491 133.2934094881
#8 74.2211477711 190.6837563313 305.8269181686 348.3304429927 470.2694219293 555.4943093854
#9 21.5171374463 47.9890597763 68.6536668433 82.9131954023 95.0052272762 99.1999097770 116.4978330953
#10 11.5573600056 24.0238341337 38.4847676134 44.6137453708 64.7500445424 126.9908128982 184.3221650927 197.9760596291 266.0574653677


Code:VS2005上编译通过
  1. #include "stdafx.h"
  2. #include <stdio.h>
  3. #define LOCAL_DEBUG
  4. #ifdef LOCAL_DEBUG
  5. #pragma warning(disable : 4996)
  6. #endif

  7. #define MAX_SIZE (10)
  8. #define PRECISION (1e-13)
  9. #define DOUBLE_ZERO (1e-13)

  10. double CalcForce(double x1, double x2, int m)
  11. {
  12.     double d = x1 > x2 ? (x1 - x2) : (x2 - x1);
  13.     return (double)m / (d * d);
  14. }

  15. double FindFinePos(int x[], int m[], int N, double left, double right)
  16. {
  17.     double pos = (left + right) / 2.0;
  18.     double force;
  19.     int i;

  20.     if (right-left < PRECISION && right-left > -PRECISION)
  21.     {
  22.         return pos;
  23.     }

  24.     force = 0.0;
  25.     i=0;
  26.     while(i<N){
  27.         if (x[i] < pos)
  28.             force += CalcForce(x[i], pos, m[i]);
  29.         else if (x[i] > pos)
  30.             force -= CalcForce(x[i], pos, m[i]);
  31.         i++;
  32.     }

  33.     if (force > DOUBLE_ZERO)
  34.     {
  35.         pos = FindFinePos(x, m, N, pos, right);
  36.     }
  37.     else if (force < -DOUBLE_ZERO)
  38.     {
  39.         pos = FindFinePos(x, m, N, left, pos);
  40.     }

  41.     return pos;
  42. }

  43. int _tmain(int argc, _TCHAR* argv[])
  44. {
  45.     int tc;
  46.     int T;

  47.     int N;
  48.     int x[MAX_SIZE];
  49.     int m[MAX_SIZE];
  50.     int i;
  51.     double pos;

  52. #ifdef LOCAL_DEBUG
  53.     freopen("ADV04_i.txt", "r", stdin);
  54.     freopen("ADV04_o.txt", "w", stdout);
  55. #endif

  56.     setbuf(stdout, NULL);
  57.     scanf("%d", &T);
  58.     tc=1;
  59.     while(tc<=T)
  60.     {
  61.         scanf("%d", &N);
  62.         i=0;
  63.         while(i<N){
  64.             scanf("%d", &x[i]);
  65.             i+=1;
  66.         }
  67.         i=0;
  68.         while(i<N){
  69.             scanf("%d", &m[i]);
  70.             i++;
  71.         }

  72.         printf("#%d", tc);
  73.         i=0;
  74.         while(i<N-1){
  75.             pos = FindFinePos(x, m, N, x[i], x[i + 1]);
  76.             printf(" %.10f", pos);
  77.             i++;
  78.         }

  79.         printf("\n");
  80.         tc+=1;
  81.     }

  82.     return 0;
  83. }
Code:VS2005上编译通过

[注]本题并不复杂,但是如果没有做过的话还真是不好处理。
for循环全部改为while循环,for循环更紧凑,但是目前我更喜欢while


阅读(2298) | 评论(1) | 转发(0) |
给主人留下些什么吧!~~

zhln2016-09-05 10:14:47

有一个欧巴桑在首饰店里看到二只一模一样的手环。一个标价五百五十元,另一个却只标价二百五十元。她大为心喜,立刻买下二百五十元的手环,得意洋洋的走出店门。临出去前,听到里面的店员悄悄对另一个店员说:「看吧,这一招屡试不爽。」
(注:试探如饵,可以轻而易举的使许多人显露出贪婪的本性,然而那常常是吃亏受骗的开始。)