New Go
This commit is contained in:
		
							
								
								
									
										46
									
								
								BasicSyntax/prime/Circul.go
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										46
									
								
								BasicSyntax/prime/Circul.go
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,46 @@
 | 
			
		||||
package main
 | 
			
		||||
 | 
			
		||||
import (
 | 
			
		||||
	"fmt"
 | 
			
		||||
	"math"
 | 
			
		||||
)
 | 
			
		||||
 | 
			
		||||
func circle(x1, y1, x2, y2 float64) float64 {
 | 
			
		||||
	return math.Hypot(x2-x1, y2-y1)
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func main() {
 | 
			
		||||
	var n int
 | 
			
		||||
	fmt.Scan(&n)
 | 
			
		||||
 | 
			
		||||
	if n <= 0 {
 | 
			
		||||
		fmt.Println("0.00")
 | 
			
		||||
		return
 | 
			
		||||
	}
 | 
			
		||||
	if n == 1 {
 | 
			
		||||
		fmt.Println("0.00")
 | 
			
		||||
		return
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	var x1, y1 float64
 | 
			
		||||
	fmt.Scan(&x1, &y1)
 | 
			
		||||
 | 
			
		||||
	if n == 2 {
 | 
			
		||||
		var x2, y2 float64
 | 
			
		||||
		fmt.Scan(&x2, &y2)
 | 
			
		||||
		fmt.Printf("%.2f\n", circle(x1, y1, x2, y2))
 | 
			
		||||
		return
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	firstX, firstY := x1, y1
 | 
			
		||||
	total := 0.0
 | 
			
		||||
	for i := 1; i < n; i++ {
 | 
			
		||||
		var x2, y2 float64
 | 
			
		||||
		fmt.Scan(&x2, &y2)
 | 
			
		||||
		total += circle(x1, y1, x2, y2)
 | 
			
		||||
		x1, y1 = x2, y2
 | 
			
		||||
	}
 | 
			
		||||
	total += circle(x1, y1, firstX, firstY)
 | 
			
		||||
 | 
			
		||||
	fmt.Printf("%.2f\n", total)
 | 
			
		||||
}
 | 
			
		||||
							
								
								
									
										23
									
								
								BasicSyntax/prime/main.go
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										23
									
								
								BasicSyntax/prime/main.go
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,23 @@
 | 
			
		||||
package main
 | 
			
		||||
 | 
			
		||||
import "fmt"
 | 
			
		||||
 | 
			
		||||
func prime(p int) bool {
 | 
			
		||||
	for i := 2; i*i <= p; i++ {
 | 
			
		||||
		if p % i == 0 {
 | 
			
		||||
			return false
 | 
			
		||||
		}
 | 
			
		||||
	}
 | 
			
		||||
	return true
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func main(){
 | 
			
		||||
	var n int
 | 
			
		||||
	fmt.Scan(&n)
 | 
			
		||||
	if prime(n) {
 | 
			
		||||
		fmt.Println("Y")
 | 
			
		||||
	} else {
 | 
			
		||||
		fmt.Println("N")
 | 
			
		||||
	}
 | 
			
		||||
  
 | 
			
		||||
}
 | 
			
		||||
							
								
								
									
										33
									
								
								BasicSyntax/prime/realnum.go
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										33
									
								
								BasicSyntax/prime/realnum.go
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,33 @@
 | 
			
		||||
package main
 | 
			
		||||
 | 
			
		||||
import "fmt"
 | 
			
		||||
 | 
			
		||||
// 定义字符串反转函数
 | 
			
		||||
func reverse(s string) string {
 | 
			
		||||
	runes := []rune(s)
 | 
			
		||||
	for i, j := 0, len(runes)-1; i < j; i, j = i+1, j-1 {
 | 
			
		||||
		runes[i], runes[j] = runes[j], runes[i]
 | 
			
		||||
	}
 | 
			
		||||
	return string(runes)
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func main() {
 | 
			
		||||
	var n string
 | 
			
		||||
	fmt.Scan(&n)
 | 
			
		||||
 | 
			
		||||
	// 检查是否以 + 或 - 开头
 | 
			
		||||
	if n[0] == '-' || n[0] == '+' {
 | 
			
		||||
		fmt.Println("NULL")
 | 
			
		||||
		return
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	// 反转字符串
 | 
			
		||||
	n = reverse(n)
 | 
			
		||||
 | 
			
		||||
	// 去掉前导零
 | 
			
		||||
	for len(n) > 1 && n[0] == '0' {
 | 
			
		||||
		n = n[1:]
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	fmt.Println(n)
 | 
			
		||||
}
 | 
			
		||||
							
								
								
									
										24
									
								
								BasicSyntax/prime/suffix.go
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										24
									
								
								BasicSyntax/prime/suffix.go
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,24 @@
 | 
			
		||||
package main
 | 
			
		||||
import "fmt"
 | 
			
		||||
 | 
			
		||||
func check(a, b int) bool {
 | 
			
		||||
	//默认前面的小于后面的
 | 
			
		||||
	for a >= 1 {
 | 
			
		||||
		if a % 10 != b % 10 {
 | 
			
		||||
			return false
 | 
			
		||||
		}
 | 
			
		||||
		a /= 10
 | 
			
		||||
		b /= 10
 | 
			
		||||
	}
 | 
			
		||||
	return true
 | 
			
		||||
}
 | 
			
		||||
func main() {
 | 
			
		||||
	var n int
 | 
			
		||||
	fmt.Scan(&n)
 | 
			
		||||
	for i := 1 ; i <= n; i++ {
 | 
			
		||||
		x := i * i
 | 
			
		||||
		if check(i, x) {
 | 
			
		||||
			fmt.Printf("%d ", i)
 | 
			
		||||
		}
 | 
			
		||||
	}
 | 
			
		||||
}
 | 
			
		||||
							
								
								
									
										59
									
								
								BasicSyntax/prime/toeng.go
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										59
									
								
								BasicSyntax/prime/toeng.go
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,59 @@
 | 
			
		||||
package main
 | 
			
		||||
 | 
			
		||||
import "fmt"
 | 
			
		||||
 | 
			
		||||
func numberToWords(n int) string {
 | 
			
		||||
	if n < 0 || n >= 1000 {
 | 
			
		||||
		return "ERR"
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	ones := []string{"zero", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine"}
 | 
			
		||||
	teens := []string{"ten", "eleven", "twelve", "thirteen", "fourteen", "fifteen",
 | 
			
		||||
		"sixteen", "seventeen", "eighteen", "nineteen"}
 | 
			
		||||
	tens := []string{"", "", "twenty", "thirty", "forty", "fifty", "sixty", "seventy", "eighty", "ninety"}
 | 
			
		||||
 | 
			
		||||
	words := []string{}
 | 
			
		||||
 | 
			
		||||
	// 百位
 | 
			
		||||
	if n >= 100 {
 | 
			
		||||
		words = append(words, ones[n/100]+" hundred")
 | 
			
		||||
		n %= 100
 | 
			
		||||
		if n != 0 {
 | 
			
		||||
			words = append(words, "and")
 | 
			
		||||
		}
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	// 十位和个位
 | 
			
		||||
	if n >= 20 {
 | 
			
		||||
		if n%10 != 0 {
 | 
			
		||||
			words = append(words, tens[n/10]+"-"+ones[n%10])
 | 
			
		||||
		} else {
 | 
			
		||||
			words = append(words, tens[n/10])
 | 
			
		||||
		}
 | 
			
		||||
	} else if n >= 10 {
 | 
			
		||||
		words = append(words, teens[n-10])
 | 
			
		||||
	} else if n > 0 {
 | 
			
		||||
		words = append(words, ones[n])
 | 
			
		||||
	} else if len(words) == 0 { // n == 0
 | 
			
		||||
		words = append(words, "zero")
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	return joinWords(words)
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func joinWords(words []string) string {
 | 
			
		||||
	res := ""
 | 
			
		||||
	for i, w := range words {
 | 
			
		||||
		if i > 0 {
 | 
			
		||||
			res += " "
 | 
			
		||||
		}
 | 
			
		||||
		res += w
 | 
			
		||||
	}
 | 
			
		||||
	return res
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func main() {
 | 
			
		||||
	var n int
 | 
			
		||||
	fmt.Scan(&n)
 | 
			
		||||
	fmt.Println(numberToWords(n))
 | 
			
		||||
}
 | 
			
		||||
							
								
								
									
										31
									
								
								BasicSyntax/prime/transame.go
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										31
									
								
								BasicSyntax/prime/transame.go
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,31 @@
 | 
			
		||||
package main
 | 
			
		||||
 | 
			
		||||
import "fmt"
 | 
			
		||||
 | 
			
		||||
func transpose(matrix [][]int) bool {
 | 
			
		||||
	for i := range matrix {
 | 
			
		||||
		for j := range matrix[i] {
 | 
			
		||||
			if matrix[i][j] != matrix[j][i] {
 | 
			
		||||
				return false
 | 
			
		||||
			}
 | 
			
		||||
		}
 | 
			
		||||
	}
 | 
			
		||||
	return true
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func main() {
 | 
			
		||||
	var n int
 | 
			
		||||
	fmt.Scan(&n)
 | 
			
		||||
	matrix := make([][]int, n)
 | 
			
		||||
	for i := range matrix {
 | 
			
		||||
		matrix[i] = make([]int, n)
 | 
			
		||||
		for j := range matrix[i] {
 | 
			
		||||
			fmt.Scan(&matrix[i][j])
 | 
			
		||||
		}
 | 
			
		||||
	}
 | 
			
		||||
	if transpose(matrix) {
 | 
			
		||||
		fmt.Println("YES")
 | 
			
		||||
	} else {
 | 
			
		||||
		fmt.Println("NO")
 | 
			
		||||
	}
 | 
			
		||||
}
 | 
			
		||||
							
								
								
									
										41
									
								
								BasicSyntax/prime/transpose.go
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										41
									
								
								BasicSyntax/prime/transpose.go
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,41 @@
 | 
			
		||||
package main
 | 
			
		||||
 | 
			
		||||
import "fmt"
 | 
			
		||||
 | 
			
		||||
func transpose(matrix [][]int) [][]int {
 | 
			
		||||
	m := len(matrix)
 | 
			
		||||
	n := len(matrix[0])
 | 
			
		||||
	res := make([][]int, n)
 | 
			
		||||
	for i := range res {
 | 
			
		||||
		res[i] = make([]int, m)
 | 
			
		||||
	}
 | 
			
		||||
	for i := 0; i < m; i++ {
 | 
			
		||||
		for j := 0; j < n; j++ {
 | 
			
		||||
			res[j][i] = matrix[i][j]
 | 
			
		||||
		}
 | 
			
		||||
	}
 | 
			
		||||
	return res
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func main() {
 | 
			
		||||
	var n int
 | 
			
		||||
	fmt.Scan(&n)
 | 
			
		||||
	matrix := make([][]int, n)
 | 
			
		||||
	for i := range matrix {
 | 
			
		||||
		matrix[i] = make([]int, n)
 | 
			
		||||
		for j := range matrix[i] {
 | 
			
		||||
			fmt.Scan(&matrix[i][j])
 | 
			
		||||
		}
 | 
			
		||||
	}
 | 
			
		||||
	res := transpose(matrix)
 | 
			
		||||
	for i := range res {
 | 
			
		||||
		for j := range res[i] {
 | 
			
		||||
			if j != n{
 | 
			
		||||
				fmt.Printf("%d ", res[i][j])
 | 
			
		||||
			} else {
 | 
			
		||||
				fmt.Printf("%d", res[i][j])
 | 
			
		||||
			}
 | 
			
		||||
		}
 | 
			
		||||
		fmt.Println()
 | 
			
		||||
	}
 | 
			
		||||
}
 | 
			
		||||
							
								
								
									
										15
									
								
								BasicSyntax/prime/yoseff.go
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										15
									
								
								BasicSyntax/prime/yoseff.go
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,15 @@
 | 
			
		||||
package main
 | 
			
		||||
import "fmt"
 | 
			
		||||
 | 
			
		||||
func main(){
 | 
			
		||||
	var n, s, m int
 | 
			
		||||
	fmt.Scan(&n, &s, &m)
 | 
			
		||||
	arr := make([]int, n)
 | 
			
		||||
	for i := 1; i <= n; i++ {
 | 
			
		||||
		arr[i] = i
 | 
			
		||||
	}
 | 
			
		||||
	while len(arr) > 1 {
 | 
			
		||||
		s = (s + m - 1) % len(arr)
 | 
			
		||||
		arr = append(arr[:s], arr[s+1:]...)
 | 
			
		||||
	}
 | 
			
		||||
}
 | 
			
		||||
		Reference in New Issue
	
	Block a user