题解 | #计算整数位数#

计算整数位数

http://www.nowcoder.com/practice/07232b8720614f7ea185391d1a7206eb

import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);
        int num = scan.nextInt();
        scan.close();

      	// 首先对两种特殊状况进行判断
        if(num==0) System.out.println(0);
        else if(num<0) System.out.println(-1);
        else {
          	// bits记录目前数字的位数
            int bits = 0;
            while (num!=0){
              	// 首先原数除以10并转换成Double,向下取整抹去小数点后数字,然后转换回int
                num = (int)Math.floor((double) num/10);
                bits++;
            }
            System.out.println(bits);
        }

    }
}
全部评论

相关推荐

不愿透露姓名的神秘牛友
10-29 21:14
疯犬丨哈士奇:喜欢你的人会主动表白,对你有想法的人会很主动,所以要你的公司不会吊着你所以懂了吧
点赞 评论 收藏
分享
评论
1
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务