POJ-1003-Hangover

来源:互联网 发布:python 树莓派 编辑:程序博客网 时间:2024/06/10 01:40

核心思想:

考虑建索引,然后查表。两种思路,可以按照card数,也可以按照length。

由于数据量不大,也可以每次从新开始计算。


注记:

1、为什么可以这样来放置card?

涉及到物理中如何求多个物体的质心问题,只要总的质心是在桌子边缘即可。

当只有一个card时,质心在1/2处,因此可以这样设想,当有两个card时,上层在下层的1/2处,然后考虑整体的质心。x = 1/2 - x,可以得到x = 1/4。依次类推,n个card时,最底层的偏移是1/(2n)。

而题目中给出的是1/(n+1),因此,题目的放置方法是不能成立的


2、强制类型转换,double转int的结果?

1.9 = 1                  -1.9 = -1

double floor( double arg );
下取整函数。

double ceil( double arg );

上取整函数。


3、%m.nlf中m和n的取值是否有限制?

好像还没有文章进行分析……


4、浮点数如何比大小?

Why floating-point numbers may lose precision

IEEE浮点表示形式

Floating-point decimal values generally do not have an exact binary representation. This is a side effect of how the CPU represents floating point data. For this reason, you may experience some loss of precision, and some floating-point operations may produce unexpected results.

This behavior is the result of one of the following:

  • The binary representation of the decimal number may not be exact.

  • There is a type mismatch between the numbers used (for example, mixing float and double).

To resolve the behavior, most programmers either ensure that the value is greater or less than what is needed, or they get and use a Binary Coded Decimal (BCD) library that will maintain the precision.

Binary representation of floating-point values affects the precision and accuracy of floating-point calculations. Microsoft Visual C++ uses IEEE floating-point format.

// Floating-point_number_precision.c// Compile options needed: none. Value of c is printed with a decimal // point precision of 10 and 6 (printf rounded value by default) to // show the difference#include <stdio.h>#define EPSILON 0.0001   // Define your own tolerance#define FLOAT_EQ(x,v) (((v - EPSILON) < x) && (x <( v + EPSILON)))int main() {   float a, b, c;   a = 1.345f;   b = 1.123f;   c = a + b;   // if (FLOAT_EQ(c, 2.468)) // Remove comment for correct result   if (c == 2.468)            // Comment this line for correct result      printf_s("They are equal.\n");   else      printf_s("They are not equal! The value of c is %13.10f "                "or %f",c,c);}
For EPSILON, you can use the constants FLT_EPSILON, which is defined for float as 1.192092896e-07F, or DBL_EPSILON, which is defined for double as 2.2204460492503131e-016. You need to include float.h for these constants. These constants are defined as the smallest positive number x, such that x+1.0 is not equal to 1.0. Because this is a very small number, you should employ user-defined tolerance for calculations involving very large numbers.


5、计算机如何处理浮点数

参考《深入理解计算机系统》第2版中文版P67,2.4浮点数


6、C语言stdlib.h中的二分查找函数

void *bsearch(const void *key, const void *base, size_t nmem, size_t size, int (*comp)(cosnt void *, const void *))
key指向所要查找的元素,base指向进行查找的数组,nmem为查找长度,一般为数组长度,size为每个元素所占的字节数,一般用sizeof(...)表示,comp指向比较子函数,它定义比较的规则。

0 0
原创粉丝点击