输入包括多组数据,每组数据包含一个 skew 数。
对应每一组数据,输出相应的十进制形式。结果不超过 2^31-1。
10120 200000000000000000000000000000 10 1000000000000000000000000000000 11 100 11111000001110000101101102000
44 2147483646 3 2147483647 4 7 1041110737
import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int num = 0; while (sc.hasNext()) { num = 0; String s = sc.next(); int len = s.length(); for (int i = 0; i < len; i++) { //方法一 //运行时间:32ms //占用内存:10460k char c = s.charAt(i); //String-->char String ss = String.valueOf(c); //char-->String int k = Integer.parseInt(ss); //String-->int 若Integer integer = Integer.valueOf(ss); //方法二 //运行时间:31ms //占用内存:10528k // int k = s.charAt(i) - '0'; //char-->int num += k * (Math.pow(2, len - i) - 1); } System.out.println(num); } } }
import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); while (scanner.hasNext()){ String s = scanner.nextLine(); int sum=0; for (int i = 0,len=s.length(); i < len ; i++) { sum+= (s.charAt(i)-'0')*(Math.pow(2,len-i)-1); } System.out.println(sum); } } }