当前位置:网站首页>数组、字符串、日期笔记【蓝桥杯】

数组、字符串、日期笔记【蓝桥杯】

2022-08-11 09:08:00 call me by ur name

判断某年某月某日是星期几 

1.模拟法:记住某天是星期几(例如公元1月1日是星期一),然后一天天模拟计算
        
    	#include<iostream>
        using namespace std;

        int whatday(int y,int m,int d){
            int ans=0;
            //计算过去当前年之后是星期几,从1月1日星期一开始计算 
            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","Wednesday","Thursday","Friday","Saturday","Sunday"};
        int main() {
             int y,m,d;
             cin>>y>>m>>d;
             cout<<weekday[whatday(y,m,d)]<<endl; 
        }


2.菜吉姆拉尔森计算公式:设星期为w,年份为y,月份为m,日期为d
    w=(d+2*m+3*(m+1)/5+y+y/4-y/100+y/400)%7
    在把计算的w+1就是真正的星期几
    注:每年的1,2月要当成上一年的13,14月来计算
  
      int main() {
        //w=(d+2xm+3x(m+1)/5+y+y/4-y/100+y/400)%7
        int w,d,m,y;
        cout<<"年";	cin>>y;
        cout<<"月";	cin>>m;
        cout<<"日";	cin>>d; 

        if(m==1){
            m=13;
            y=y-1;
        }
        if(m==2){
            m=14;
            y=y-1;
        }
        w=(d+2*m+3*(m+1)/5+y+y/4-y/100+y/400)%7;
        cout<<"星期"<<w+1<<endl;

        return 0;
    }

打印十字图形(蓝桥杯真题)

问题描述

小明为某机构设计了一个十字型的徽标(并非红十字会啊),如下所示:
..$$$$$$$$$$$$$..
..$...........$..
$$$.$$$$$$$$$.$$$
$...$.......$...$
$.$$$.$$$$$.$$$.$
$.$...$...$...$.$
$.$.$$$.$.$$$.$.$
$.$.$...$...$.$.$
$.$.$.$$$$$.$.$.$
$.$.$...$...$.$.$
$.$.$$$.$.$$$.$.$
$.$...$...$...$.$
$.$$$.$$$$$.$$$.$
$...$.......$...$
$$$.$$$$$$$$$.$$$
..$...........$..
..$$$$$$$$$$$$$..
对方同时也需要在电脑dos窗口中以字符的形式输出该标志,并能任意控制层数。
输入格式
一个正整数 n (n<30) 表示要求打印图形的层数。

输出格式
对应包围层数的该标志。
样例输入1
1

样例输出1
..$$$$$..
..$...$..
$$$.$.$$$
$...$...$
$.$$$$$.$
$...$...$
$$$.$.$$$
..$...$..
..$$$$$..
样例输入2
3

样例输出2
..$$$$$$$$$$$$$..
..$...........$..
$$$.$$$$$$$$$.$$$
$...$.......$...$
$.$$$.$$$$$.$$$.$
$.$...$...$...$.$
$.$.$$$.$.$$$.$.$
$.$.$...$...$.$.$
$.$.$.$$$$$.$.$.$
$.$.$...$...$.$.$
$.$.$$$.$.$$$.$.$
$.$...$...$...$.$
$.$$$.$$$$$.$$$.$
$...$.......$...$
$$$.$$$$$$$$$.$$$
..$...........$..
..$$$$$$$$$$$$$..

解法

#include<iostream> 
using namespace std;

const int MAX=130;	//需设置为大于5+4*30的数
bool map[MAX][MAX]; 

void print(int n)
{
	//通过n计算出打印十字图的规格(N*N)
	int N=5+4*n; 
	//首先标记最内层的十字架
	for(int i=N/2-1;i<=N/2+3;i++)
		map[N/2+1][i]=map[i][N/2+1]=true;	//一横一竖
	//然后从最外层到里层逐个标记十字架
	for(int i=1;i<=n;i++)
	{
		//接下来标记四个边角部分
		int x=2*i+1,y=2*i+1;
		//左上边角
		map[x][y]=map[x][y-1]=map[x-1][y]=true; 
		//右上边角
		y=N-2*i;
		map[x][y]=map[x][y+1]=map[x-1][y]=true;
		//右下边角
		x=N-2*i;
		map[x][y]=map[x][y+1]=map[x+1][y]=true;
		//左下边角
		y=2*i+1;
		map[x][y]=map[x][y-1]=map[x+1][y]=true;
		
		//接下来标记十字架的非边角部位(即“四个墙壁”)
		for(int j=2*i+1;j<=N-2*i;j++)
			map[2*i-1][j]=map[j][N-2*(i-1)]=map[N-2*(i-1)][j]=map[j][2*i-1]=true; 
	} 
	
	//打印
	for(int i=1;i<=N;i++){
		for(int j=1;j<=N;j++)
			if(map[i][j]) cout<<"$";
			else cout<<".";
		cout<<endl;
	} 
}

int main()
{
	int n;
	cin>>n;
	print(n);
	return 0;
}

日期问题(蓝桥杯真题)

问题描述

小明正在整理一批历史文献。这些历史文献中出现了很多日期。小明知道这些日期都在 1960 年 1 月 1 日至 2059 年 12 月 31 日。令小明头疼的是,这些日期采用的格式非常不统一,有采用年/月/日的,有采用月/日/年的,还有采用日/月/年的。

更加麻烦的是,年份也都省略了前两位,使得文献上的一个日期,存在很多可能的日期与其对应。

比如 02/03/04,可能是 2002 年 03 月 04 日、2004 年 02 月 03 日或 2004 年 03 月 02 日。

给出一个文献上的日期,你能帮助小明判断有哪些可能的日期对其对应吗?

输入描述

输出若干个不相同的日期,每个日期一行,格式是 “yyyy-MM-dd”。多个日期按从早到晚排列。

示例

输入
02/03/04
输出
2002-03-04
2004-02-03
2004-03-02
#include <bits/stdc++.h>
using namespace std;
int arr[13]= { 0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };//记录每个月的天数

struct date {//日期的结构体
	int year, month, day;
};

date da[3] = { 0 };//结构体数组

bool isLeap(int y) {
	return ((y % 4 == 0) && (y % 100 != 0) || (y % 400 == 0));    //判断是否为闰年
}

void f(int a, int b, int c,int d)//判断日期是否合法同时存入数组中
{
	if (a >= 60)
	{
		a += 1900;
	}
	else
	{
		a += 2000;
	}
	da[d].year = a;
	if (isLeap(a)) arr[2] = 29;
	if (b > 0 && b < 13 && c>0 && c <= arr[b]) {
		da[d].month = b;
		da[d].day = c;
	}
	
}

bool bijiao(date a, date b)//sort排序函数对应的排序规则
{
	if (a.year == b.year)
	{
		if (a.month == b.month)
		{
			return a.day < b.day;
		}
		else
		{
			return a.month < b.month;
		}
	}
	else return a.year < b.year;
}

int main()
{
	string s;
	cin >> s;
	int a, b, c;
	a = stoi(s.substr(0, 2));
	b = stoi(s.substr(3, 2));
	c = stoi(s.substr(6, 2));
	f(a, b, c, 0);//此处分别对三种情况进行判断
	f(c, b, a, 1);
	f(c, a, b, 2);
	int arr1[3] = { 0 };
	sort(da,da+3, bijiao);//将合法的日期排序
	date dd = da[0];
	for (int i = 1; i <= 2; i++)//去除相同的日期
	{
		if (da[i].year == dd.year&& da[i].month == dd.month && da[i].day == dd.day)
		{
			da[i] = { 0,0,0 };
		}
		else
		{
			dd = da[i];
		}
	}
	for (int i = 0; i < 3; i++)//循环输出结果
	{
		if (da[i].day != 0)
		{
			cout << da[i].year << "-" << setw(2) << setfill('0') << da[i].month << "-" << setw(2) << setfill('0') << da[i].day << endl;
		}
	}
	return 0;
}

ASCII码

48–'0'

65–'A'

97–'a'

常用用法

int main(){	
	// char 转化为 ASCII 码 
	char c1='A';
	cout<<(char)(c1+1)<<endl;//B 
	//ASCII 转化为 char
	cout<<(int)c1<<endl;//65
	cout<<c1+1<<endl;//66(默认转化为int)  
    return 0;
} 

字母三角形

#include<bits/stdc++.h>
using namespace std;

int main(){	
	int n;
	cin>>n;
	for(int i=1;i<=n;i++){
		/* string的一种用法 
		   string str=string(字符的个数,字符); 
		   第二个变量要写字符,而不是字符串,因为多个字符组合成字符串
		*/
		string space=string(n-i,' ');
		string ch=string(2*i-1,'A'+i-1);
		cout<<space+ch<<endl;
	}
    return 0;
} 

//案例
输入:
5

输出:
    A
   BBB
  CCCCC
 DDDDDDD
EEEEEEEEE

主要是知道string的这种用法即可

升级版

/* 
输入:
F
输出:
     A
    ABA
   ABCBA
  ABCDCBA
 ABCDEDCBA
ABCDEFEDCBA

输入:
5
输出:
       1
      121
     12321
    1234321
   123454321
*/

#include<bits/stdc++.h>
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;
		}

	}
}

先正着输到最中间,然后保留那个数再倒着输

日期排序(牛客题)

题目描述

有一些日期,日期格式为“AA/BB/CCCC”。编程将日期从小到大排列。

输入描述:

输入一个整数N,代表输入日期的个数。接下来N行,输入N个格式为“AA/BB/CCCC”的日期

输出描述:

输出排序后的日期,一个日期占一行

示例1
输入
6
10/22/2003
02/12/2004
15/12/1999
12/31/2005
10/21/2003
11/30/2005

输出
15/12/1999
10/21/2003
10/22/2003
02/12/2004
11/30/2005
12/31/2005

我的题解

#include<bits/stdc++.h>
using namespace std;
class date{
    public:
      date(int year,int month,int day);
      bool operator < (const date& d);
      void print(){
          cout<<setw(2)<<setfill('0')<<day<<'/'<<setw(2)<<setfill('0')<<month<<'/'<<year<<endl;
      }
    private:
      int year;
      int month;
      int day;
};

date::date(int year,int month,int day){
    this->year=year;
    this->month=month;
    this->day=day;
}

bool date::operator < (const date& d){
    if(this->year<d.year){
        return true;
    }else if(this->year>d.year){
        return false;
    }else{
        if(this->month<d.month){
            return true;
        }else if(this->month>d.month){
            return false;
        }else{
            if(this->day<d.day){
                return true;
            }else{
                return false;
            }
        }
    }
}

int main(){
    int n,y,m,d;
    char c;
    scanf("%d",&n);
    vector<date> a;
    for(int i = 0; i<n; ++i){
        cin>>d>>c>>m>>c>>y;
        date b(y,m,d);
        a.push_back(b);
    } 
    sort(a.begin(), a.end());
    for(int i = 0 ; i < n ; ++i){
       a[i].print(); 
    }
}

这题的需要注意的地方在于输入和输出的问题

scanf("%d/%d/%d",&d,&m,&y)  像这样就可以满足题目的那种格式,scanf可以自定义格式

或者像我的题解那样单独再设个char c 来吸收'/'

输出格式——setw()只会影响它后面 一个 的输出​​​​​​​

原网站

版权声明
本文为[call me by ur name]所创,转载请带上原文链接,感谢
https://blog.csdn.net/qq_61786525/article/details/125959274