机试考点整理(1)——字符串与日期问题

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接: https://blog.csdn.net/qq_43145926/article/details/96451940

字符串问题
//没事多看几遍
//代码vs通过后复制到这方便查看
C++ String的用法

KMP算法
动画讲解
代码:(还是有点不懂啊。。。

void Getnext(int next[],String t)
{
   int j=0,k=-1;
   next[0]=-1;
   while(j<t.length-1)
   {
      if(k == -1 || t[j] == t[k])
      {
         j++;k++;
         if(t[j]==t[k])//当两个字符相同时,就跳过
            next[j] = next[k];
         else
            next[j] = k;
      }
      else k = next[k];
   }
}


样例输出
A
BBB

#include <iostream>
#include <string>
using namespace std; 
int main()
{
	int n;
	cin>>n;
	for(int i=1;i<=n;++i)
	{
		string space=string(n-i,' ');
		string ch=string(2*i-1,'A'+i-1);
		cout<<space+ch<<endl;//string可以直接相加
	}
	return 0;
}

//输入一个打字额字母A-Z或者一个数字1-9

 A         1
ABA       121     
#include <iostream>
using namespace std;
int main()
{
	char c;
	cin>>c;
	if(c>='A'&&c<='Z')
	{
		for(int i=1;i<=c-'A'+1;i++)
		{
			for(int j=1;j<=c-'A'+1-i;j++)
			{
				cout<<" ";
			}//空格
			for(int j=1;j<=i;j++)//每行的前半部分
			{
				cout<<(char)('A'+j-1);
			}
			for(int j=i-1;j>=1;j--)//每行的后半部分
			{
				cout<<(char)('A'+j-1);
			}
			cout<<endl;
		}
	}else{
		for(int i=1;i<=c-'1'+1;i++)
		{
			for(int j=1;j<=c-'1'+1-i;j++)
			{
				cout<<" ";
			}//空格
			for(int j=1;j<=i;j++)
			{
				cout<<(char)('1'+j-1);
			}
			for(int j=i-1;j>=1;j--)
			{
				cout<<(char)('1'+j-1);
			}
			cout<<endl;
		}
	
	}
	return 0;
}

输入2 2

+-+-+
|*|*|
+-+-+
|*|*|
+-+-+
#include<iostream>
using namespace std;

int main()//就是找规律
{
	int n,m;
	cin>>n>>m;//n行m列
	for(int i=1;i<=n;i++)
	{
		for(int j=1;j<=m;j++)
		{
			cout<<"+-";
		}
		cout<<"+"<<endl;
		for(int j=1;j<=m;j++)
		{
			cout<<"|*";
		}
		cout<<"|"<<endl;
	}
	for(int j=1;j<=m;j++)
	{
		cout<<"+-";
	}
	cout<<"+"<<endl;
	return 0;
}

C++ String 字符串复制 strcpy(char *destin,char source);
字符串拼接 strcat(dest,str1);
java的string可以直接用+连接
strcmp(str1,str2)字符串比较,大于返回大于0,可以直接用char数组来实现字符串比较

A1: A
A2: ABA
A3: ABACABA
求根据规律输出An

#include <stdio.h>
#include <string.h>
char res[5000000];
int main()//就是字符串的连接
{
	int n;
	scanf("%d",&n);
	int len=0;
	for(int i=1;i<=n;++i)
	{
		strcat(res+len+1,res);//+1是中间那个新字符
		res[len]='A'+i-1;//把中间那位补上
		len=strlen(res);
	}
	printf("%s\n",res);
	getchar();
	getchar();
}

字符串查找
查找str2在str1种出现的次数

#include<cstdio>
#include<cstring>
char s1[1005],s2[1005];
int main()
{
	fgets(s1,1004,stdin);
	fgets(s2,1004,stdin);
	int len1=strlen(s1)-1,len2=strlen(s2)-1;
	int ans=0;
	for(int i=0;i+len2-1<len1;i++)
	{
		bool matched=true;
		for(int j=0;j<len2;j++){
		if(s1[i+j]!=s2[j])
		{
			matched=false;
			break;
		}
		}
		if(matched)
		{
		ans++;
		}
	}
	printf("%d\n",ans);
	getchar();
	return 0;
}

日期问题
用程序算出几月几号是星期几,蔡基姆拉尔森计算公式。星期为w,年份y,月份m,日期d

w=(d+2m+3(m+1)/5+y+y/4-y/100+y/400)%7

然后把计算出来的w加上1就是真正的星期几了。注意每年的1,2月份要当成上一年的13,14月计算,上述除法均为整除。

输入年月日,输出星期几

#include <iostream>
#include <string>
using namespace std;
int whatday(int y,int m,int d){
	//返回正确的星期。用0-6表示星期1-7
	int ans=0;
	for(int i=1;i<y;i++)
	{
		if((i%100!=0 && i%4==0)||i%400==0)
		{
			ans +=366%7;
			ans %=7;
		}else{
			ans +=365%7;
			ans %=7;
		}
	}
	for(int i=1;i<m;i++)
	{
		if(i==1||i==3||i==5||i==7||i==8||i==10||i==12)
		{
			ans+=31%7;
			ans %=7;
		}else if(i==4 || i==6 ||i==9 ||i==11)
		{
			ans+=30%7;
			ans %=7;
		}else if((y%100!=0 &&y%4==0)||y%400==0)
		{
			ans+=29%7;
			ans %=7;
		}else
		{
			ans +=28%7;
			ans %=7;
		}
	}
	ans +=(d-1)%7;//减去今天
	ans %=7;
	return ans;
}

string weekday[7]={"Monday","Tuesday","Wed","Thur","Fri"};
int main()
{
	int y,m,d;
	cin>>y>>m>>d;
	cout<<weekday[whatday(y,m,d)]<<endl;
	return 0;
}

输入在一起的年月日和在一起的天数
输出k天纪念日的日期

#include <cstdio>
int day[13]={0,31,28,31,30,31,30,31,31,30,31,30,31};
int main()
{
	int y,m,d,k;
	scanf("%d%d%d%d",&y,&m,&d,&k);
	for(int i=1;i<=k;i++)
	{
		if((y%100!=0 &&y%4==0) || y%400==0)
		{
			day[2]=29;
		}else
		{
			day[2]=28;
		}
		d++;
		if(d==day[m]+1)//模拟题细节很重要
		{
			d=1;
			m++;
		}
		if(m==13)
		{
			m=1;
			y++;
		}
	}
	printf("%04d-%02d-%02d\n",y,m,d);
	return 0;
}

猜你喜欢

转载自blog.csdn.net/qq_43145926/article/details/96451940