zoukankan      html  css  js  c++  java
  • Computers 递推题 sum[i][j]=max(sum[i-1][i-1]+c+sum[i][j],sum[i-1][j]);

    Description

    Everybody is fond of computers, but buying a new one is always a money challenge. Fortunately, there is always a convenient way to deal with. You can replace your computer and get a brand new one, thus saving some maintenance cost. Of course, you must pay a fixed cost for each new computer you get.

    Suppose you are considering an n year period over which you want to have a computer. Suppose you buy a new computer in year y,1<=y<=n Then you have to pay a fixed cost c, in the year y, and a maintenance cost m(y,z) each year you own that computer, starting from year y through the year zz<=n, when you plan to buy - eventually - another computer.

    Write a program that computes the minimum cost of having a computer over the n year period.

    Input

    The program input is from a text file. Each data set in the file stands for a particular set of costs. A data set starts with the cost c for getting a new computer. Follows the number n of years, and the maintenance costs m(y,z)y=1..nz=y..n. The program prints the minimum cost of having a computer throughout the n year period.

    White spaces can occur freely in the input. The input data are correct and terminate with an end of file.

    Output

    For each set of data the program prints the result to the standard output from the beginning of a line.

    Sample Input

    3
    3
    5 7 50
    6 8
    10

    Sample Output

    19
    ***********************************************************************************************************************************************************
    经典
    ***********************************************************************************************************************************************************
     1 #include<iostream>
     2 #include<string>
     3 #include<cstring>
     4 #include<cstdio>
     5 #include<vector>
     6 using namespace std;
     7 int sum[1001][1001];
     8 int n,c,i,j,k;
     9 int main()
    10 {
    11     while(scanf("%d",&c)!=EOF)
    12     {
    13         scanf("%d",&n);
    14         for(i=0;i<n;i++)
    15          for(j=i;j<n;j++)
    16           scanf("%d",&sum[i][j]);
    17         for(i=0;i<n;i++)
    18          for(j=i;j<n;j++)
    19          {
    20             if(i==0)sum[i][j]=sum[i][j]+c;
    21             else
    22               sum[i][j]=min(sum[i-1][i-1]+c+sum[i][j],sum[i-1][j]);//表示i年的最小花费
    23          }
    24          printf("%d
    ",sum[n-1][n-1]);
    25     }
    26     return 0;
    27 }
    View Code
  • 相关阅读:
    Python_Example_异常try except 学习/经验/示例
    Python_Example_pycharm&&socket 网络编程 通讯交互
    Python_Example_Pycharm与MySQL交互 初始化互通
    Python_Example_MySQL&Navicat学习
    Python_Example_常用正则&&表达式符号
    Python_Example_modbus串口_完成模拟DSP通信_实现程序
    Python_Example_Excel读取后存储显示_实现程序
    Python_Example_文件路径读取返回_文件目录选择文件_实现程序
    Python_Example_modbus协议 串口 _接收_处理_响应 实现程序
    Python_Example_字符串转换实现程序
  • 原文地址:https://www.cnblogs.com/sdau--codeants/p/3364831.html
Copyright © 2011-2022 走看看