A、组队比赛
题意:输入四个数字,分为两组,问两组数的最小值。
思路:
最大的和最小的一对,结果取绝对值。复杂度 (1)。
Code:
#include<bits/stdc++.h>
using namespace std;
int read(){
int x=0;char ch=getchar();
while(!isdigit(ch)) ch=getchar();
while(isdigit(ch)) x=(x<<3)+(x<<1)+(ch^48),ch=getchar();
return x;
}
void print(int x){
if(x>=10) print(x/10);
putchar(x%10+'0');
}
int a[4];
int main() {
a[0]=read(),a[1]=read(),a[2]=read(),a[3]=read();
sort(a,a+4);
print(abs(a[3]+a[0]-a[1]-a[2])),puts("");
} B、每日一报
题意:发送n条信息,每条包括日期学号和体温,体温大于等于38.0的要上报,按要求输出需要上报的人数和信息。
思路:
输入的信息中温度大于等于38.0的存在结构体中,在结果体中重载<,因为sort排序需要用到<,然后sort排序输出就可以了。复杂度 (nlog n)。
Code:
#include<bits/stdc++.h>
#define ll long long
#define js ios::sync_with_stdio(false);cin.tie(0);cout.tie(0)
using namespace std;
struct node{
int tm,id;
double wdu;
bool operator<(const node &a) const{
if(tm!=a.tm)
return tm>a.tm;
else if(wdu!=a.wdu)
return wdu>a.wdu;
else
return id<a.id;
}
}a[100];
int n,cnt;
int main() {
js; node b;
cin>>n;
for(int i=1;i<=n;++i) {
cin>>b.tm>>b.id>>b.wdu;
if(b.wdu>=38.0)
a[cnt++]=b;
}
sort(a,a+cnt);
cout<<cnt<<endl;
for(int i=0;i<cnt;++i) cout<<fixed<<setprecision(1)<<a[i].tm<<" "<<a[i].id<<" "<<a[i].wdu<<endl;
} C、最长非公共子序列
题意:就是找一个最长的子序列只存在一个序列中。
思路:
思维题,最长公共子序列是dp,但是加个非就是一眼题了(我当时还想dp去写),如果两个字符串相同,就不存在最长公共子序列,输出-1,反之答案就是最长的字符串的长度。
Code:
#include<bits/stdc++.h>
#define js ios::sync_with_stdio(false);cin.tie(0);cout.tie(0)
using namespace std;
string s1,s2;
map<string,int>mp;
int main() {
js;
cin>>s1>>s2;
++mp[s1];
++mp[s2];
if(mp[s1]==2) cout<<"-1"<<endl;
else {
cout<<max(s1.size(),s2.size())<<endl;
}
return 0;
} D、最大字符集
题意:给一个长度n,用01组成m个字符串,大的通过删掉头或尾不能得到小的串,且长度不超过n,输出m和可能的组合。
思路:
规律题,当n大于2,可以有如下方式组合:
11
101
1001
10001 有n-1个符合要求的串。
当n=1,输0或1,n=2,输0,11或者1,00。
代码:
#include<bits/stdc++.h>
#define ll long long
#define js ios::sync_with_stdio(false);cin.tie(0);cout.tie(0)
using namespace std;
int n;
int main() {
js;
cin>>n;
if(n==1) cout<<"1\n0\n";
if(n==2) cout<<"2\n1\n00\n";
if(n>2) {
cout<<n-1<<endl;
cout<<"11\n";
for(int i=3;i<=n;++i) {
cout<<"1";
for(int j=1;j<=i-2;++j) cout<<"0";
cout<<"1\n";
}
}
return 0;
} E、美味的序列
题目:n个零食要吃,每个零食都有一个美味度,没吃完一个其它的零食美味度都会减一。
思路:
因为就是减到负的都要吃,所以答案就是全部零食的美味度之和减去减少的美味度,全部的美味度之和预处理就可以了,减少的美味度就是一个首项为1,公差为1的等差数列的前n-1项和。
Code:
#include<bits/stdc++.h>
#define ll long long
#define js ios::sync_with_stdio(false);cin.tie(0);cout.tie(0)
using namespace std;
ll a,n;
ll ans;
int main() {
js;
cin>>n;
for(int i=1;i<=n;++i) {
cin>>a;
ans+=a;
}
ans-=n*(n-1)/2;
cout<<ans<<endl;
} F、日期小助手
题意:给出一个日期,输出下一个父亲节或者母亲节是什么时候。
思路:
默认1900年1月1日是星期一,根据这个算出1900年到年份x的母亲节或父亲节的的天数,然后算出是星期几,最后求得日期,注意闰年天数+1。母亲节的范围是 ,父亲节的范围是
,注意只有21是st,注意特判。
Code:
#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<queue>
#include <vector>
#include<map>
#include<set>
#include<utility>
#define scanf1(a) scanf("%d",&a)
#define scanf2(a,b) scanf("%d%d",&a,&b)
#define mes(a,b) memset(a,b,sizeof a)
#define js ios::sync_with_stdio(false);cin.tie(0);cout.tie(0)
#define ll long long
#define inf 0x3f3f3f3f
using namespace std;
int calcmother(int year) {
int n, whichday, motherday, s = 0;
for (n = 1900; n < year; n++)
{
if ((n % 4 == 0 && n % 100 != 0) || (n % 400 == 0))
s = s + 366;
else
s = s + 365;
}
if ((year % 4 == 0 && year % 100 != 0) || year % 400 == 0)
{
s = s + 121;
whichday = s % 7;
motherday = (14 - whichday);
return motherday;
}
else
{
s = s + 120;
whichday = s % 7;
motherday = (14 - whichday);
return motherday;
}
}
int calcfather(int year) {
int n, whichday, fatherday, s = 0;
for (n = 1900; n < year; n++)
{
if ((n % 4 == 0 && n % 100 != 0) || (n % 400 == 0))
s = s + 366;
else
s = s + 365;
}
if ((year % 4 == 0 && year % 100 != 0) || year % 400 == 0)
{
s = s + 152;
whichday = s % 7;
fatherday = (21 - whichday);
return fatherday;
}
else
{
s = s + 151;
whichday = s % 7;
fatherday = (21 - whichday);
return fatherday;
}
}
int t,year,mon,day;
int main() {
scanf1(t);
while (t--) {
scanf1(year),scanf1(mon), scanf1(day);
int montherdaynow = calcmother(year);
int montherdaynext = calcmother(year + 1);
int fatherdaynow = calcfather(year);
if (mon < 5 || mon == 5 && day < montherdaynow)
printf("Mother's Day: May %dth, %d\n", montherdaynow, year);
else if (mon < 6 || mon == 6 && day < fatherdaynow)
if (fatherdaynow == 21)
printf("Father's Day: June %dst, %d\n", fatherdaynow, year);
else
printf("Father's Day: June %dth, %d\n", fatherdaynow, year);
else
printf("Mother's Day: May %dth, %d\n", montherdaynext, year + 1);
}
return 0;
}

京公网安备 11010502036488号