Last active
May 18, 2026 17:58
-
-
Save blinkinglight/5861685afc5f6c8828696a6f03bc6414 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| === PRADINĖ BŪSENA === | |
| ID ParentID Cores RAM HDD Būsena | |
| --------------------------------------------------------- | |
| 1 0 40 256 100 Laisvas | |
| 2 0 40 256 100 Laisvas | |
| 3 0 40 256 100 Laisvas | |
| === BANDOME PARDUOTI: 4 vnt. (24 Core, 128GB RAM, 20TB HDD) === | |
| Parduotas serveris #1 imant resursus iš Serverio ID: 1 | |
| -> Sukurtas likutis (ParentID: 1): 16 Core, 128GB RAM, 80TB HDD | |
| Parduotas serveris #2 imant resursus iš Serverio ID: 2 | |
| -> Sukurtas likutis (ParentID: 2): 16 Core, 128GB RAM, 80TB HDD | |
| Parduotas serveris #3 imant resursus iš Serverio ID: 3 | |
| -> Sukurtas likutis (ParentID: 3): 16 Core, 128GB RAM, 80TB HDD | |
| Parduotas serveris #1 imant resursus iš Serverio ID: 4 | |
| -> Sukurtas likutis (ParentID: 4): 0 Core, 0GB RAM, 60TB HDD | |
| Parduotas serveris #2 imant resursus iš Serverio ID: 5 | |
| -> Sukurtas likutis (ParentID: 5): 0 Core, 0GB RAM, 60TB HDD | |
| Parduotas serveris #1 imant resursus iš Serverio ID: 6 | |
| -> Sukurtas likutis (ParentID: 6): 12 Core, 124GB RAM, 60TB HDD | |
| Parduotas serveris #2 imant resursus iš Serverio ID: 9 | |
| -> Sukurtas likutis (ParentID: 9): 8 Core, 120GB RAM, 40TB HDD | |
| Parduotas serveris #3 imant resursus iš Serverio ID: 10 | |
| -> Sukurtas likutis (ParentID: 10): 4 Core, 116GB RAM, 20TB HDD | |
| Parduotas serveris #4 imant resursus iš Serverio ID: 11 | |
| -> Sukurtas likutis (ParentID: 11): 0 Core, 112GB RAM, 0TB HDD | |
| KLAIDA: Nepavyko parduoti serverio #5! Trūksta 4 Core / 4GB RAM / 20TB HDD viename gabale. | |
| === GALUTINĖ BŪSENA === | |
| ID ParentID Cores RAM HDD Būsena | |
| --------------------------------------------------------- | |
| 1 0 40 256 100 Supjaustytas | |
| 2 0 40 256 100 Supjaustytas | |
| 3 0 40 256 100 Supjaustytas | |
| 4 1 16 128 80 Supjaustytas | |
| 5 2 16 128 80 Supjaustytas | |
| 6 3 16 128 80 Supjaustytas | |
| 7 4 0 0 60 Laisvas | |
| 8 5 0 0 60 Laisvas | |
| 9 6 12 124 60 Supjaustytas | |
| 10 9 8 120 40 Supjaustytas | |
| 11 10 4 116 20 Supjaustytas | |
| 12 11 0 112 0 Laisvas | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package main | |
| import ( | |
| "fmt" | |
| ) | |
| // Server aprašo vieną serverį arba jo likutį | |
| type Server struct { | |
| ID int | |
| ParentID int // Jei 0 - tai originalus serveris. Jei >0 - tai likutis. | |
| Cores int | |
| RAM int // GB | |
| HDD int // TB | |
| IsUsed bool // Žymi, ar šis fizinis vienetas jau buvo perpjautas/parduotas | |
| } | |
| // Inventory saugo visą serverių vektorių | |
| type Inventory struct { | |
| Servers []Server | |
| NextID int | |
| } | |
| // AddServer prideda naują serverį (arba likutį) į vektorių | |
| func (inv *Inventory) AddServer(parentID, cores, ram, hdd int) { | |
| inv.Servers = append(inv.Servers, Server{ | |
| ID: inv.NextID, | |
| ParentID: parentID, | |
| Cores: cores, | |
| RAM: ram, | |
| HDD: hdd, | |
| IsUsed: false, | |
| }) | |
| inv.NextID++ | |
| } | |
| // Sell bando parduoti nurodytą kiekį serverių su nurodytais parametrais | |
| func (inv *Inventory) Sell(count, reqCores, reqRAM, reqHDD int) { | |
| for i := 1; i <= count; i++ { | |
| allocated := false | |
| // Ieškome tinkamo nepanaudoto serverio | |
| for j := 0; j < len(inv.Servers); j++ { | |
| srv := &inv.Servers[j] | |
| // Tikriname, ar serveris dar nepanaudotas ir turi pakankamai VISO reikalingo fizinio kiekio | |
| if !srv.IsUsed && srv.Cores >= reqCores && srv.RAM >= reqRAM && srv.HDD >= reqHDD { | |
| // 1. Žymime originalų serverį kaip iškomplektuotą/supjaustytą | |
| srv.IsUsed = true | |
| fmt.Printf("Parduotas serveris #%d imant resursus iš Serverio ID: %d\n", i, srv.ID) | |
| // 2. Skaičiuojame likučius (atpjautą „štangos“ galą) | |
| leftCores := srv.Cores - reqCores | |
| leftRAM := srv.RAM - reqRAM | |
| leftHDD := srv.HDD - reqHDD | |
| // 3. Jei liko bent kiek resursų, dedame atgal į vektorių su ParentID | |
| if leftCores > 0 || leftRAM > 0 || leftHDD > 0 { | |
| inv.AddServer(srv.ID, leftCores, leftRAM, leftHDD) | |
| fmt.Printf(" -> Sukurtas likutis (ParentID: %d): %d Core, %dGB RAM, %dTB HDD\n", | |
| srv.ID, leftCores, leftRAM, leftHDD) | |
| } | |
| allocated = true | |
| break // Pereiname prie kito kliento norimo serverio | |
| } | |
| } | |
| // Jei perėjome visą vektorių ir neradome pakankamai didelio gabalo: | |
| if !allocated { | |
| fmt.Printf("KLAIDA: Nepavyko parduoti serverio #%d! Trūksta %d Core / %dGB RAM / %dTB HDD viename gabale.\n", | |
| i, reqCores, reqRAM, reqHDD) | |
| } | |
| } | |
| } | |
| // Print parodo dabartinę sandėlio būklę | |
| func (inv *Inventory) Print() { | |
| fmt.Println("\nID\tParentID\tCores\tRAM\tHDD\tBūsena") | |
| fmt.Println("---------------------------------------------------------") | |
| for _, s := range inv.Servers { | |
| status := "Laisvas" | |
| if s.IsUsed { | |
| status = "Supjaustytas" | |
| } | |
| fmt.Printf("%d\t%d\t\t%d\t%d\t%d\t%s\n", s.ID, s.ParentID, s.Cores, s.RAM, s.HDD, status) | |
| } | |
| fmt.Println() | |
| } | |
| func main() { | |
| inv := Inventory{NextID: 1} | |
| // 1. Užpildome pradinį inventorių (3 jūsų minėti serveriai) | |
| inv.AddServer(0, 40, 256, 100) | |
| inv.AddServer(0, 40, 256, 100) | |
| inv.AddServer(0, 40, 256, 100) | |
| fmt.Println("=== PRADINĖ BŪSENA ===") | |
| inv.Print() | |
| // 2. Bandome parduoti 4 serverius (24 Core, 128GB RAM, 20TB HDD) | |
| fmt.Println("=== BANDOME PARDUOTI: 4 vnt. (24 Core, 128GB RAM, 20TB HDD) ===") | |
| inv.Sell(3, 24, 128, 20) | |
| inv.Sell(2, 16, 128, 20) | |
| inv.Sell(1, 4, 4, 20) | |
| // 3. Žiūrime, kas liko sandėlyje po operacijos | |
| fmt.Println("=== GALUTINĖ BŪSENA ===") | |
| inv.Print() | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment