D. Even String

#include<bits/stdc++.h>
#define int long long
using namespace std;
const int N=2e5+5,mod=998244353;
const int inf=0x3f3f3f3f3f3f3f3f;
int Pow(int a,int b){
	int ans=1;
	while(b){
		if(b&1)ans=ans*a%mod;
		b>>=1,a=a*a%mod;
	}
	return ans;
}
void solve(){
	vector<int> arr(26);
	int sum=0,sum1,sum2;
	for(int& c:arr)cin>>c,sum+=c;
	sum1=sum/2;
	sum2=(sum+1)/2;
	vector<int> dp(sum+1);
	dp[0]=1;
	//dp求解每种字符选和不选使得总和为sum有多少种情况
	for(int i=0;i<26;i++){
		if(arr[i]){
			for(int j=sum;j>=arr[i];j--){
				dp[j]=dp[j]+dp[j-arr[i]];
			}
		}
	}
	//初始化阶乘和阶乘的逆元
	vector<int> fac(sum+1),inv(sum+1);
	fac[0]=inv[0]=fac[1]=1;
	for(int i=2;i<=sum;i++)fac[i]=fac[i-1]*i%mod;
	inv[sum]=Pow(fac[sum],mod-2);
	for(int i=sum-1;i>=1;i--)inv[i]=inv[i+1]*(i+1)%mod;
	int ans=dp[sum1]*fac[sum1]%mod*fac[sum2]%mod;
	for(int i=0;i<26;i++)ans=ans*inv[arr[i]]%mod;
	cout<<ans<<"\n";
}
signed main(){
	ios::sync_with_stdio(false);
	cin.tie(0);
	int t=1;
	cin>>t;
	while(t--)solve();
	cout.flush(),system("pause");
	return 0;
}

`java.lang.NumberFormatException` 是一种常见的运行时异常,通常发生在尝试将字符串转换为数值类型时失败的情况下。对于输入字符串 `180.20` 导致的 `NumberFormatException` 异常,以下是详细的分析和解决方案: ### 问题原因 当尝试通过 `Integer.parseInt()` 或其他类似的整数解析方法处理带有小数点的字符串(如 `"180.20"`)时,会抛出 `java.lang.NumberFormatException` 异常。这是因为这些方法期望接收的是纯整数形式的字符串,而带小数点的字符串无法被识别为有效的整数[^1]。 ### 解决方案 #### 方法一:使用浮点型数据类型 如果需要支持小数部分,则应改用能够表示浮点数的方法,例如 `Double.parseDouble()` 或 `Float.parseFloat()` 来替代 `Integer.parseInt()`。这样可以成功解析包含小数点的字符串。 ```java public class NumberFormatExample { public static void main(String[] args) { String numberString = "180.20"; try { double result = Double.parseDouble(numberString); System.out.println("Parsed as double: " + result); } catch (NumberFormatException e) { System.err.println("Error parsing the string to a number."); } } } ``` #### 方法二:验证并清理输入 在某些场景下,可能希望先判断输入是否合法后再执行转换操作。可以通过正则表达式或其他方式预处理输入字符串,确保其符合预期的数据格式。 ```java import java.util.regex.Pattern; public class ValidateInputExample { private static final Pattern INTEGER_PATTERN = Pattern.compile("^-?\\d+$"); public static boolean isValidInteger(String strNum) { if (strNum == null || !INTEGER_PATTERN.matcher(strNum).matches()) { return false; } return true; } public static void main(String[] args) { String testStr = "180.20"; if (!isValidInteger(testStr)) { System.out.println("The provided string is not an integer!"); // Attempt conversion using floating-point types instead. try { float floatValue = Float.parseFloat(testStr); System.out.println("Converted successfully to float: " + floatValue); } catch (NumberFormatException ex) { System.err.println("Failed converting even with float type."); } } else { int intValue = Integer.parseInt(testStr); System.out.println("Successfully parsed as integer: " + intValue); } } } ``` ### 注意事项 尽管提供了多种解决途径,但在实际开发过程中仍需注意以下几点: - **明确需求**:确认业务逻辑确实允许存在非整数值的情况。 - **边界测试**:充分考虑各种极端情况下的输入值,包括但不限于负号、科学计数法等形式的表现。 - **国际化考量**:不同地区可能存在不同的数字书写习惯(如千分位符号),这可能会进一步影响到程序的行为表现[^4]。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值