oj 的 go语言运行环境感觉有些问题
error输出是入参违法时的输出,这里输入了一次入参,但是代码执行了两次且到了不同的分支,起初以为是代码换行符的原因,但是删除换行符后输入了一次入参,代码执行了三次,产生了三个结果,之前没有遇到过这个问题,本地 centos go1.9.2 linux/386 没有遇到这个问题。
代码:
package main import( "fmt" "math" ) func zeroToNineteen(num int64){ exps := []string{"one","two","three","four","five","six","seven","eight","nine","ten","eleven","twelve","thirteen","fourteen","fivteen","sixteen","seventeen","eighteen","nineteen"} fmt.Print(exps[num-1]) } func twentyToNinetyNine(num int64){ if num<20 { zeroToNineteen(num) return } a,b := num/10,num%10 exps := []string{"twenty","thirty","fourty","fifty","sixty","seventy","eighty","ninety"} fmt.Print(exps[a-2]) if b!=0 { fmt.Print(" ") zeroToNineteen(b) } } func oneHundredTo999(num int64){ if num<100 { twentyToNinetyNine(num) return } exps := " hundred" a,b := num/100,num%100 zeroToNineteen(a) fmt.Print(exps) if b!=0 { fmt.Print(" and ") twentyToNinetyNine(b) } } func oneThousandTo999999(num int64){ if num<1000 { oneHundredTo999(num) return } exps := " thousand " a,b := num/1000,num%1000 oneHundredTo999(a) fmt.Print(exps) if b!=0 { oneHundredTo999(b) } } func oneMillionTo999999999(num int64){ if num<1000000 { oneThousandTo999999(num) return } exps := " million " a,b := num/1000000,num%1000000 oneHundredTo999(a) fmt.Print(exps) if b!=0 { oneThousandTo999999(b) } } func oneBillionToInt32Max(num int64){ if num<1000000000 { oneMillionTo999999999(num) return } exps := " billion " a,b := num/1000000000,num%1000000000 oneHundredTo999(a) fmt.Print(exps) if b!=0 { oneMillionTo999999999(b) } } func numToEng(num int64){ if num==0 { fmt.Println("zero") return } if num<0 { fmt.Print("negative ") } //注:int64最小值在取绝对值后会发生越界,数字太大这里不单独处理了 num = int64(math.Abs(float64(num))) if num>1000 && num%1000<100{ num_2 := num%1000 num_1 := num - num_2 oneBillionToInt32Max(num_1) fmt.Print("and ") twentyToNinetyNine(num_2) fmt.Println() return } oneBillionToInt32Max(num) fmt.Println() } func main(){ for{ var num int64 _,err := fmt.Scan(&num); if err != nil{ fmt.Println("error") return } numToEng(num) } }