usaco preface

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

/*
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;
}       

你可能感兴趣的:(usaco preface)