1.判断回文序列
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define N 100
int main()
{
char inchar[N];
int num[N];
int i=0;
int j=0;
int len=0;
scanf("%s",inchar);
len = strlen(inchar);
printf("len:%d",len);
for(i=0;i<len;i++)
{
num[i]=inchar[i]-'0';
}
for(j=0,i=i-1;i>=0;j++,i--)
{
if(num[i]!=num[j])
break;
}
if(j==len)
{
printf("yes\n");
return 1;
}
else
{
printf("no\n");
return 0;
}
}
这题本身不难,注意点在于使用整形数组存放回文序列,以及如何输入任意长度的回文序列(当然是字符串形式)。
2.给定日期判断是一个星期的星期几。
#include <stdio.h>
//给定一个日期,返回一个星期中的星期几。
#define NDAY_OF_YEAR 36
int isYunYear(int year)
{
return ((year%4==0 && year%100!=0) || (year%400 == 0));
}
int main()
{
int dayofweek=0;
int year,month,day;
int nr_day[]={0,31,28,31,30,31,30,31,31,30,31,30,31};
int pre_year,pos_year;
long nday=0;
scanf("%d%d%d",&year,&month,&day);
if(year<1900 || year > 2200)
{
printf("year parameter is wrong\n");
return 1;
}
if(month<1 || month >12)
{
printf("month parameter is wrong\n");
return 1;
}
if(day<1 || (day > (nr_day[month]+((month == 2)&&isYunYear(year)))))
{
printf("day parameter is wrong\n");
return 1;
}
printf("%d %d %d\n",year,month,day);
if(year >1980)
{
pre_year=1980;
pos_year=year;
}
else
{
pre_year=year;
pos_year=1980;
}
while(pos_year-pre_year>0)
{
nday += NDAY_OF_YEAR +isYunYear(pre_year);
pre_year++;
}
while(month>1)
{
nday += nr_day[month-1] + (((month-1)==2) && isYunYear(year));
month--;
}
nday += day-1;
dayofweek = (nday%7 + 2)%7;
switch(dayofweek)
{
case 0:
printf("Sun\n");
break;
case 1:
printf("Mon\n");
break;
case 2:
printf("Tue\n");
break;
case 3:
printf("Wed\n");
break;
case 4:
printf("Thr\n");
break;
case 5:
printf("Fri\n");
break;
case 6:
printf("Sat\n");
break;
}
return 0;
}
网上有种快速计算的方法:
http://blog.csdn.net/iefswang/article/details/10019991
3.英语字母的电话号码转换成数字形式。
每个数字字母都是以大写字母进行区分的,如OneTwoZero。0有两种表示Zero或0h,另外首字母不是0。
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define N 100
int main()
{
char note_num[N];
char bak_num[N][N];
int i=0,j=0,k=0;
char* num_table[12]={"Zero","One","Two","Three","Four","Five","Six","Seven","Eight","Nine","0h","Double",};
char ret_num[N];
int n=0;
int flag_double=0;
//scanf("%s",note_num); scanf以空格为默认结束标志
/*
do
{
note_num[i]=getchar();
i++;
}
while(note_num[i-1]!='\n');
note_num[i] ='\0';
*/
gets(note_num);//gets可以包含空格,字符串结尾自动加'\0'。也可以用getchar,但要进行判断,以'\n'为结束标志,注意:最后加上'\0'
printf("%s",note_num);
int len = strlen(note_num);
for(i=0;i<len;i++)
{
if(i == 0)
{
if((note_num[0]<'A') || (note_num[0]>'Z'))
{
printf("first letter should be alphabet\n");
exit(1);
}
}
if(((note_num[i]>'A') && (note_num[i]<'Z'))||(note_num[i]=='0' && note_num[i+1]== 'h'))
{
k=0;
do
{
bak_num[j][k]=note_num[i];
k++;
i++;
}
while((note_num[i]>'a') && (note_num[i]<'z'));
bak_num[j][k]='\0';
j++;
i--;
}
else
{
printf("wrong letter\n");
exit(1);
}
}
i=j;
for(j=0;j<i;j++,n++)
{
//printf("%s\n",bak_num[j]);
for(k=0;k<12;k++)
{
if(!strcmp("Double",num_table[k]))
{
flag_double=1;
break;
}
if(!strcmp(bak_num[j],num_table[k]))
{
ret_num[n]=k%10+'0';
if(1==flag_double)
{
ret_num[n-1] = ret_num[n];
flag_double=0;
}
break;
}
}
}
ret_num[n] = '\0';
printf("%s\n",ret_num);
return 0;
}
以上代码是自己写的可能有些不足,欢迎纠正。