特殊时间(蓝桥杯)

特殊时间

问题描述

本题为填空题,只需要算出结果后,在代码中使用输出语句将所填结果输出即可。
2022年2月22日22:20 是一个很有意义的时间, 年份为 2022 , 由 3 个 2 和 1 个 0 组成, 如果将月和日写成 4 位, 为 0222 , 也是由 3 个 2 和 1 个 0 组 成, 如果将时间中的时和分写成 4 位, 还是由 3 个 2 和 1 个 0 组成。
小蓝对这样的时间很感兴趣, 他还找到了其它类似的例子, 比如 111 年 10 月 11 日 01:11,2202年2月22日22:02等等。
请问, 总共有多少个时间是这种年份写成 4 位、月日写成 4 位、时间写成 4 位后由 3 个一种数字和 1 个另一种数字组成。注意 1111 年 11 月 11 日 11:11 不算,因为它里面没有两种数字。

答案:212

c++

这题一定要注意判断日期合法性那个地方

#include<iostream>
using namespace std;

int main()
{
	int res=0;
	for(int u=0;u<=9;u++)//出现 1 次的数 
	{
		for(int v=0;v<=9;v++)//出现 3 次的数 
		{
			if(u==v)//这两个数不能相等 
			{
				continue;
			}
			int a=0,b=0,c=0;//用来储存合法的年份、月份日期、时间 有几个,相乘就是答案 
			for(int pos=0;pos<4;pos++)//出现一次的数出现的位置在哪 
			{
				int nums[4];//将 u,v 这两个数存入数组中 
				for(int i=0;i<4;i++)//遍历数组 
				{
					if(i==pos)//u是出现一次的数,存一次 
					{
						nums[i]=u;
					}
					else
					{
						nums[i]=v;
					}
				}
				int y=nums[0]*1000+nums[1]*100+nums[2]*10+nums[3];//2020
				a++;//年份都是合法的 
				int m=y/100,d=y%100;
				if(m>=1&&m<=12&&d>=1&&d<=22)//判断日期合法性 
				//tips: 31 是无效的,因为 若是存在31,则只有一种情况是 1131,11月是没有31天的
				//      30 是无效的,因为 不存在 0030 3330
				//      以此类推
				//      22  是有效的最大值, 1222 0222 
				{
					b++;
				}
				if(m>=0&&m<=23&&d>=0&&d<=59)//判断时间合法性 
				{
					c++;
				}
			}
			res+=a*b*c;
		}
	}
	cout<<res<<endl;
	return 0;
}

c语言(暴力破解版)

20分钟以上的运行时间

#include<stdio.h>
#include<string.h>

int tongji(char str[])
{
  int a[10]={0};
  int count=0;
  for(int i=0;i<12;i++)
  {
    a[str[i]-'0']++;
  }
  for(int i=0;i<10;i++)
  {
    if(a[i]!=0)
      count++;
  }
  return count;
}

int tiaojian(char a[],char b[],char c[])
{
  int count1=0,count2=0,count3=0;
  int mcount1=0,mcount2=0,mcount3=0;
  int ma=0,mb=0,mc=0;//确保三个数组中的三个元素是一样的,否则会出现2022 0002 0002非法数据 
  for(int i=0;i<4;i++)
  {
    for(int j=0;j<4;j++)
    {
      if(a[i]==a[j])
        count1++;
      if(b[i]==b[j])
        count2++;
      if(c[i]==c[j])
        count3++;
    }
    if(mcount1<count1)
    {
    	mcount1=count1;
    	ma=a[i];
	}
    if(mcount2<count2)
    {
    	mcount2=count2;
    	mb=b[i];
	}
    if(mcount3<count3)
    {
    	mcount3=count3;
    	mc=c[i];
	}
    count1=0,count2=0,count3=0;
  }
  if((mcount1==3&&mcount2==3&&mcount3==3)&&(ma==mb&&mb==mc))
    return 1;
  return 0;
}

int get_string(int year,int day,int time)
{
  char years[5],days[5],times[5];
  char str[13];
  int i=0;
  for(int k=3;k>=0;k--)//倒着存,则在数组里是正的数据 
  {
    years[k]=year%10+'0';
    year/=10;
    days[k]=day%10+'0';
    day/=10;
    times[k]=time%10+'0';
    time/=10;
  }
  strcpy(str,years);
  strcat(str,days);
  strcat(str,times);
//  puts(str);
  if(tongji(str)==2)//判断数组里面的元素种类,如 202220222022 的元素种类为 2 ,符合题意 
  {
    if(tiaojian(years,days,times)==1)//判断数组里面的相同元素是否有3个,如果相同元素有三个,则说明满足题目条件2“ 3 个一种数字和 1 个另一种数字组成” 
    {
    	puts(str);
    	return 1;
	}
  }
  return 0;
}

int main(int argc, char *argv[])
{
  int months[13]={0,31,28,31,30,31,30,31,31,30,31,30,31};
  int i,j,l,k,m;
  int count=0;
  for(i=1;i<=9999;i++)
  {
    if((i%4==0&&i%100!=0)||(i%400==0))//判断日期合法性 
      months[2]=29;
    for(j=1;j<=12;j++)
    {
      for(l=1;l<=months[j];l++)
      {
      	for(k=0;k<24;k++)
      	{
      		for(m=0;m<60;m++)
      		{
      			if(get_string(i,j*100+l,k*100+m)==1)
          			count++;
			}
		}
      }
    }
  }
  printf("%d",count);
  return 0;
}
  • 6
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

命运从未公平

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值