USACO 2.2

一遍过。

就是用重复计算的思想,写一个函数,这个函数就是判断一个数字用到哪些数几次,然后是通过递归判断值在哪个范围,用一个函数就可以重复计算,

是枚举的算法,时间耗费有些大。

代码:

/*
ID: me
PROG: preface
LANG: C++
*/
#include<iostream>
#include <math.h>
#include <stdio.h>
#include <string.h>
using namespace std;
int q2[5000];
int q3[500];
int fun(int x){
    if(x>3000){
            q2[1000]+=3;
      fun(x-3000);
    }else if(x>=1000){
        q2[1000]+=x/1000;
        fun(x-(x/1000)*1000);
    }else if(x>=500){
        if(x>=900){
           q2[1000]++;
           q2[100]++;
           fun(x-900);
        }else{
            q2[500]+=x/500;
            fun(x-x/500*500);
        }
    }else if(x>=100){
        if(x>=400){
            q2[500]++;
            q2[100]++;
            fun(x-400);
        }else{
            q2[100]+=x/100;
            fun(x-(x/100)*100);
        }
    }else if(x>=50){
        if(x>=90){
            q2[100]++;
            q2[10]++;
            fun(x-90);
        }else{
            q2[50]++;
            fun(x-50);
        }
    }else if(x>=10){
        if(x>=40){
            q2[50]++;
            q2[10]++;
            fun(x-40);
        }else{
            q2[10]+=x/10;
            fun(x-(x/10*10));
        }
    }else if(x>=5){
        if(x>=9){
            q2[10]++;
            q2[1]++;
            fun(x-9);
        }else{
            q2[5]++;
            fun(x-5);
        }
    }else if(x>=1){
        if(x>=4){
            q2[5]++;
            fun(5-x);
        }else {
            q2[1]+=x/1;
        }
    }
}
int main(){
    freopen("preface.in","r",stdin);
    freopen("preface.out","w",stdout);
    memset(q2,0,sizeof(q2));
    memset(q3,0,sizeof(q3));
    int n,i,j,k,f1,f2,a,b,f3,t1,t2,t3,t4,t5;
    cin >> n;
    for(i=1;i<=n;i++)
        fun(i);
    if(q2[1])cout << "I " << q2[1] << endl;
    if(q2[5])cout << "V " << q2[5] <<endl;
    if(q2[10]) cout<< "X " << q2[10] << endl;
    if(q2[50])cout << "L " << q2[50]<< endl;
    if(q2[100])cout << "C " << q2[100] << endl;
    if(q2[500])cout << "D " << q2[500] << endl;
    if(q2[1000])cout <<"M " << q2[1000] << endl;
    return 0;

}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值