index — ti25-glauchau-code @ master

Meine Lösungen (oder auch nicht) für die Programmieraufgaben in der TI25 an der Staatlichen Studienakademie Glauchau

ima/2025-10-08/binaerzaehler.go (view raw)

 1
 2
 3
 4
 5
 6
 7
 8
 9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
package main

import (
	"fmt"
	"strconv"
)
func binaer(decimal int) string {
	n := int64(decimal)
	return strconv.FormatInt(n, 2)
}

func countUp(end int) []string {
	var rows []string
	for i := range end {
		rows = append(rows, binaer(i))
	}
	return rows
}

func countOnes(rows []string, k int) {
	for _, try := range rows {
		ones := 0
		for _, digit := range try {
			if digit == '1' {
				ones++
			}
		}
		if ones == k {
			fmt.Println(try)
		}
	}
}

func main() {
	k := 3
	n := 4

	rows := countUp(1 << n)
	fmt.Println(rows)
	countOnes(rows, k)
}