Computers

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 maintenanc e cost. Of course, you must pay a fixed cost 
for each new computer you get.  
 
Suppose you are considering a 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  z,  z<=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..n,  z=y..n. The program prints the minimum 
cost of having a computer throughout the n year period. 
 

Output

White spaces can occur freely in the input. The input data are correct and terminate with 
an end of file. For each set of data the program prints the result to the standard output from the 
beginning of a line. An input/output sample is in the table below. There is a single data set. The 
cost for getting a new computer is  c=3.  The time period n is  n=3 years, and the maintenance 
costs are:  
 
•   For the first computer, which is certainly bought:  m(1,1)=5,  m(1,2)=7,  m(1,3)=50, 
•   For the second computer, in the event the current computer is replaced:  m(2,2)=6, 
m(2,3)=8 
•   For the third computer, in the event  the current computer is replaced:  m(3,3)=10 .  
 
 The result for the data set is the minimum cost 19. 
 

Sample Input

3
3
5 7 50
6 8
10

Sample Output

19
题意:用n年电脑问题,第一年用c元买电脑(这一年有维修费m(1,1)),第二年有两种选择
1)不买电脑,用第一年买的,但维修费高,第一第二年维修费为m(1,2);
2)新买电脑,花费c,并支付本年维修费m(2,2)。
。。。如此,用完n年,求最小消费额。
分析:如题,可模拟此过程-->
5 7 50
6 8
10
可变换为,每年需要花费的金额-->
5 2 43
6 2
10
模拟过程:由第一年推至第n年
第一年--》
5 2 43
6 2
  10
第二年--》
5 7 43
(5+3+6) 2
10
第三年--》
5 7 50
14 14+2
min(14,7)+3+10
可知最小消费为16+3=19
。。。模拟题!
代码:
#include <iostream>
#include <stdio.h>
using namespace std;
int num[1001][1001];
int main()
{
    int c,n,i,j,k,min,temp;
	while(scanf("%d",&c)!=EOF)
	{
		scanf("%d",&n);
		for(i=1;i<=n;i++)
		{
			temp=0;
			for(j=i;j<=n;j++)
			{
				scanf("%d",&num[i][j]);
				if(j>i) num[i][j]-=temp;
				temp+=num[i][j];
			}
		}
		for(i=2;i<=n;i++)
		{
			min=1000000;
			for(j=1;j<=i;j++)
			{
				if(j==i)
				{
					for(k=1;k<=j-1;k++)
						if(min>num[k][i-1])
							min=num[k][i-1];
					num[i][j]+=min+c;
					continue;
				}
				num[j][i]+=num[j][i-1];
			}
		}
		min=1000000;
		for(i=1;i<=n;i++)
			if(num[i][n]<min)
				min=num[i][n];
		printf("%d\n",min+c);
	}
    return 0;    
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
public class ComputerSystem extends JFrame{ private Vector<Computer> computers; private JButton button_Start; private JPanel panel0; private JDialog dialog_enter1; private final String[][] StaffLists = {{"p1","p1"},{"p2","p2"},{"p3","p3"},{"m1","m1"},{"m2","m2"}}; public ComputerSystem(Vector<Computer> computers){ this.computers = computers; setLayout(null); panel0 = new JPanel(); JLabel label = new JLabel(new ImageIcon("")); panel0.add(label); getContentPane().add(panel0); button_Start = new JButton("Click to login",new ImageIcon("ComputerStore.png"));//这个图片整不上去,最后整一个,好看 add(button_Start); setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); button_Start.setBounds(5,5,400,150); button_Start.setHorizontalTextPosition(SwingConstants.RIGHT); button_Start.setVerticalTextPosition(SwingConstants.CENTER); button_Start.setFont(new Font("黑体",Font.BOLD,40)); public class Main { public static void main(String[] args) { //Computer[] computers = new Computer[999]; Vector<Computer> computers = new Vector<>(999); Scanner scanner = null; int i = -1; try { scanner = new Scanner(new BufferedReader(new FileReader("C:\Users\86137\Desktop\Computer.txt"))); String item; try { while (scanner.hasNext()) { i++; item = scanner.nextLine(); String[] cols = item.split(","); if(Objects.equals(cols[0], "Desktop PC")){ computers.add(new Desktop(cols[0], cols[1], cols[2], cols[3], cols[4], Integer.valueOf(cols[7]), Integer.valueOf(cols[5]), Integer.valueOf(cols[6]))); } if(Objects.equals(cols[0], "Laptop")){ computers.add(new Laptop(cols[0], cols[1], cols[2], cols[3], cols[4], Integer.valueOf(cols[8]), Integer.valueOf(cols[5]), Integer.valueOf(cols[6]), Double.valueOf(cols[7]))); } if(Objects.equals(cols[0], "Tablet")){ computers.add(new Tablet(cols[0], cols[1], cols[2], cols[3], cols[4], Integer.valueOf(cols[6]), Double.valueOf(cols[5]))); } // computers[i].category = cols[0]; // computers[i].Type = cols[1]; // computers[i].ID = cols[2]; // computers[i].Brand = cols[3]; // computers[i].CPU_Family = cols[4]; // computers[i].Price = Integer.valueOf(cols[5]); } }finally { if (scanner != null) { scanner.close(); } } } catch (IOException e) { e.printStackTrace(); } ComputerSystem computerSystem = new ComputerSystem(computers); computerSystem.setTitle("Computer Products Management System"); computerSystem.setSize(700,300); computerSystem.setLocationRelativeTo(null); computerSystem.setVisible(true); computerSystem.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); // TableFilterDemo demo = new TableFilterDemo(computers); } }这段代码为什么无法呈现按钮和图片。 一些类没给你,不影响
最新发布
05-25

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值