USACO Preface

前面好幾天,俺要發文章CSDN都跟我作對,點擊發表一點反應都沒有,俺怒了,實在不想繼續搞下去,尤其,那時候傻逼的俺也沒有發現CSDN直接粘貼Word文件的地兒,鬱悶了。下面幾個USACO程序都直接貼代碼了。



/*
ID: fairyroad
TASK: preface
LANG: C++
*/
#include<fstream>
using namespace std;
ifstream fin("preface.in");
ofstream fout("preface.out");

const char Symbol[7] = {'I', 'V', 'X', 'L', 'C', 'D', 'M'};
const int cnt[10][3] = {0,0,0, 1,0,0, 3,0,0, 6,0,0, 7,1,0, 7,2,0, 8,3,0, 10,4,0, 13,5,0, 14,5,1};
const int cnt2[10][3] = {0,0,0, 1,0,0, 2,0,0, 3,0,0, 1,1,0, 0,1,0, 1,1,0, 2,1,0 , 3,1,0 , 1,0,1 };

int n; // input
int res[9]; // store the result

int main()
{
    fin>>n;
    int pos = 0, k = 1;
    for(; k <= n; k *= 10, pos += 2)
    {
        for(int i = 0; i < 3; ++i)
        {
            res[i+pos] += n/(k*10)*cnt[9][i]*k;
  			if (n/k%10) res[i+pos] +=  cnt[n/k%10-1][i]*k;
			res[i+pos] += (n%k+1)*cnt2[n/k%10][i]; // 處理最高位
        }
    }

    for(int i = 0; i < 9; ++i)
        if(res[i]) fout<<Symbol[i]<<' '<<res[i]<<"\n";

    return 0;
}





發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章