E题(恶心的规规矩矩/条条框框题)
时间
题目描述
判断字符串是否为时间标准格式
时间标准格式为时:分:秒,采用24小时制
输入
输入第一行有一个整数T,代表接下来有T组测试数据
接下来T行,每一行输入一个字符串s
数据范围:1≤T≤ 1 0 3 10^{3} 103 ,1≤len(s)≤8
输出
对于每组测试数据,如果是标准时间格式,输出"Yes",否则输出"No"
样例输入
8
00:00:00
21:09:34
24:00:00
13:61:45
7:7:7
04:-2:45
shijian
time
样例输出
Yes
Yes
No
No
No
No
No
No
这题恶心死我了,我WA了好几次
第一次 WA的代码:
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<string>
#include<iostream>
#include<math.h>
const int amx = 110;
using namespace std;
char ch[amx];
int main()
{
int t;
scanf("%d",&t);
bool f =true;
bool key =true;
while(t--)
{
if (f==true)
{
getchar();
f=false;
}
gets(ch);
// puts("*********************888\n");
// puts(ch);
int l =strlen(ch);
int i=l;
if(ch[i-3]!=':'||ch[i-6]!=':')
{
key= false;
}
if( (((ch[l-2]-'0')*10+(ch[i-1]-'0')) >=0) &&(((ch[l-2]-'0')*10+(ch[l-1]-'0')) <60) &&key == true)
{
// puts("*********************mmmmmmmmmmmmmm\n");
if(((ch[i-5]-'0')*10+(ch[i-4]-'0')) >=0 &&((ch[i-5]-'0')*10+(ch[i-4]-'0')) <60)
{
if(((ch[i-8]-'0')*10+(ch[i-7]-'0')) >=0 &&((ch[i-8]-'0')*10+(ch[i-7]-'0')) <24)
{
key =true;
}
else
{
key = false;
}
}
else
{
key = false;
}
}
else
key = false;
if(key==true)printf("Yes\n");
if(key==false)printf("No\n");
}
return 0;
}
一开始没有考虑完全,然后乱用变量(上面还有我乱用的痕迹)
遇到这类题目,一定要往最严格的条件写代码。
AC码:
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<string.h>
#include<iostream>
#include<math.h>
using namespace std;
const int amx = 10010;
char ch[amx];
int main()
{
int t;
scanf("%d",&t);
bool key =true;
while(t--)
{
int o =0 ;
scanf("%s",ch);
int l =strlen(ch);
if(l!=8)
{
key =false;
}
if((ch[2]!=':')||(ch[5]!=':'))
key =false;
for(int i=0; i<l; i++)
{
if((ch[i]>='0')&&(ch[i]<='9'))
o++;
}
if(o!=6)
key =false;
if(( (ch[0]-'0')*10+(ch[1]-'0') <24 ) &&( (ch[0]-'0')*10+(ch[1]-'0')>=0 ) &&(key==true ) )
{
if(( (ch[3]-'0')*10+(ch[4]-'0') <60 ) &&( (ch[3]-'0')*10+(ch[4]-'0')>=0 ) )
{
if(( (ch[6]-'0')*10+(ch[7]-'0') <60 ) &&( (ch[6]-'0')*10+(ch[7]-'0')>=0 ) )
{
key =true;
}
else
key =false;
}
else
key =false;
}
else
key =false;
if(key==true)
{
printf("Yes\n");
}
else
printf("No\n");
key = true;
}
return 0;
}
B题 区间排序
题目描述
现在有一个序列,请按要求的操作进行处理。
1 L R:表示从l到r进行递增排序
2 L R:表示从l到r进行递减排序
输入
t组数据,每组数据第一行有两个数n,m,第二行有n个整数a1…an。接下来m行,每行有3个整数,代表要求执行的操作(1或2)和操作的左右区间L,R。(t≤100,n,m≤104;1≤L≤R≤n,1≤ai≤10e4)
输出
输出处理后的序列。
样例输入
1
10 5
5 12 3 1 15 2 13 4 54 21
1 2 3
2 5 9
1 4 10
2 3 9
1 1 2
样例输出
3 5 21 15 13 12 4 2 1 54
这题只要一开始考虑到sort()的排序函数,就立马能解题
AC码
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<string>
#include<iostream>
const int amx = 10010;
using namespace std;
int a[amx];
int b[amx];
int c[amx];
int cmp(int x,int y)
{
return x>y;
}
int main()
{
int t;
scanf("%d",&t);
while(t--)
{
int n,m;
scanf("%d %d ",&n,&m);
for(int k=0; k<n; k++)
{
scanf("%d",&a[k]);
}
while(m--)
{
int info,l,r;
scanf("%d%d%d",&info,&l,&r);
if(info==1)
sort(a+l-1,a+r);
else
sort(a+l-1,a+r,cmp);
}
for(int k=0; k<n; k++)
{
printf("%d",a[k]);
if(k==n-1)
{
printf("\n");
}
else
printf(" ");
}
}
return 0;
}
A题 最少排序
现在有1-n n个数字组成的序列,求出最少需要交换多少次就能使其变成一个递增序列。
学姐说从环这个角度考虑
参考博文
学完图论再来康康;
此时已经2021年1月3日22:37:20了
洗洗睡去~
预告
Vjudge J题(map+结构体