1057 数零壹

101 篇文章 0 订阅

在这里插入图片描述

solution

给出的字符串可能含有空格,用getline()接收

#include<iostream>
#include<string>
using namespace std;
int main(){
	string s;
	int n = 0, zero = 0, one = 0, d;
	getline(cin, s);
	for(int i = 0; i < s.size(); i++){
		if(isalpha(s[i])) {
			if(s[i] <= 'Z') n += s[i] - 'A' + 1;
			else n += s[i] - 'a' + 1;
		}
	}
	while(n){
		d = n % 2;
		if(d) one++;
		else zero++;
		n /= 2;
	}
	cout << zero << " " << one;
	return 0;
} 
  • 3
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是Java实现大写金额转数字的示例代码: ```java public class AmountUtil { private static final String[] CN_UPPER_NUMBER = {"零", "壹", "贰", "叁", "肆", "伍", "陆", "柒", "捌", "玖"}; private static final String[] CN_UPPER_MONETRAY_UNIT = {"分", "角", "元", "拾", "佰", "仟", "万", "拾", "佰", "仟", "亿", "拾", "佰", "仟"}; private static final BigDecimal MAX_VALUE = new BigDecimal("9999999999999.99"); /** * 将大写金额转换为数字形式 * * @param amount 大写金额 * @return 数字形式 */ public static BigDecimal convertToNumber(String amount) { if (!amount.matches("^[壹贰叁肆伍陆柒捌玖拾佰仟万亿元角分]*$")) { throw new IllegalArgumentException("不是有效的大写金额格式"); } StringBuilder sb = new StringBuilder(amount); // 处理万、亿 int index = sb.indexOf("亿"); if (index > -1) { String temp = sb.substring(0, index); BigDecimal num = new BigDecimal(convertToNumber(temp).toString()); sb.delete(0, index + 1); sb.insert(0, " "); BigDecimal value = new BigDecimal(convertToNumber(sb.toString().trim()).toString()); return num.multiply(new BigDecimal("100000000")).add(value); } index = sb.indexOf("万"); if (index > -1) { String temp = sb.substring(0, index); BigDecimal num = new BigDecimal(convertToNumber(temp).toString()); sb.delete(0, index + 1); sb.insert(0, " "); BigDecimal value = new BigDecimal(convertToNumber(sb.toString().trim()).toString()); return num.multiply(new BigDecimal("10000")).add(value); } // 处理元、角、分 BigDecimal result = new BigDecimal("0"); int numIndex = 0; int unitIndex = 0; boolean getZero = false; while (numIndex < amount.length()) { char c = amount.charAt(numIndex); if (c >= '壹' && c <= '玖') { int num = c - '壹' + 1; if (unitIndex == 2) { result = result.add(new BigDecimal(num)); } else if (unitIndex == 6) { result = result.add(new BigDecimal(num).multiply(new BigDecimal("10000"))); } else if (unitIndex == 10) { result = result.add(new BigDecimal(num).multiply(new BigDecimal("100000000"))); } else { result = result.add(new BigDecimal(num).multiply(new BigDecimal(Math.pow(10, 3 - unitIndex)))); } getZero = false; } else if (c == '零') { getZero = true; } else if (CN_UPPER_MONETRAY_UNIT[unitIndex].equals(String.valueOf(c))) { getZero = false; } else if (c == '元') { getZero = false; } else if (c == '角') { if (getZero) { result = result.add(new BigDecimal("0.1")); } getZero = false; } else if (c == '分') { if (getZero) { result = result.add(new BigDecimal("0.01")); } getZero = false; } if (CN_UPPER_MONETRAY_UNIT[unitIndex].equals(String.valueOf(c))) { ++unitIndex; } ++numIndex; } return result; } public static void main(String[] args) { String amount = "壹亿贰仟叁佰肆拾伍万陆仟柒佰捌拾玖元零壹分"; System.out.println(convertToNumber(amount)); // 123456789.01 } } ``` 以上代码可以将大写金额转换为数字形式,支持亿、万、元、角、分等单位。注意,该方法只支持中文大写金额格式,不支持阿拉伯数字和英文格式。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值