USACO 2.2.1Preface Numbering

本文介绍了一个C++程序,该程序接收一个整数N,并将其转换为罗马数字表示形式,随后统计并输出每种罗马数字字符的出现次数。

题意:

给定整数N, 采用罗马数字的方式表达,统计每个罗马字符的个数,从小到大输出

解法:

枚举,统计每一个数字的字符数,然后输出

/*
ID: lsswxr1
PROG: preface
LANG: C++
*/
#include <iostream>
#include <vector>
#include <map>
#include <list>
#include <set>
#include <deque>
#include <stack>
#include <queue>
#include <algorithm>
#include <cmath>
#include <cctype>
#include <cstdio>
#include <iomanip>
#include <cmath>
#include <cstdio>
#include <string>
#include <cstring>
#include <fstream>
using namespace std;

#define USACO
#ifdef USACO
#define cin fin
#define cout fout
#endif
//////////////////////////////////////////////////////////////////////////
///宏定义
const int  INF = 1000000000;
const int MAXN = 40001;
const int maxn = MAXN;
///全局变量 和 函数
int N;
int counter[5000];
inline void cnt(int num, int tot)
{
    if (tot != 0)
    {
        if (tot == 9)
        {
            counter[num * 10] += 1;
            counter[num] += 1;
        }
        else if (tot >= 5)
        {
            int v = tot / 5;
            int c = tot % 5;
            counter[num * 5] += v;
            counter[num] += c;
        }
        else if (tot == 4)
        {
            counter[num] += 1;
            counter[num * 5] += 1;
        }
        else
        {
            counter[num] += tot;
        }
    }
}
int ans[] = {1, 5, 10, 50, 100, 500, 1000}; //一共7个
char ansChar[] = {'I', 'V', 'X', 'L', 'C', 'D', 'M'};
int main()
{

#ifdef USACO    
    ofstream fout ("preface.out");
    ifstream fin ("preface.in");
#endif   
    while (cin >> N)
    {
        memset(counter, 0, sizeof(counter));
        for (int i = 1; i <= N; i++)
        {
            int temp = i;
            int thousond = temp / 1000;
            if (thousond != 0)
            {
                counter[1000] += thousond;
            }
            temp -= thousond * 1000;
            int hundred = temp / 100;
            temp -= 100 * hundred;
            cnt(100, hundred);

            int ten = temp / 10;
            temp -= 10 * ten;
            cnt(10, ten);

            cnt(1, temp);            
        }
        for (int i = 0; i < 7; i++)
        {
            if (counter[ans[i]] == 0)
            {
                continue;
            }
            cout << ansChar[i] << " ";
            cout << counter[ans[i]];
            cout << endl;
        }
    }
    
    
    ///结束
    return 0;
}

 

转载于:https://www.cnblogs.com/rayforsure/p/3526027.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值