获取s.length()后给一个j作为指针获取2的多少次方,常规for遍历,用好pow函数计算即可
import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); while (sc.hasNext()) { String s = sc.nextLine(); int sum = 0; int j = s.length(); for (int i = 0; i < s.length(); i++) { sum += (s.charAt(i) - '0') * (Math.pow(2, j) - 1); j--; } System.out.println(sum); } } }