Sean's Note

2007年4月29日 星期日

[C/C++] 函式 pow( ) 的用法

這是 math.h 裡的註解

Excess precision when using a 64-bit mantissa for FPU math ops can
cause unexpected results with some of the MSVCRT math functions.  For 
example, unless the function return value is stored (truncating to
53-bit mantissa), calls to pow with both x and y as integral values
sometimes produce a non-integral result.
                                                                               
One workaround is to reset the FPU env to 53-bit mantissa
by a call to fesetenv (FE_PC53_ENV).  Amother is to force storage
of the return value of individual math functions using wrappers.
NB, using these wrappers will disable builtin math functions and
hence disable the folding of function results at compile time when
arguments are constant.
最好不要拿 pow( ) 直接做 Boolen 運算
=================================================
int n = 3;
double temp;
i f ( pow(10, n) == 1000.0 )  <-- which seems alright, but BAD!!
  DO SOMTHING....
temp = pow(10, n);
i f ( temp == 1000.0 )  <-- need another storage, but BETTER!!
  DO SOMTHING....
=================================================

2007年4月21日 星期六

[C/C++] ACM - 628 - Passwords

1st is a Russian who solved 1978 problems,
3rd is a Japanese who solved 1790 problems,
21nd is  a Taiwanese who solved 1278 problems,
1389th is me who solved 170 problems,
They are far far far  away from me.......


這題並不會很難, 但是用到 pow() 函式的時候卻發現了有趣的問題.
情況一:
pow(10, 2) == 100    <= TRUE
pow(10, 3) == 1000  <= TRUE
pow(10, 4) == 10000 <= TRUE
情況二:
n = 2;
pow(10, n) == 100     <= TRUE
n = 3;
pow(10, n) == 1000   <= FALSE
n = 4;
pow(10, n) == 10000 <= TRUE
不知道為什麼會這樣哩  : P
去問問數值分析老師好了, 哈哈.


2006年10月19日 星期四

[C/C++]ACM - 10188 Automated Judge Script ( Notice : 存取字串 )

1st is a Russian who solved 1878 problems,
5th is a Japanese who solved 1698 problems,
14nd is  a Taiwanese who solved 1284 problems,
817nd is  a Taiwanese (CCU student) who solved 266 problems,
2250rd is me who solved 117 problems,
They are far far far  away from me.......


ACM 的題目總是很隱晦 = ="
如果沒有Luck 貓的測資, 這題也有兩星吧 @@
另外, 有特別的測資像是
Input
1
1
Output
Run #1: Accepted
問題出現了, 如何讀掉Input後, 按下 Enter所產生的 /n 呢?
方法一 :
scanf ("%*c");  // 以這題似乎有錯.
方法二 :
scanf ("\n");   // 會讀掉第一次按下的Enter, 如果第二次以後也按Enter也會讀掉, 但是我們只要讀掉第一次.
方法三 :
getchar();  // 只讀掉Input後的Enter, 之後不管按幾次Enter都不會讀掉.

2006年10月6日 星期五

[C/C++] ACM - 10196 Check the check ( Notice : 存取字串 )

如果 Input 剛好是 8 x 8 的陣列, 用下列方法存取時,

for ( i = 0; i < 8;  i++) 
  scanf ("%s", board[i]);
要注意宣告至board[8][9] 或board[8][N] ( N >= 9 ) 
第九個位置, board[i][8] 是用來存放結束字元的.
如果是用
 for ( i = 0; i < 8; i++)
   {
   for ( j = 0; i < 8; i++)
     scanf ("%c", &board[i][j]);
   scanf ("%c"); 
   }
則 N >= 8  即可, 不用考慮結束字元.
但是須注意把換行 \n skip掉, 否則會存放在陣列裡.
PS : 另外, 題目雖已說明不會有黑白棋都被將軍的情況, 但是似乎是有的, 而且有的話, 印出 white king is in check.

2006年9月2日 星期六

[C/C++] ACM - TLE ( Time Limited Exceed )

1st is a Russian who solved 1801 problems,
5th is a Japanese who solved 1620 problems,
13nd is  a Taiwanese who solved 1282 problems,
2287rd is me who solved 112 problems,
They are far far far  away from me....... 


型態宣告的不夠大,
也可能造成程式 TLE ( Time Limited Exceed ),
像是 880 - Cantor Fractions
需要宣告至 unsigned long long
演算法沒錯, 但是如果宣告成 long 或 long long,
就會造成程式 TLE,
不過還不知道是為什麼. @@

2005年11月4日 星期五

[C/C++] 自做隨機函數使亂數產生於0~1之間

#include
#include


double _rand(void)             
{
FILE *file;
double x, value;                  

file = fopen ("rand.txt", "r");  
fscanf(file,"%lf",&x);             
value = x*x + x - 1;            
if (value < 0)
  {
  value = -value;                   
  }
file = fopen ("rand.txt", "w");
fprintf (file, "%lf", value);      
fclose(file);                 
return value;                     

}

int main()
{
again:
printf ("產生了一個亂數為 %.4lf\n", _rand());
system ("pause");
goto again;
return 0;