1 / 3

11032: Function Overloading

11032: Function Overloading. ★★★☆☆ 題組: Problem Set Archive with Online Judge 題號: 1 1032:Function Overloading 解題者: 許智祺 解題日期: 200 7 年 5 月 8 日 題意: 判對輸入之數字是否為 self number ,或輸入兩數字之間有幾個 self number 。. 題意範例: input: 3 output: 101 Case 1: 91 1 9 Case 2: 5

lilian
Download Presentation

11032: Function Overloading

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. 11032:Function Overloading • ★★★☆☆ • 題組:Problem Set Archive with Online Judge • 題號:11032:Function Overloading • 解題者:許智祺 • 解題日期:2007年5月8日 • 題意:判對輸入之數字是否為self number,或輸入兩數字之間有幾個self number。

  2. 題意範例: input: 3 output: 101 Case 1: 91 1 9 Case 2: 5 20 Case 3: -1 所謂的self number就是指無法由別的數經由本身加上他的sum of digit組成, 例如:101=91+9+1 17=13+1+3 因此101和17就不是self number,而20就是一個self number。 輸入的第一個數代表要輸入多少筆數據,若輸入一個數代表詢問此數是否為self number若是則回傳-1,不是則回傳是由哪個是組成,若輸入兩個數子表示詢問此區間內有多少個self number。

  3. 解法:利用gets來讀取輸入,可以避免用scanf和cin時會略過空格後面的數的問題,將數字存入一個char陣列,用for迴圈檢查,若讀到空格表示輸入的數為兩個,若直接讀到eof表示值有一個數,再利用atoi將字元轉換成數字後存入一個int變數中,丟入題目提供的副程式作計算。解法:利用gets來讀取輸入,可以避免用scanf和cin時會略過空格後面的數的問題,將數字存入一個char陣列,用for迴圈檢查,若讀到空格表示輸入的數為兩個,若直接讀到eof表示值有一個數,再利用atoi將字元轉換成數字後存入一個int變數中,丟入題目提供的副程式作計算。 • 解法範例: 12 210 a[0]=1 a[1]=2 a[2]=‘ ’ a[3]=2 a[4]=1 a[5]=0 a[6]=‘eof’ 利用for迴圈判斷可知a[2]為空格,temp=3 num1=atoi(a); 可將12從字元轉會成數字並存入num1 num2=atou(&a[temp]);將210轉成數字並存入num2 • 討論:程式提供了方程式的解法,但在執行的效率上不佳,為純粹的暴力法,但也想不出其他方法,時間複雜度n2。

More Related