题目出自:原文题目 http://blog.youkuaiyun.com/u014651817/article/details/38871501
一.上机笔试题目(共三题,两小时)
1.输入年月日,如:2014 5 17,则输出:2014 5 18,即输出为输入的下一天。(60分)
#include<iostream>
using namespace std;
void day_next(int y,int m,int d)
{
if(d>31||m>12)
{
cout<<"月份:"<<m<<" 日期:"<<d<<"输入错误!"<<endl;
return;
}
bool rn=false;
bool dayue=false;
if((y%4==0&&y/100!=0)||(y%400==0))
{
rn=true;
}
if(m==1||m==3||m==5||m==7||m==8||m==10||m==12)
{
dayue=true;
}
if(!rn)
{
if(d==29&&m==2)
{
cout<<"非闰年!没有2月29日"<<endl;
return;
}
if((dayue&&d<=30)||(!dayue&&d<=29))
{
d++;
if(m==2&&d==29)
{
m++;
d=1;
}
}
if(dayue&&d==31)
{
d=1;
m++;
if(m==13)
{
y++;
m=1;
}
}
if(!dayue&&d==30)
{
d=1;
m++;
}
cout<<y<<" "<<m<<" "<<d<<endl;
}
else
{
if((dayue&&d<=30)||(!dayue&&d<=29))
{
d++;
if(d=30&&m==2)
{
m++;
d=1;
}
}
if(dayue&&d==31)
{
d=1;
m++;
if(m==13)
{
y++;
m=1;
}
}
if(!dayue&&d==30&&m!=2)
{
d=1;
m++;
}
cout<<y<<" "<<m<<" "<<d<<endl;
}
}
int main()
{
int year;
int month;
int day;
cout<<"请输入年 月 日:";
cin>>year>>month>>day;
day_next(year,month,day);
system("pause");
return 0;
}
里面加入了闰年的判断,以及输入错误的警告!
2.如输入:i am a happy girl
则输出:a
am
girl
happy
i
即像字典那样按首字母为单词排序。(100分)
#include<iostream>
#include<vector>
#include<string>
using namespace std;
int main()
{
vector<string> str;
cout<<"请输入字符串(#结束):";
string s;
while(cin>>s)
{
if(s=="#")
break;
str.push_back(s);
}
string q;
for(int i=0;i<str.size()-1;i++)
{
for(int j=i+1;j<str.size();j++)
{
if(str[i]>str[j])
{
q=str[i];
str[i]=str[j];
str[j]=q;
}
}
}
for(int i=0;i<str.size();i++)
{
cout<<str[i]<<endl;
}
system("pause");
return 0;
}
唯一不足就是多输入了个‘#’。
3.输入,如:abtgshxc,a?tg?
则输出:abtgs
即输入两个字符串(均小写字母),用逗号隔开,第二个字符串可能用问号代替字母,在第一个字符串里查找第二个字符串,在其他字母对的上的情况下,问号可以取与之对应的字母,并输出,第一次找到的即可输出,不需要再往后找第二个。(汗...不知道表达清楚没有...)(160分)
#include<iostream>
#include<string>
using namespace std;
void fun(char s1[],int a,char s2[],int b)
{
int i=0,j=0;
while(s2[j]!='\0')
{
while(s1[i]==s2[j])//第一次相同后,开始输出
{
cout<<s2[j];
i++;
j++;
}
i++;
if(s1[i]==s2[j+1])//处理 ? 后面的字符,只有s2的下一个字符与s1相同时,才可以确定对s2输出,以及++
{
cout<<s1[i-1];
j++;
i++;
cout<<s2[j];
j++;
}
if(i>=a)//防止s2字符数大于s1字符数导致程序崩溃
break;
}
cout<<endl;
}
int main()
{
char s1[20],s2[20];
int p=0,q=0,i=0;
string ch;
cin>>ch;
int m=ch.size();
while(ch[i]!=',')
s1[p++]=ch[i++];
i++;
while(i<m)
s2[q++]=ch[i++];
s1[p]='\0';
s2[q]='\0';
int a=strlen(s1);
int b=strlen(s2);
fun(s1,a,s2,b);
system("pause");
return 0;
}
上图是s1字符串大于s2字符串的,输出了匹配结果。
上图是s1字符串小于s2的,第一次找到的即可输出,不需要再往后找第二个,故输出为第一个。