24h購物| | PChome| 登入
2012-12-24 16:16:44| 人氣436| 回應0 | 上一篇 | 下一篇

[UVA][dp] 11032 - Function Overloading

推薦 0 收藏 0 轉貼0 訂閱站台

Problem F

Function Overloading

Time limit: 1 second

 

Overloading refers to the use of the same thing for different purposes. C++ permits overloading of functions. This means we can use the same function name to create functions that perform a variety of different tasks.

 

Consider two functions with the same name, but different number of parameters.

 

int fun(int a, int b) {

int ans = 0;

int i, j, cnt;

for(i=a; i<=b; i++) {

cnt = 0;

for(j=1; j<=i; j++) {

if( j + sod(j) == i ) cnt++;

}

if( cnt == 0 ) ans++;

}

return ans;

}

 

int fun(int a) {

     int i;

     for(i=1; i<=a; i++){

          if( i + sod(i) == a ){

              return i;

          }

}

return -1;

}

 

Where, sod(n) = sum of digits of n.

So,

sod(13) = 1 + 3 = 4 and

sod(204) = 2 + 0 + 4 = 6.

 

Input

 

The first line of the input file will contain an integer that gives the number of test cases. Each case will be given in one line. A line might contain one integer or two integers. All integers will be in the range [1, 10000000].

Total number of test cases will be less than 1000.

If the input contains two integers then the first function is called and if it contains one integer then the second function is called. The corresponding integers are passed as parameters.

 

Output

 

For each case, first output the case number followed by the return value of the corresponding function.

 

Sample Input

Output for Sample Input

3

101

1 9

20

Case 1: 91

Case 2: 5

Case 3: -1

 

ProblemSetter: Sohel Hafiz

Special Thanks: Jane Alam Jan

Next Generation Contest 2


設多一點變數比較快

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <iostream>
using namespace std;
int dp1[10001000] = {};
char dp0[10001000] = {};
int main() {
    int k1, k2, k3, k4, k5, k6, k7, num, num2;
    for(k1 = 0; k1 < 10; k1++)
    for(k2 = 0; k2 < 10; k2++)
    for(k3 = 0; k3 < 10; k3++)
    for(k4 = 0; k4 < 10; k4++)
    for(k5 = 0; k5 < 10; k5++)
    for(k6 = 0; k6 < 10; k6++)
    for(k7 = 0; k7 < 10; k7++) {
        num = k7+k6*10+k5*100+k4*1000+k3*10000+k2*100000+k1*1000000;
        num2 = num+k1+k2+k3+k4+k5+k6+k7;
        //printf("%d\n", num);
        if(dp0[num2] == 0)    dp0[num2] = num2-num;
        if(num)
            dp1[num] = dp1[num-1] + (dp0[num] == 0);
    }
    dp1[10000000] = dp1[9999999] + (dp0[10000000] == 0);
    int t, cases = 0;
    char cmd[500];
    scanf("%d", &t);
    cin.ignore(100, '\n');
    while(t--) {
        gets(cmd);
        int a, b;
        printf("Case %d: ", ++cases);
        if(sscanf(cmd, "%d %d", &a, &b) == 2) {
            if(a > b)   puts("0");
            else
                printf("%d\n", dp1[b]-dp1[a-1]);
        } else {
            sscanf(cmd, "%d", &a);
            printf("%d\n", dp0[a] ? a-dp0[a] : -1);
        }
    }
    return 0;
}

台長: Morris
人氣(436) | 回應(0)| 推薦 (0)| 收藏 (0)| 轉寄
全站分類: 不分類 | 個人分類: UVA |
此分類下一篇:[UVA] 11403 - Binary Multiplication
此分類上一篇:[UVA][parse] 11878 - Homework Checker

是 (若未登入"個人新聞台帳號"則看不到回覆唷!)
* 請輸入識別碼:
請輸入圖片中算式的結果(可能為0) 
(有*為必填)
TOP
詳全文