Advertisement
Catsher

2

May 22nd, 2025
206
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Go 2.29 KB | None | 0 0
  1. package main
  2.  
  3. import "fmt"
  4.  
  5. type Plan struct {
  6.     inv1, inv2 float64
  7.     profit     float64
  8. }
  9.  
  10. func main() {
  11.     capital, years := 10000.0, 4
  12.  
  13.     profit := func(enterprise int, x float64) float64 {
  14.         if enterprise == 1 {
  15.             return 0.4 * x
  16.         }
  17.         return 0.3 * x
  18.     }
  19.  
  20.     returnRate := func(enterprise int, x float64) float64 {
  21.         if enterprise == 1 {
  22.             return 0.5 * x
  23.         }
  24.         return 0.8 * x
  25.     }
  26.  
  27.     dp := make([]map[float64]Plan, years+1)
  28.     for y := range dp {
  29.         dp[y] = make(map[float64]Plan)
  30.     }
  31.  
  32.     // Заполняем последний год
  33.     for c := 0.0; c <= capital; c += 10 {
  34.         dp[years][c] = Plan{inv1: c, profit: profit(1, c)}
  35.     }
  36.  
  37.     // Обратный проход
  38.     for y := years - 1; y >= 1; y-- {
  39.         for c := 0.0; c <= capital; c += 10 {
  40.             if y == 1 && c != capital {
  41.                 continue
  42.             }
  43.  
  44.             max, best := -1.0, 0.0
  45.             for x := 0.0; x <= c; x += 10 {
  46.                 current := profit(1, x) + profit(2, c-x)
  47.                 returned := returnRate(1, x) + returnRate(2, c-x)
  48.                 total := current + dp[y+1][returned].profit
  49.  
  50.                 if total > max {
  51.                     max, best = total, x
  52.                 }
  53.             }
  54.             dp[y][c] = Plan{inv1: best, inv2: c - best, profit: max}
  55.         }
  56.     }
  57.  
  58.     // Восстановление решения
  59.     var result []Plan
  60.     current := capital
  61.     total := 0.0
  62.  
  63.     for y := 1; y <= years; y++ {
  64.         p := dp[y][current]
  65.         result = append(result, p)
  66.         total += profit(1, p.inv1) + profit(2, p.inv2)
  67.         current = returnRate(1, p.inv1) + returnRate(2, p.inv2)
  68.     }
  69.  
  70.     // Вывод результатов
  71.     fmt.Println("┌──────────┬────────────┬────────────┐")
  72.     fmt.Println("│   Год    │ Предпр. 1  │ Предпр. 2  │")
  73.     fmt.Println("├──────────┼────────────┼────────────┤")
  74.     for i, p := range result {
  75.         fmt.Printf("│ %-8d │ %10.2f │ %10.2f │\n", i+1, p.inv1, p.inv2)
  76.         if i < len(result)-1 {
  77.             fmt.Println("├──────────┼────────────┼────────────┤")
  78.         }
  79.     }
  80.     fmt.Println("└──────────┴────────────┴────────────┘")
  81.     fmt.Printf("\nИтоговый доход: \033[1;32m%.2f\033[0m\n", total)
  82. }
  83.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement