一遍过。
就是用重复计算的思想,写一个函数,这个函数就是判断一个数字用到哪些数几次,然后是通过递归判断值在哪个范围,用一个函数就可以重复计算,
是枚举的算法,时间耗费有些大。
代码:
/*
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;
}