usaco preface

本文介绍了一个C++程序,该程序用于统计从1到指定数字的所有整数中罗马数字I、V、X、L、C、D和M出现的次数。通过对每个数位的数值进行判断并累加相应的罗马数字符号,最终输出每种符号的使用频率。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

题目很简单,这种题做起来也不会错,不过做法很搓,代码很长。

/*
ID: stormdp1
LANG: C++
TASK: preface
*/
#include <iostream>
#include <cstdio>
#include <cmath>

using namespace std;

int i, v, x;
int l, c, d;
int m;

void Cal(int n)
{
    if(n / 1000 > 0) {
        m += n / 1000;
        n %= 1000;
    }
    if(n / 100 > 0) {
        if(n / 100 <= 3) {
            c += n / 100;
        }
        else if(n / 100 <= 8) {
            d += 1;
            c += abs(n / 100 - 5);
        }
        else {
            m++;
            c++;
        }
        n %= 100;
    }
    if(n / 10 > 0) {
        if(n / 10 <= 3) {
            x += n / 10;
        }
        else if(n / 10 <= 8) {
            l += 1;
            x += abs(n / 10 - 5);
        }
        else {
            c++;
            x++;
        }
        n %= 10;
    }
    if(n > 0) {
        if(n <= 3) {
            i += n;
        }
        else if(n <= 8) {
            v += 1;
            i += abs(n - 5);
        }
        else {
            x++;
            i += 1;
        }
    }
}

int main()
{
    int num;
    
    freopen("preface.in", "rb", stdin);
    freopen("preface.out", "wb", stdout);
    while(scanf("%d", &num) != EOF) {
        i = v = x = 0;
        l = c = d = 0;
        m = 0;
        
        for(int j = 1; j <= num; j++)
            Cal(j);
        
        if(i)
            printf("I %d\n", i);
        if(v)
            printf("V %d\n", v);
        if(x)
            printf("X %d\n", x);
        if(l)
            printf("L %d\n", l);
        if(c)
            printf("C %d\n", c);
        if(d)
            printf("D %d\n", d);
        if(m)
            printf("M %d\n", m);        
    }
    
    fclose(stdin);
    fclose(stdout);
    return 0;
}       

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值