zoukankan      html  css  js  c++  java
  • 取一个整数a从右端开始的4~7位

    题目:取一个整数a从右端开始的47位。


    程序分析:可以这样考虑:
      (1)先使a右移4位。
      (2)设置一个低4位全为1,其余全为0的数。可用~(~0 < <4)
      (3)将上面二者进行&运算。

     1 package com.li.FiftyAlgorthm;
     2 
     3 import java.util.Scanner;
     4 
     5 public class FS {
     6     public static void main(String[] args) {
     7         Scanner s = new Scanner(System.in);
     8         boolean is = true;
     9         System.out.print("请输入一个7位以上的正整数:");
    10         long a = s.nextLong();
    11         String ss = Long.toString(a);
    12         char[] ch = ss.toCharArray();
    13         int j = ch.length;
    14         if (j < 7) {
    15             System.out.println("输入错误!");
    16         } else {
    17             System.out.println("截取从右端开始的4~7位是:" + ch[j - 7] + ch[j - 6]
    18                     + ch[j - 5] + ch[j - 4]);
    19         }
    20     }
    21 }
  • 相关阅读:
    rust 宏
    umi 调试
    rust 编译器工作流
    rust 神奇的特质
    rust 生命周期2
    rust 函数-生命周期
    rust 九九乘法表
    rust 代码生成选项
    rust 编码模式
    恢复到版本并销毁之后的git提交记录
  • 原文地址:https://www.cnblogs.com/justdoitba/p/7143442.html
Copyright © 2011-2022 走看看