#include
using namespace std;
int month[13]={0,31,28,31,30,31,30,31,31,30,31,30,31};
int week=4;
bool isdate(int date){
int yyyy=date/10000;
int mm=date/100%100;
int dd=date%100;
if(mm<1||mm>12)return false;
if(yyyy%400==0||(yyyy%100!=0&&yyyy%4==0))month[2]=29;
else month[2]=28;
if(dd<=0||dd>month[mm])return false;
return true;
}
bool isfirst(int date){
int dd=date%100;
if(dd==1)return true;
return false;
}
bool ismon(){
week=(week+1)%7;
//printf("week=%d\n",week);
if(week==0)return true;
return false;
}
int main()
{
long long ans=0;
for(int i=20000101;i<=20201001;i++){
if(isdate(i)){
ans++;
if(ismon()||isfirst(i)){
ans++;
//printf("%d\n",i);
}
}
}
printf("%lld",ans);
return 0;
}
2.直线
题目地址
#include
#include
#include
using namespace std;
typedef pair<double,double>PDD;
#define x first
#define y second
vector<PDD>point;
set<PDD>line;
int main(){
int n=20,m=21;
int sum=n+m;
for(int i=0;i<n;i++){
for(int j=0;j<m;j++){
point.push_back({(double)i,(double)j});
for(int k=0;k<i*m+j;k++){
PDD t=point[k];
if(t.x==i||t.y==j)continue;
double a=(t.y-j)*1.0/(t.x-i);
// b=(x1y2-x2y1)/(x2-x1) 截距计算,最小二乘法
double b=(t.y*i-t.x*j)*1.0/(t.x-i);
//printf("a==%lf b==%lf\n",a,b);
int len=line.size();
line.insert({a,b});
if(len!=line.size())sum++;
}
}
}
printf("%d\n",sum);
return 0;
}
3.循环小数
题目链接
#include
#define ll long long
using namespace std;
ll gcd(ll a,ll b)
{
return b?gcd(b,a%b):a;
}
int main()
{
ll p,q,num;
cin>>p>>q>>num;
ll X,Y;//x分子,y分母
ll t=pow(10,q-p+1); //1e5
ll loop=num%t;//循环数值 42857
ll l=num/t;//未循环 1
X=loop+(t-1)*l; //42857+(1e5-1)*1
Y=pow(10,p-1)*(t-1); //10*(1e5-1)
ll g=gcd(X,Y);
X/=g;
Y/=g;
cout<<X<<" "<<Y<<endl;
return 0;
}
4.卡牌换位
#include
#include
#include
using namespace std;
string st,rst;
int a,b;
int dx[4]= {1,0,-1,0},dy[4]= {0,1,0,-1};
struct node{
string str;//当前字符
int p;//空字符位置
int step;//已经走的步数
int pre;//空字符上次位置
node(){
}
node(string str,int p,int step,int pre){
this->str=str;
this->p=p;
this->step=step;
this->pre=pre;
}
};
int bfs(string st,int p,int step)
{
queue<node>q;
q.push(node(st,p,0,-1));
while(!q.empty())
{
node t=q.front();
string nst=t.str;
int step=t.step,p=t.p,pre=t.pre;
q.pop();
if(nst[a]=='B'&&nst[b]=='A')return step;
int x=p/3,y=p%3;
for(int i=0; i<4; i++)
{
int sx=x+dx[i],sy=y+dy[i];
if(sx*3+sy!=pre&&sx>=0&&sx<2&&sy>=0&&sy<3)
{
swap(nst[sx*3+sy],nst[p]);
q.push({nst,sx*3+sy,step+1,p});
swap(nst[sx*3+sy],nst[p]);
}
}
}
return -1;
}
int main()
{
getline(cin,st);
getline(cin,rst);
st+=rst;
int p;
for(int i=0; i<6; i++)
{
if(st[i]=='A')
a=i;
if(st[i]=='B')
b=i;
if(st[i]==' ')
p=i;
}
//字符串,为空的位置,交换次数,上一个交换的位置
cout<<bfs(st,p,0);
return 0;
}
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)