- 二转十,再十转二
import java.util.*;
class Main {
public static void main(String args[]) {
Scanner sc = new Scanner(System.in);
int T = sc.nextInt();
while(T-- > 0) {
String s = sc.next();
long n = 0;
for(int i = 0; i < s.length(); i++) {
n = n * 2 + s.charAt(i) - '0';
}
output(n + 1);
output(n + 3);
}
}
static void output(long num) {
for(int i = 32; i >= 0; i--) {
if(i == 32 && (num >> i & 1) == 0) continue;
System.out.print(num >> i & 1);
}
System.out.println("");
}
}
2.高精度加法
import java.util.*;
class Main {
public static void main(String args[]) {
Scanner sc = new Scanner(System.in);
int T = sc.nextInt();
while(T-- > 0) {
List<Integer> A = new ArrayList<>();
List<Integer> B = new ArrayList<>();
List<Integer> C = new ArrayList<>();
String s = sc.next();
for(int i = 31; i >= 0; i--) {
A.add(s.charAt(i) - '0');
}
B.add(1);
C = add(A, B);
for(int i = C.size() - 1; i >= 0; i--) System.out.print(C.get(i));
System.out.printf("\n");
B.add(1);
C = add(A, B);
for(int i = C.size() - 1; i >= 0; i--) System.out.print(C.get(i));
System.out.printf("\n");
}
}
static List<Integer> add(List<Integer> A, List<Integer> B) {
List<Integer> C = new ArrayList<>();
int t = 0;
for(int i = 0; i < A.size(); i++) {
t += A.get(i);
if(i < B.size()) t += B.get(i);
C.add(t %2);
t /= 2;
}
if(t >0) C.add(t);
return C;
}
}