4.日期处理
简单的模拟题....
#include<iostream>
using namespace std;
typedef struct
{
int year;
int month;
int day;
} date;
int month[13] = {0,31,28,31,30,31,30,31,31,30,31,30,31};
bool isRunyear(int year)
{
if(((year%4==0) && (year%100!=0))
|| (year % 400) == 0)
return true;
return false;
}
int yearDay(date d)
{
int totalDay = 0;
if(d.month==1)
{
totalDay = d.day;
return totalDay;
}
for(int i=1;i<d.month;i++)
{
totalDay += month[i];
}
totalDay += d.day;
if(isRunyear(d.year))
totalDay++;
return totalDay;
}
int betweenDays(date d1,date d2)
{
if(d1.year == d2.year)
return yearDay(d2)-yearDay(d1);
else
{
int totalDays = 0;
for(int i=d1.year;i<d2.year;i++)
{
totalDays += isRunyear(i)?366:365;
}
return totalDays - yearDay(d1) + yearDay(d2);
}
}
int dayOfWeek(date d)
{
date d1;
d1.year = d.year;
d1.month = d.month;
d1.day = 1;
return betweenDays(d1,d) + 1;
}
int main()
{
date d1;
date d2;
cin>>d1.year>>d1.month>>d1.day;
cin>>d2.year>>d2.month>>d2.day;
cout<<betweenDays(d1,d2)<<endl;
date d3;
cin>>d3.year>>d3.month>>d3.day;
cout<<dayOfWeek(d3)<<endl;
return 0;
}
5.查找后缀连字符
简单的字符串处理...
#include <stdio.h>
#include <stdlib.h>
#include <iostream>
#include <cstring>
char *p[] = {"et-ic", "al-is-tic", "s-tic", "p-tic", "-lyt-ic", "ot-ic", "an-tic",
"n-tic", "c-tic", "at-ic", "h-nic", "n-ic", "m-ic", "l-lic", "b-lic", "-clic", "l-ic",
"h-ic", "f-ic", "d-ic", "-bic", "a-ic", "-mac", "i-ac"};
int getlastsamechar(char* a)
{
int result = -1;
int i = 0;
int j = 0;
int alen = strlen(a);
char* aswap = new char[alen + 1];
strcpy(aswap, a);
for(i = 0; i < alen/2; i++)
{
char c = aswap[i];
aswap[i] = aswap[alen - 1 - i];
aswap[alen - 1 - i] = c;
}
printf("%s\n",aswap);
int n = sizeof(p)/sizeof(char*);
for(i = 0; i < n; i++)
{
int len = strlen(p[i]);
char* b = new char[len+1];
strcpy(b,p[i]);
for(j = 0; j < len; j++)
{
if(b[j] == '-')
{
for(int q = j; q < len; q++)
{
b[q] = b[q+1];
}
len--;
}
}
for(j = 0; j < len/2; j++)
{
char c = b[j];
b[j] = b[len - 1 - j];
b[len - 1- j] = c;
}
if(alen >= len)
{
for(j = 0; j < len; j++)
{
if(aswap[j] != b[j])
{
break;
}
}
delete b;
if(j == len)
{
result = i;
break;
}
}
}
delete aswap;
return result;
}
int main()
{
char *a = "clinic";
int result = getlastsamechar(a);
printf("%d\n",result);
if(-1 != result)
{
char* res = new char[strlen(p[result]) + 1];
strcpy(res, p[result]);
printf("%s\n", res);
}
return 0;
}
6.数码管显示
#include <iostream>
using namespace std;
char *number[] = {"1011111","0000101","1110110","1110101","0101101",
"1111001","1111011","0010101","1111111","1111101"};
char** getNumber(int bit)
{
char** result= new char*[5];
for(int i = 4; i >= 0; i--)
{
result[i] = number[bit%10];
bit/=10;
}
return result;
}
int main()
{
int number;
cin>>number;
char** num = getNumber(number);
for(int i = 0; i < 5; i++)
{
cout<<*num<<endl;
num++;
}
return 0;
}