Swift | 输入一个正整数,把它的各位数字前后颠倒一下,并输出颠倒后的结果

输入一个正整数,把它的各位数字前后颠倒一下,并输出颠倒后的结果. 例如 123456 输出 654321

读取输入的函数

func scanf()->String?{
        let fileHandle = NSFileHandle.init(fileDescriptor:STDIN_FILENO)
        let data = fileHandle.availableData
        let str = String.init(data: data,encoding:NSUTF8StringEncoding)
        if str == nil{
               return nil
        }
        return(str! as NSString).substringToIndex((str! as NSString).length - 1)as String
}
func changgeNum(num: Int)->Int{
       var number = num  
       var count = 0
       while number != 0{
              count += 1
              number /= 10
  }
      var flag = Int(pow(10,Doublt(count - 1)))
      var shuzu = num
      var sum = 0
      for _ in 1...count{
      sum += shuzu % 10 * flag
      shuzu /= 10
      flag /= 10
    }
return sum
}
var s = Int(scanf()!)!
print(changgeNum(s))

你可能感兴趣的:(Swift | 输入一个正整数,把它的各位数字前后颠倒一下,并输出颠倒后的结果)