This is the 24th day of my participation in the August More Text Challenge
L1-003 Single digit statistics (15 points)The Go language | Golang
Input format:
Each input contains 1 test case, that is, a positive integer N of no more than 1000 bits.
Output format:
For each different one digit in N, output the digit D and the number M of occurrences in N in the format of D:M in a line. The output is required in ascending order of D.
Example Input:
100311
Copy the code
No blank line at the end
Example output:
0:2
1:3
3:1
Copy the code
No blank line at the end
Ideas:
- Treat it as a string and compile each character.
- The map key is used to store these characters, and the map value is used to store the quantity.
- Filter out excess elements if there are any.
- And then finally sort it again and output it.
The code is as follows:
package main
import (
"fmt"
"sort"
)
func main(a) {
var str string
//var k map[rune]int // Since it was not initialized after declaring k, it is nil and does not point to any memory address.
k := make(map[rune]int) // The make method is required to allocate the specified memory address. The program can run normally after modification:
_, _ = fmt.Scan(&str)
for _,item := range str { // Add the value of the same key to 1
k[item] += 1
}
j := 0
keys := make([]int.len(k))
for k1 := range k { // Filter duplicate keys
keys[j] = int(k1)
j++
}
sort.Ints(keys) // Sort
for _,item := range keys {
fmt.Printf("%s:%d\n".string(item),k[rune(item)])
}
}
Copy the code
L1-004 Calculation of Celsius temperature (5 marks)The Go language | Golang
Given a temperature F in Fahrenheit, we need to write a program to calculate the temperature C in Celsius. The calculation formula is C=5 x (F−32)/9. The problem ensures that the input and output are within the range of integers.
Input format:
The input gives a temperature in Fahrenheit in one line.
Output format:
Output an integer value of Celsius in the format “Celsius = C” on a line.
Example Input:
150
Copy the code
No blank line at the end
Example output:
Celsius = 65
Copy the code
No blank line at the end
Ideas:
Basic inputs and outputs
Note:
- Input must pass the address
&
Otherwise, an error will be reported. - If they’re given temperature C to output
An integer value
, so we just need to define it asint
Type is good, because go isStrongly typed
Language, so if defined asint
You can calculate it anyway and you end up with, rightint
The division cast
The code is as follows:
package main
import "fmt"
func main(a) {
var F int
_,_ = fmt.Scan(&F)
fmt.Printf("Celsius = %d".5*(F- 32) /9)}Copy the code