How to change 12345
to 54321
?
With a string, you can change the string to a rune
, and reverse it, but you cannot do the same for an integer. I have searched and found no one talking about this. Examples131415
>>> 514131
1357
>>> 7531
123a
>>> ERROR
-EDIT-
I was thinking, why not create a slice
and index that?
Then I realized that you can't index int
(http://play.golang.org/p/SUSg04tZsc)
MY NEW QUESTION IS
How do you index an int?
OR
How do you reverse a int?
反向int golang
- 写回答
- 好问题 0 提建议
- 追加酬金
- 关注问题
- 邀请回答
-
3条回答 默认 最新
- duancoubeng5909 2016-03-13 18:01关注
Here is a solution that does not use indexing an
int
package main import ( "fmt" ) func reverse_int(n int) int { new_int := 0 for n > 0 { remainder := n % 10 new_int *= 10 new_int += remainder n /= 10 } return new_int } func main() { fmt.Println(reverse_int(123456)) fmt.Println(reverse_int(100)) fmt.Println(reverse_int(1001)) fmt.Println(reverse_int(131415)) fmt.Println(reverse_int(1357)) }
Result:
654321 1 1001 514131 7531
本回答被题主选为最佳回答 , 对您是否有帮助呢?解决 无用评论 打赏 举报
悬赏问题
- ¥15 如何绘制动力学系统的相图
- ¥15 对接wps接口实现获取元数据
- ¥20 给自己本科IT专业毕业的妹m找个实习工作
- ¥15 用友U8:向一个无法连接的网络尝试了一个套接字操作,如何解决?
- ¥30 我的代码按理说完成了模型的搭建、训练、验证测试等工作(标签-网络|关键词-变化检测)
- ¥50 mac mini外接显示器 画质字体模糊
- ¥15 TLS1.2协议通信解密
- ¥40 图书信息管理系统程序编写
- ¥20 Qcustomplot缩小曲线形状问题
- ¥15 企业资源规划ERP沙盘模拟