程序不考虑负数的情况,若数字含有0,则逆序形式也含有0,如输入为100,则输出为001
数据范围:
package main import ( "fmt" "bufio" "os" ) func main() { scanner := bufio.NewScanner(os.Stdin) scanner.Scan() numStr := scanner.Text() for i := len(numStr)-1; i>=0; i--{ fmt.Printf("%s", string(numStr[i])) } }
package main import( "fmt" ) func main() { var num string fmt.Scanf("%s",&num) fmt.Println(reverseString(num)) } func reverseString(s string) string { bytes := []byte(s) for from, to := 0, len(bytes)-1; from < to; from, to = from+1, to-1 { bytes[from], bytes[to] = bytes[to], bytes[from] } return string(bytes) }
package main import( "bufio" "os" "fmt" "strings" ) func main(){ scanner := bufio.NewScanner(os.Stdin) scanner.Scan() num := scanner.Text() res := make([]string,0) for i:=len(num)-1;i>= 0;i--{ res = append(res,string(num[i])) } fmt.Println(strings.Join(res,"")) }
package main import ( "fmt" "bytes" ) func reverseString(i int) string { var result bytes.Buffer for { remainder := i % 10 result.WriteString(fmt.Sprintf("%d", remainder)) i = i / 10 if i == 0 { break } } return result.String() } func main() { var num int fmt.Scanf("%d", &num) fmt.Println(reverseString(num)) }