• ny269 VF


    VF

    时间限制:1000 ms  |  内存限制:65535 KB
    难度:2
     
    描述
    Vasya is the beginning mathematician. He decided to make an important contribution to the science and to become famous all over the world. But how can he do that if the most interesting facts such as Pythagor’s theorem are already proved? Correct! He is to think out something his own, original. So he thought out the Theory of Vasya’s Functions. Vasya’s Functions (VF) are rather simple: the value of the Nth VF in the point S is an amount of integers from 1 to N that have the sum of digits S. You seem to be great programmers, so Vasya gave you a task to find the milliard VF value (i.e. the VF with N = 109) because Vasya himself won’t cope with the task. Can you solve the problem?
     
    输入
    There are multiple test cases.
    Integer S (1 ≤ S ≤ 81).
    输出
    The milliard VF value in the point S.
    样例输入
    1
    样例输出
    10
    
    
    题意讲解:本题目是让你求10亿个数的每个位数的数字之和为s的,共有都少个;
    例如:和为1的有1,10,100,1000,10000,100000,1000000,10000000,100000000,1000000000共有十个;
    题意大概就是如此,输入一个S,然后求出和为s,的共有多少个;
    可以用动态规划来做:
    状态转移方程:

    初始条件:dp[0][j]=1   j=1、2…9

    状态转移方程:

    dp[i][j]表示:和为i,不超过j个数相加的符合条件的数有多少个

    dp[i][j]=sum{dp[i-k][j-1]  k=0、1…9}

    代码如下:
           

     1 #include<iostream>
     2 using namespace std;
     3 int a[83][13];
     4 void fun()
     5 {
     6     int sum,n,i,j,k;
     7     for(i=0;i<=11;i++)
     8         a[0][i]=1;
     9     for(i=1;i<=81;i++)
    10         for(j=1;j<=9;j++)
    11         for(k=0;k<10 && i-k>=0;k++)
    12            if(a[i-k][j-1])
    13                a[i][j]+=a[i-k][j-1];
    14 }
    15 int main()
    16 {
    17     int t;
    18     fun();
    19     while(cin>>t)
    20     {
    21         if(t==1)
    22             cout<<10<<endl;
    23         else
    24         cout<<a[t][9]<<endl;
    25     }
    26     return 0;
    27 }

  • 相关阅读:
    面试题32
    面试题28. 对称的二叉树
    面试题55
    面试题04. 二维数组中的查找
    面试题58
    面试题57. 和为s的两个数字
    如果Python对于磁盘没有写入权限,还会运行吗?
    Python中的import语句
    Python决定一个变量时局部的,还是全局的,是在编译期
    Python中的Comprehensions和Generations
  • 原文地址:https://www.cnblogs.com/lovychen/p/3361963.html
Copyright © 2020-2023  润新知