Sean's Note

2008年7月21日 星期一

[PHP] odbc_num_rows()

網路上看了一下, 取得的值不如預期, 大部分是因為 odbc 的 driver 不支援,

詳細我也不太清楚, 反正要取得 Total 有幾筆資料, 方法很多,

query 裡用 COUNT(*) 就行了.


2007年10月27日 星期六

[C/C++] ACM - 10539 - Almost Primes Numbers

這題的 long long 真是超級機車的, 遇到了一些奇怪的 coding 問題, submit 了大概快三十次才 AC 吧 = =

 
[問題一]
 
quicksort 裡的 compare function, 如果型態不是 long long,
 
以下兩種寫法都行, 只需對應參數的型態.
 
但如果是 long long 就只能用寫法二了, 真不知道為什麼? @@
 
寫法一 :
 int compare( const void *a, const void *b)
return (long long *)a - (long long*)b) ; }
 
寫法二 :
int compare(const void *a,const void *b)
{
   long long *c, *d;
   c=(long long *)a;
   d=(long long *)b;
  
   if(*c>*d)
      return 1;
   else if(*c==*d)
      return 0;
   else
      return -1;
 
[問題二]
 
寫法一跟寫法二的意思是一樣的, 可是寫法一和寫法二 nonprime[] 裡存放的東西卻不一樣.
 
寫法一才是正確的, 寫法二卻不是?
 
寫法一:
long long temp;
long long nonprime[80080];
for (i = 0, k = 0; k < 80070; i++)
  {
  temp = prime[i];
  while ((temp *= prime[i]) <= 1e12)
    { nonprime[k++] = temp; }
  }
 
寫法二:
long long nonprime[80080];
for (i = 0, k = 0; k < 80070; i++)
  {
  nonprime[k] = prime[i]*prime[i];  // prime[i] * prime[i]  都會小於等於 1e12,  所以寫法一的迴圈也會跑  while (nonprimep[k] * prime[i] <= 1e12)
    {
    nonprime[k+1] = nonprime[k] * prime[k];
    k++; 
    }
   }
 
[心得]
 
突然發現 cout 可以正確的印出 long long 的值,
 
以後遇到 long long 的題目還是用 C++ 寫好了.


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,
不過還不知道是為什麼. @@