Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package main
- import "fmt"
- type Plan struct {
- inv1, inv2 float64
- profit float64
- }
- func main() {
- capital, years := 10000.0, 4
- profit := func(enterprise int, x float64) float64 {
- if enterprise == 1 {
- return 0.4 * x
- }
- return 0.3 * x
- }
- returnRate := func(enterprise int, x float64) float64 {
- if enterprise == 1 {
- return 0.5 * x
- }
- return 0.8 * x
- }
- dp := make([]map[float64]Plan, years+1)
- for y := range dp {
- dp[y] = make(map[float64]Plan)
- }
- // Заполняем последний год
- for c := 0.0; c <= capital; c += 10 {
- dp[years][c] = Plan{inv1: c, profit: profit(1, c)}
- }
- // Обратный проход
- for y := years - 1; y >= 1; y-- {
- for c := 0.0; c <= capital; c += 10 {
- if y == 1 && c != capital {
- continue
- }
- max, best := -1.0, 0.0
- for x := 0.0; x <= c; x += 10 {
- current := profit(1, x) + profit(2, c-x)
- returned := returnRate(1, x) + returnRate(2, c-x)
- total := current + dp[y+1][returned].profit
- if total > max {
- max, best = total, x
- }
- }
- dp[y][c] = Plan{inv1: best, inv2: c - best, profit: max}
- }
- }
- // Восстановление решения
- var result []Plan
- current := capital
- total := 0.0
- for y := 1; y <= years; y++ {
- p := dp[y][current]
- result = append(result, p)
- total += profit(1, p.inv1) + profit(2, p.inv2)
- current = returnRate(1, p.inv1) + returnRate(2, p.inv2)
- }
- // Вывод результатов
- fmt.Println("┌──────────┬────────────┬────────────┐")
- fmt.Println("│ Год │ Предпр. 1 │ Предпр. 2 │")
- fmt.Println("├──────────┼────────────┼────────────┤")
- for i, p := range result {
- fmt.Printf("│ %-8d │ %10.2f │ %10.2f │\n", i+1, p.inv1, p.inv2)
- if i < len(result)-1 {
- fmt.Println("├──────────┼────────────┼────────────┤")
- }
- }
- fmt.Println("└──────────┴────────────┴────────────┘")
- fmt.Printf("\nИтоговый доход: \033[1;32m%.2f\033[0m\n", total)
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement