一个整数“犯二的程度”定义为该数字中包含2的个数与其位数的比值。如果这个数是负数,则程度增加0.5倍;如果还是个偶数,则再增加1倍。例如数字-13142223336是个11位数,其中有3个2,并且是负数,也是偶数,则它的犯二程度计算为:3/11×1.5×2×100%,约为81.82%。本题就请你计算一个给定整数到底有多二。
输入第一行给出一个不超过50位的整数N。
在一行中输出N犯二的程度,保留小数点后两位。
-13142223336
81.82%
具体步骤如下(两种思路适合处理长整数!!!):
if (Integer.parseInt(numberStr) % 2 == 0) {
ratio *= 2;
}
报错原因:
Exception in thread "main" java.lang.NumberFormatException: For input string: "13142223336"
正确方式:
boolean isEven = numberStr.endsWith("0") || numberStr.endsWith("2") || numberStr.endsWith("4") || numberStr.endsWith("6") || numberStr.endsWith("8");
System.out.printf("%.2f%%\n",xxxx);
import java.math.BigInteger;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
String numberStr = scanner.nextLine();
BigInteger number = new BigInteger(numberStr);
int count2 = 0;
int length = numberStr.length();
// 如果是负数,减去负号的位数
if (number.compareTo(BigInteger.ZERO) < 0) {
length--;
}
// 计算数字中2的个数
for (char c : numberStr.toCharArray()) {
if (c == '2') {
count2++;
}
}
double ratio = (double) count2 / length;
// 负数,增加0.5倍
if (number.compareTo(BigInteger.ZERO) < 0) {
ratio *= 1.5;
}
// 偶数,增加1倍
if (number.mod(BigInteger.valueOf(2)).equals(BigInteger.ZERO)) {
ratio *= 2;
}
// 输出结果
System.out.printf("%.2f%%\n", ratio * 100);
}
}
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
String numberStr = scanner.nextLine();
boolean isNegative = numberStr.startsWith("-");
boolean isEven = numberStr.endsWith("0") || numberStr.endsWith("2") || numberStr.endsWith("4") || numberStr.endsWith("6") || numberStr.endsWith("8");
// 移除负号(如果存在)
if (isNegative) {
numberStr = numberStr.substring(1);
}
int count2 = 0;
for (int i = 0; i < numberStr.length(); i++) {
if (numberStr.charAt(i) == '2') {
count2++;
}
}
double ratio = (double) count2 / numberStr.length();
if (isNegative) {
ratio *= 1.5;
}
if (isEven) {
ratio *= 2;
}
System.out.printf("%.2f%%\n", ratio * 100);
}
}
//nextBigInteger():控制台读入一个BigInteger型数据,类似于int型的nextInt();
Scanner scan = new Scanner(System.in); // 读入
BigInteger m = scan.nextBigInteger(); // 读入一个BigInteger;
//构造方法:默认为十进制,也是我们最常用的,同时也支持自定义进制类型(已存在的);
//在构造将函数时,把radix进制的字符串转化为BigInteger
String str = "1011100111";
int radix = 2;
BigInteger num1 = new BigInteger(str,radix); //743
//我们通常不写,则是默认成10进制转换,如下:
BigInteger num2 = new BigInteger(str); //1011100111
//基本运算
BigInteger a = new BigInteger("13");
BigInteger b = new BigInteger("4");
int n = 3;
//1.加
BigInteger bigNum1 = a.add(b); //17
//2.减
BigInteger bigNum2 = a.subtract(b); //9
//3.乘
BigInteger bigNum3 = a.multiply(b); //52
//4.除
BigInteger bigNum4 = a.divide(b); //3
//5.取模(需 b > 0,否则出现异常:ArithmeticException("BigInteger: modulus not positive"))
BigInteger bigNum5 = a.mod(b); //1
//6.求余
BigInteger bigNum6 = a.remainder(b); //1
//7.平方(需 n >= 0,否则出现异常:ArithmeticException("Negative exponent"))
BigInteger bigNum7 = a.pow(n); //2197
//8.取绝对值
BigInteger bigNum8 = a.abs(); //13
//9.取相反数
BigInteger bigNum9 = a.negate(); //-13
// 比较大小
BigInteger bigNum1 = new BigInteger("52");
BigInteger bigNum2 = new BigInteger("27");
//1.compareTo():返回一个int型数据(1 大于; 0 等于; -1 小于)
int num = bigNum1.compareTo(bigNum2); //1
//2.max():直接返回大的那个数,类型为BigInteger
// 原理:return (compareTo(val) > 0 ? this : val);
BigInteger compareMax = bigNum1.max(bigNum2); //52
//3.min():直接返回小的那个数,类型为BigInteger
// 原理:return (compareTo(val) < 0 ? this : val);
BigInteger compareMin = bigNum1.min(bigNum2); //27
//常量
//0
BigInteger zero = BigInteger.ZERO;
//1
BigInteger one = BigInteger.ONE;
//10
BigInteger ten = BigInteger.TEN;