首页 > 其他 > 详细

UVa 11032 Function Overloading

时间:2015-01-09 22:27:47      阅读:333      评论:0      收藏:0      [点我收藏+]

Problem F

Function Overloading

Time limit: 3 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

 


#include <iostream>
#include <algorithm>
#include <sstream>
#include <string>
#include <cstdio>
using namespace std;
const int maxn=10000005;

int a[2],cnt,b[maxn],dp[maxn];
string str;

int sod(int num)
{
    int p=num,ret=0;
    while(p)
    {
        ret+=(p%10);
        p/=10;
    }
    return ret;
}

void initial()
{
    for(int i=1; i<maxn; i++)   b[i]=-1;
    for(int i=1; i<maxn; i++)
    {
        int t=i+sod(i);
        if(t<maxn && b[t]==-1)  b[t]=i;
    }
    for(int i=1; i<maxn; i++)
    {
        dp[i]=dp[i-1];
        if(b[i]==-1)  dp[i]++;
    }
}

void input()
{
    getline(cin,str);
    stringstream ss(str);
    cnt=0;
    while(ss>>a[cnt])  cnt++;
}

void solve(int co)
{
    if(cnt==1)  printf("Case %d: %d\n",co,b[a[0]]);
    else
    {
        if(a[0]<a[1])  swap(a[0],a[1]);
        printf("Case %d: %d\n",co,dp[a[0]]-dp[a[1]-1]);
    }
}
int main()
{
    int T;
    initial();
    scanf("%d",&T);
    getchar();
    for(int co=1; co<=T; co++)
    {
        input();
        solve(co);
    }
    return 0;
}


 

UVa 11032 Function Overloading

原文:http://blog.csdn.net/u012596172/article/details/42559969

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!