①
将输入的十进制正整数转换为罗马数据。
假设罗马数据中只使用如下七个基值字母:M、D、C、L、X、V、I,分别用来表示1000、500、100、50、10、5、1。例如十进制数678对应的罗马数据为:DCLXXVIII,而十进制数1978对应的罗马数据为MDCCCCLXXVI。
程序如下:
1

#include<iostream>
2

using namespace std;
3

int main()
4


{
5

int value;
6

cout<<"Enter value:";
7

cin>>value;
8

int test_val=value;
9


int count[]={0,0,0,0,0,0,0};
10


char rom[]={'M','D','C','L','X','V','I'};
11


int div[]={1000,500,100,50,10,5,1};
12

for(int i=0;i<7;++i)
13


{
14

count[i]=test_val/div[i];
15

test_val=test_val%div[i];
16

}
17

cout<<value<<"(10)=";
18

for(int i=0;i<7;++i)
19

for(int j=0;j<count[i];++j)
20

cout<<rom[i];
21

cout<<"(Rom)"<<endl;
22

return 0;
23

}
24

②
计算某一天是星期几
由用户输入一个年份(如2002)以及该年元旦是星期几,而后再输入改年的任意一个月份日期(如输入12和31,表示12月31日),由程序
计算出这一天是星期几。
例如,程序执行后,可在屏幕上显示形式的结果:
input YEAR and what WEEKDAY of 1.1(0~6) in that year:2002 2
input MONTH(1~12) and DAY(1~28|29|30|31) of that year:12 31
2002.12.31 => weekday:2
程序如下:
1

#include<iostream>
2

using namespace std;
3

int main()
4


{
5

int year,pre_day;
6


int days[]={0,31,28,31,30,31,30,31,31,30,31,30};
7

cout<<"Enter YEAR and what WEEKDAY of 1.1(0~6)in that year:";
8

cin>>year>>pre_day;
9

int month,day;
10

cout<<"Enter MONTH(1~12)and DAY(1~28|29|30|31) of that year:";
11

cin>>month>>day;
12

int wek;
13

int allday=0;
14

for(int i=0;i<month;++i)
15

allday+=days[i];
16

if(year%400==0 ||(year%4==0 && year%100!=0)) //判断是否为闰年
17


{
18

if(month>2)allday+=day+1;
19

}
20

else
21

allday+=day;
22

int la=allday%7;
23

wek=pre_day+la-1;
24

cout<<year<<"."<<month<<"."<<day<<"=>"<<"weekday:"<<wek<<endl;
25

return 0;
26

}
27

③
在字符串中记录各字符出现的次数
1
node* last=p1;
2
int letter[26];
3
char cc;
4
for(int i=0;i<26;++i)
5
letter[i]=0;
6
while(last)
7
{
8
cc=last->c;
9
if(cc>='A'&&cc<='Z')
10
letter[cc-'A']++;
11
if(cc>='a'&&cc<='z')
12
letter[cc-'a']++;
13
last=last->next;
14
}