当前位置:网站首页>Array, string, date notes [Blue Bridge Cup]
Array, string, date notes [Blue Bridge Cup]
2022-08-11 09:24:00 【call me by ur name】
Determine the day of the week for a certain day of a certain year

1.模拟法:Remember what day of the week a day is(例如公元1月1日是星期一),Then simulate the calculation day by day
#include<iostream>
using namespace std;
int whatday(int y,int m,int d){
int ans=0;
//Calculates the day of the week past the current year,从1月1Counting starts on Monday
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;
}
}
//Calculated past the current month,是星期几
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;
}
}
//Calculate after a few days have passed
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.Dish Jim Larson Calculation Formula:Let the week bew,年份为y,月份为m,日期为d
w=(d+2*m+3*(m+1)/5+y+y/4-y/100+y/400)%7
in the calculationw+1is the real day of the week
注:每年的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;
}
Print the cross shape(蓝桥杯真题)
问题描述
小明为某机构设计了一个十字型的徽标(并非红十字会啊),如下所示:
..$$$$$$$$$$$$$..
..$...........$..
$$$.$$$$$$$$$.$$$
$...$.......$...$
$.$$$.$$$$$.$$$.$
$.$...$...$...$.$
$.$.$$$.$.$$$.$.$
$.$.$...$...$.$.$
$.$.$.$$$$$.$.$.$
$.$.$...$...$.$.$
$.$.$$$.$.$$$.$.$
$.$...$...$...$.$
$.$$$.$$$$$.$$$.$
$...$.......$...$
$$$.$$$$$$$$$.$$$
..$...........$..
..$$$$$$$$$$$$$..
对方同时也需要在电脑dos窗口中以字符的形式输出该标志,并能任意控制层数.
输入格式
一个正整数 n (n<30) 表示要求打印图形的层数.
输出格式
对应包围层数的该标志.
样例输入1
1
样例输出1
..$$$$$..
..$...$..
$$$.$.$$$
$...$...$
$.$$$$$.$
$...$...$
$$$.$.$$$
..$...$..
..$$$$$..
样例输入2
3
样例输出2
..$$$$$$$$$$$$$..
..$...........$..
$$$.$$$$$$$$$.$$$
$...$.......$...$
$.$$$.$$$$$.$$$.$
$.$...$...$...$.$
$.$.$$$.$.$$$.$.$
$.$.$...$...$.$.$
$.$.$.$$$$$.$.$.$
$.$.$...$...$.$.$
$.$.$$$.$.$$$.$.$
$.$...$...$...$.$
$.$$$.$$$$$.$$$.$
$...$.......$...$
$$$.$$$$$$$$$.$$$
..$...........$..
..$$$$$$$$$$$$$..解法
#include<iostream>
using namespace std;
const int MAX=130; //Need to be set to greater than5+4*30的数
bool map[MAX][MAX];
void print(int n)
{
//通过nCalculate the specifications for the printed cross(N*N)
int N=5+4*n;
//Mark the innermost cross first
for(int i=N/2-1;i<=N/2+3;i++)
map[N/2+1][i]=map[i][N/2+1]=true; //一横一竖
//Then mark the crosses one by one from the outermost layer to the inner layer
for(int i=1;i<=n;i++)
{
//Next mark the four corner sections
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;
//Next mark the non-corner parts of the cross(即“four walls”)
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(字符的个数,字符);
The second variable is to write characters,而不是字符串,Because multiple characters are combined into a 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
主要是知道stringcan be used in this way
升级版
/*
输入:
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;
}
}
}
Lose to the middle first,Then keep that number and lose it backwards
日期排序(牛客题)
题目描述
There are some dates,日期格式为“AA/BB/CCCC”.Program the dates in ascending order.
输入描述:
输入一个整数N,Represents the number of input dates.接下来N行,输入N个格式为“AA/BB/CCCC”的日期
输出描述:
Output sorted dates,One date per line
示例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();
}
}The point to note in this question is the input and output issues
scanf("%d/%d/%d",&d,&m,&y) A format like this can satisfy the question,scanf可以自定义格式
Or set up a separate one like my solutionchar c 来吸收'/'
输出格式——setw()will only affect the back of it 一个 的输出
边栏推荐
猜你喜欢
![[wxGlade learning] wxGlade environment configuration](/img/fd/32ceb707c4468e18038b813b6f3925.png)
[wxGlade learning] wxGlade environment configuration

MySQL性能调优,必须掌握这一个工具!!!(1分钟系列)

代码签名证书可以解决软件被杀毒软件报毒提醒吗?

【剑指offer】左旋字符串,替换空格,还有类题!!!

深度学习100例 —— 卷积神经网络(CNN)识别眼睛状态

企业展厅制作要具备的六大功能

VoLTE基础自学系列 | 3GPP规范解读之Rx接口(上集)

pycharm cancel msyql expression highlighting

音视频+AI,中关村科金助力某银行探索发展新路径 | 案例研究

零基础创作专业wordpress网站12-设置标签栏图标(favicon)
随机推荐
阿里云OSS上传文件超时 探测工具排查方法
WordpressCMS主题开发02-制作顶部header.php和footer.php
How to use QTableWidget
The no-code platform helps Zhongshan Hospital build an "intelligent management system" to realize smart medical care
Data middle platform program analysis and development direction
专题讲座8 字符串(一) 学习心得
VideoScribe卡死解决方案
深度学习100例 —— 卷积神经网络(CNN)识别眼睛状态
如何在移动钱包中搭建一个小程序应用商店
中国电子学会五级考点详解(一)-string类型字符串
mindspore 执行模型转换为310的mindir文件显示无LRN算子
中移链EOSJS实战使用
Primavera Unifier 高级公式使用分享
自定义卷积核的分组转置卷积如何实现?
STM32之串口传输结构体
pycharm 取消msyql表达式高亮
安装ES7.x集群
基于C#通过PLCSIM ADV仿真软件实现与西门子1500PLC的S7通信方法演示
游戏服务器中集群网关的设计
基于 VIVADO 的 AM 调制解调(1)方案设计