1. 程式人生 > >洛谷P1465 序言頁碼 Preface Numbering

洛谷P1465 序言頁碼 Preface Numbering

sign turn get code ber class bits queue ||

洛谷P1465 序言頁碼 Preface Numbering

 1 #include <cstdio> 
 2 #include <cmath>
 3 #define For(i,j,k) for(int i=j;i<=k;i++) 
 4 using namespace std ; 
 5 const char c[7] = { I,V,X,L,C,D,M } ; 
 6 int a[7] ; 
 7 int n ; 
 8 
 9 inline int read() 
10 {
11     int x = 0 , f = 1 ; 
12 char ch = getchar() ; 13 while(ch<0||ch>9) { if(ch==-) f = -1 ; ch = getchar(); } 14 while(ch>=0&&ch<=9) { x = x * 10+ch-48 ; ch = getchar(); } 15 return x * f ; 16 } 17 18 inline void find(int val) 19 { 20 int x ; 21 if(val>=1000) { 22 a[6
]+=val/1000 ; 23 val%=1000 ; 24 } 25 if(val>=100) { 26 x = val / 100 ; 27 if(x<=3) a[4]+=x ; 28 if(x==4) a[4]++,a[5]++ ; 29 if(x>=5&&x<=8) a[4]+=x-5,a[5]++ ; 30 if(x==9) a[4]++,a[6]++ ; 31 val%=100 ; 32 } 33 if(val>=10
) { 34 x = val / 10 ; 35 if(x<=3) a[2]+=x ; 36 if(x==4) a[2]++,a[3]++ ; 37 if(x>=5&&x<=8) a[2]+=x-5,a[3]++ ; 38 if(x==9) a[2]++,a[4]++ ; 39 val%=10 ; 40 } 41 x = val ; 42 if(x<=3) a[0]+=x ; 43 if(x==4) a[0]++,a[1]++ ; 44 if(x>=5&&x<=8) a[0]+=x-5,a[1]++ ; 45 if(x==9) a[0]++,a[2]++ ; 46 } 47 48 int main() 49 { 50 n = read() ; 51 For(i,1,n) find(i) ; 52 For(i,0,6) 53 if(a[i]) printf("%c %d\n",c[i],a[i]) ; 54 return 0 ; 55 }

 1 #include<set>
 2 #include<map>
 3 #include<list>
 4 #include<queue>
 5 #include<stack>
 6 #include<string>
 7 #include<math.h>
 8 #include<time.h>
 9 #include<vector>
10 #include<bitset>
11 #include<memory>
12 #include<utility>
13 #include<stdio.h>
14 #include<sstream>
15 #include<iostream>
16 #include<stdlib.h>
17 #include<string.h>
18 #include<algorithm>
19 #define LL unsigned long long   
20 using namespace std;
21 char c[31][5]={
22 " ","I","II","III","IV","V","VI","VII","VIII","IX","X",
23 "XX","XXX","XL","L","LX","LXX","LXXX","XC","C","CC",
24 "CCC","CD","D","DC","DCC","DCCC","CM","M","MM","MMM"};
25 int num[31]={0,1,2,3,4,5,6,7,8,9,10,20,30,40,50,60,70,
26 80,90,100,200,300,400,500,600,700,800,900,1000,2000,3000};//每個字母對應的數字
27 int n;
28 int a[26];//26個字母,大霧,其實是存那幾個字母的數量
29 void find(int nn)//nn轉成羅馬數字然後統計? 
30 {
31     int j=30;
32     char s[30];
33     while (num[j]>nn) j--;//找到第一個<=n的數 
34     for (;j>=1;j--)
35     {
36         if (nn>=num[j])//如果nn比這個數大或者等於這個數
37         {/*其實是不用擔心40被強行分成XXX和X的,因為40在表中*/
38             nn-=num[j];//減掉 
39             for (int x=0;x<strlen(c[j]);x++)//統計各個字母的個數 
40             a[int(c[j][x])-65]++; 
41         } 
42         if (nn==0) return; //節約時間。已經搜完了 
43     }
44 } 
45 int main() 
46 {
47     freopen("preface.in","r",stdin);
48     freopen("preface.out","w",stdout); 
49     scanf("%d",&n);//讀入 
50     for (int i=1;i<=n;i++) 
51     find(i);
52     //輸出,因為是按照這個順序所以我找不到什麽好辦法輸出了 
53     if (a[int(I)-65]!=0) printf("I %d\n",a[int(I)-65]);
54     if (a[int(V)-65]!=0) printf("V %d\n",a[int(V)-65]);
55     if (a[int(X)-65]!=0) printf("X %d\n",a[int(X)-65]);
56     if (a[int(L)-65]!=0) printf("L %d\n",a[int(L)-65]);
57     if (a[int(C)-65]!=0) printf("C %d\n",a[int(C)-65]);
58     if (a[int(D)-65]!=0) printf("D %d\n",a[int(D)-65]);
59     if (a[int(M)-65]!=0) printf("M %d\n",a[int(M)-65]);
60     return 0;
61 }

洛谷P1465 序言頁碼 Preface Numbering