《算法竞赛·快冲300题》每日一题:“英文数字计数”

《算法竞赛·快冲300题》即将在2024年出版,提供C++、Java、Python三种语言的代码解题方案,专注于中低难度题目,适合学习者进行算法训练。文章给出了一个关于将数字转化为英文单词并计算字母数量的题目,包括各语言的代码实现。
摘要由CSDN通过智能技术生成

算法竞赛·快冲300题》将于2024年出版,是《算法竞赛》的辅助练习册。
所有题目放在自建的OJ New Online Judge
用C/C++、Java、Python三种语言给出代码,以中低档题为主,适合入门、进阶。


英文数字计数” ,链接: http://oj.ecustacm.cn/problem.php?id=1792

题目描述

【题目描述】 数字1-n转换成英文单词,统计字母出现次数。
例如1-5,把写成英文单词分别是:one、two、three、four、five,累计19个字母。
数字342:three hundred and forty-two,包含23个字母
数字115:one hundred and fifteen,包含20个字母
单词“and”的使用方法遵循英语的规范,最终只需统计字母,不要统计空格和连字符。 。
【输入格式】 一个正整数n,n≤1000。
【输出格式】 输出一个数字表示答案。
【输入样例】

5

【输出样例】

19

题解

   这是一道难度不高的模拟题,需要细致地考虑各种情况,见代码中的注释。

【笔记】 提高编码能力 。

C++代码

#include<bits/stdc++.h>
using namespace std;
//one two three four five six seven eight nine ten
int a1[] = {0, 3, 3, 5, 4, 4, 3, 5, 5, 4, 3};
//eleven twelve thirteen fourteen fifteen sixteen seventeen eighteen nineteen twenty
int a2[] = {0, 6, 6, 8, 8, 7, 7, 9, 8, 8, 6};
//ten twenty thirty forty fifty sixty seventy eighty ninety hundred
int a3[] = {0, 3, 6, 6, 5, 5, 5, 7, 6, 6, 7};
int Count_100(int x){              //x小于100
    if(x <= 10)   return a1[x];
    if(x <= 20)   return a2[x - 10];
    return a3[x / 10] + a1[x % 10];
}
int Count(int x){
    if(x < 100)      return Count_100(x);
    if(x == 100)     return 10;    //one hundred
    if(x == 1000)    return 11;    //one thousand
    if(x % 100 == 0) return a1[x / 100] + 7;
    return a1[x / 100] + 7 + 3 + Count_100(x % 100);
}
int main(){
    int n, ans = 0;
    cin >> n;
    for(int i=1; i<=n; i++)   ans += Count(i);
    cout<<ans<<endl;
    return 0;
}

Java代码

import java.util.*;
public class Main {
    //one two three four five six seven eight nine ten
    static int[] a1 = {0, 3, 3, 5, 4, 4, 3, 5, 5, 4, 3};
    //eleven twelve thirteen fourteen fifteen sixteen seventeen eighteen nineteen twenty
    static int[] a2 = {0, 6, 6, 8, 8, 7, 7, 9, 8, 8, 6};
    //ten twenty thirty forty fifty sixty seventy eighty ninety hundred
    static int[] a3 = {0, 3, 6, 6, 5, 5, 5, 7, 6, 6, 7};
    static int Count_100(int x) { //x小于100
        if (x <= 10) return a1[x];
        if (x <= 20) return a2[x - 10];
        return a3[x / 10] + a1[x % 10];
    }
    static int Count(int x) {
        if (x < 100)   return Count_100(x);
        if (x == 100)  return 10; //one hundred
        if (x == 1000) return 11; //one thousand
        if (x % 100 == 0) return a1[x / 100] + 7;
        return a1[x / 100] + 7 + 3 + Count_100(x % 100);
    }
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int n = scanner.nextInt();
        int ans = 0;
        for (int i = 1; i <= n; i++) ans += Count(i);
        System.out.println(ans);
    }
}

Python代码

# one two three four five six seven eight nine ten
a1 = [0, 3, 3, 5, 4, 4, 3, 5, 5, 4, 3]
# eleven twelve thirteen fourteen fifteen sixteen seventeen eighteen nineteen twenty
a2 = [0, 6, 6, 8, 8, 7, 7, 9, 8, 8, 6]
# ten twenty thirty forty fifty sixty seventy eighty ninety hundred
a3 = [0, 3, 6, 6, 5, 5, 5, 7, 6, 6, 7]
def count_100(x):                       # x小于100
    if x <= 10:     return a1[x]
    if x <= 20:     return a2[x - 10]
    return a3[x // 10] + a1[x % 10]
def count(x):
    if x < 100:    return count_100(x)
    if x == 100:   return 10            # one hundred
    if x == 1000:  return 11            # one thousand
    if x % 100 == 0:  return a1[x // 100] + 7
    return a1[x // 100] + 7 + 3 + count_100(x % 100)
n = int(input())
ans = 0
for i in range(1, n + 1):   ans += count(i)
print(ans)
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

罗勇军

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值