UVa 11032 Function Overloading


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;
}
时间: 2024-07-30 10:17:43

UVa 11032 Function Overloading的相关文章

function overloading/ declare function

Declare a function To declare a function without identifying the argument list, you can do it in this way: void say hello(...); here, you use three point (...) to indicate that the function have no argument. function overloading Keep in mind that the

javascript 函数重载 overloading

函数重载 https://en.wikipedia.org/wiki/Function_overloading In some programming languages, function overloading or method overloading is the ability to create multiple methods of the same name with different implementations. Calls to an overloaded functi

Why Not Specialize Function Templates?

Why Not Specialize Function Templates? This article appeared in C/C++ Users Journal, 19(7), July 2001. 原文在此: http://www.gotw.ca/publications/mill17.htm While the title of this article is a question, it could also be made into a statement: this articl

部署zabbix监控mysql (一) 安装zabbix

部署zabbix监控mysql (1)安装LAMP环境 [[email protected] ~]# yum -y installmysql-server http php (2)安装zabbix web所需要的依赖包 [[email protected] ~]# yum -y installmysql-dev gcc net-snmp-devel curl-devel perl-DBI php-gd php-mysql php-bcmathphp-mbstring php-xml 安装Fpin

Atitit.编程语言原理---方法重载的实现与设计&#160;调用方法的原理

Atitit.编程语言原理---方法重载的实现与设计 调用方法的原理 1. 重载包括:普通方法的重载和构造方法的重载 1 1.1. 横向重载”和“纵向重载”1 1.2. 方法签名通过  方法名称,参数数量,参数类型+返回类型 参数顺序(命名参数)来组成1 1.3. 重载(重新载选方法流程,如下图所示2 2. 重载的实现方法::argus参数 vs  默认值.可选参数法 可选参数3 2.1. __call()函数是php类的默认魔法函数 3 2.2. Java通过反射调用方法重载 执行方法4 2.

[C/CPP系列知识] C++中extern “C” name mangling -- Name Mangling and extern “C” in C++

http://www.geeksforgeeks.org/extern-c-in-c/ C++函数重载(function overloading),但是C++编译器是如何区分不同的函数的呢?----是通过在函数名是加些信息来区不同的函数,即所谓的Name Mangling.C++标准并没有对name mangling技术,各个编译器可以添加不同的信息. 考虑下面的函数 int f (void) { return 1; } int f (int) { return 0; } void g (voi

c++构造函数问题,初始化和赋值问题

默认构造函数(就是没有参数的构造函数) The Default ConstructorThe default constructor is the constructor used to create an object when you don't provide explicit initialization values. That is, it's the constructor used for declarations like this: Stock stock1;  // use

Effective C++ -----条款41:了解隐式接口和编译期多态

classes和templates都支持接口(interface)和多态(polymorphism). 对classes而言接口是显式的(explicit),以函数签名为中心.多态则是通过virtual函数发生于运行期. 对template参数而言,接口是隐式的(implicit),奠基于有效表达式.多态则是通过template具现化和函数重载解析(function overloading resolution)发生于编译期.

《Effective C++》:条款41-条款42

条款41了解隐式接口和编译期多态 条款42了解typename的双重意义 条款41:了解隐式接口和编译期多态 面向对象编程总是以显示接口(explicit interfaces)和运行期多态(runtime polymorphism)来解决问题.例如 class Widget{ public: Widget(); virtual ~Widget(); virtual std::size_t size() const; virtual void normalize(); void swap(Wid