package main
import "fmt"
func main() {
for i := 1; i <= 9; i++{
for j := 1;j <= i;j++{
fmt.Printf("%d * %d = %d\t",j,i,i*j)
}
fmt.Println()
}
}
2、水仙花数
package main
import (
"fmt"
"math"
)
func main() {
for num := 100;num <= 999;num++{
var i = num / 100
var j = num / 10%10
var k = num %10
if math.Pow(float64(i),3)+math.Pow(float64(j),3)+math.Pow(float64(k),3) == float64(num){
fmt.Println(num)
}
}
}
3、百钱百鸡
package main
import "fmt"
func main() {
for cock := 0;cock <20;cock++{
for hen :=0;hen < 33;hen ++{
for chick := 0;chick <= 100;chick += 3{
if(5*cock+3*hen+chick/3 == 100 && cock+hen+chick ==100){
fmt.Printf("鸡翁%d只,鸡肉%d只,小鸡%d只\n",cock,hen,chick)
}
}
}
}
}
4、打印等腰三角形
package main
import "fmt"
func main() {
var line int =10
for i := 0;i <line ; i++{
for j :=0;j<line-i-1;j++{
fmt.Print(" ")
}
for k :=0;k<i*2+1;k++{
fmt.Print("*")
}
fmt.Println()
}
}
5、斐波那契数列
package main
import "fmt"
const NUM = 10
func fibonacci(n int) int{
if n < 2{
return n
}
return fibonacci(n-2) + fibonacci(n-1)
}
func main() {
var i int
for i =0;i<NUM;i++{
fmt.Printf("%d\t",fibonacci(i))
}
}
6、
package main
import "fmt"
const NUM = 10
func jc(n int) int{
sum := 0
for i:=0;i<=n;i++{
sum+=i*i
}
return sum
}
func main() {
var i int
for i =0;i<NUM;i++{
fmt.Printf("%d\t",jc(i))
}
}
7、阶乘
package main
import "fmt"
func Fa(n uint64) (result uint64){
if(n >0){
result = n*Fa(n-1)
return result
}
return 1
}
func main() {
var i int = 5
fmt.Printf("%d的阶乘是%d\n",i,Fa(uint64(i)))
}
8、统计输入字符个数
package main
import "fmt"
func main() {
var str string
fmt.Scan(&str)
slice := []byte(str)
m := make(map[byte]int)
for i :=0;i<len(slice);i++{
m[slice[i]]++
}
for k,v := range m{
fmt.Printf("%c:%d\n",k,v)
}
}