目录
1002题 C++ to Python
分析:
把 “std::make_tuple” 中的字符忽略即可。
代码:
#include<iostream>
using namespace std;
int main()
{
int T;
cin>>T;
string out;
string in;
while(T--)
{
out="";
cin>>in;
for(int i=0;i<in.length();i++){
if((in[i]>=48&&in[i]<=57)||in[i]=='('||in[i]==')'||in[i]=='-'||in[i]==',')
out+=in[i];
}
cout<<out<<endl;
}
}1007题 Snatch Groceries
分析:
按照区间左端点排序,然后暴力枚举即可。
代码:
#include<iostream>
#include<algorithm>
using namespace std;
typedef struct{
int st;
int ed;
} people;
bool cmp(people a,people b)
{
return a.st<b.st;
}
int main()
{
int t;
cin>>t;
int num,st,ed,cnt;
while(t--)
{
cnt=0;
cin>>num;
people time[num];
for(int i=0;i<num;i++)
{
cin>>st>>ed;
time[i].st=st;
time[i].ed=ed;
}
sort(time,time+num,cmp);
bool flag=true;
for(int i=1;i<num;i++)
{
if(time[i].st>time[i-1].ed)
{
cnt++;
}
else
{
flag=false;
break;
}
}
if(flag==true)
cnt++;
cout<<cnt<<endl;
}
}1009题 ShuanQ
分析:
先找出M,然后可以通过 ans = x * q mod M 直接计算出明文
代码:
#include <stdio.h>
#include <string.h>
#include <math.h>
typedef long long ll;
//const int N=2e6+1;
//bool prime[N];
int main()
{
//
// memset(prime,1,sizeof(prime));
// prime[0]=0;
// prime[1]=0;
// for(int i=2;i<N;i++){
// if(prime[i]==1){
// for(int j=i+i;j<N;j+=i){
// prime[j]=0;
// }
// }
// }
int t;
scanf("%d",&t);
for(int i=0;i<t;i++){
ll p,q,e;
scanf("%lld %lld %lld",&p,&q,&e);
ll maxOfpq=p>q?p:q;
ll j=p*q-1;
ll m=0;
ll r=j;
for(ll k=2;k<=j/k;k++){
while(j%k==0){
j/=k;
}
}
m=j;
if(maxOfpq<m){
printf("%lld\n",e*q%m);
}
else{
printf("shuanQ\n");
}
}
}分析:
n 较小,直接背包dp。n 很大时,用365填充到一个较小的范围,再dp。
代码:
#include<bits/stdc++.h>
using namespace std;
const int N=1e5;
long long dp[N];
void solve(){
int i;
long long n,ans=0;
cin>>n;
if(n>76205){//n很大时,76205为7、31、365的最小公倍数
ans+=(n-76205)/365;
n-=ans*365;
}
if(dp[n]>1e17)cout<<-1<<endl;
else cout<<ans+dp[n]<<endl;
}
int main(){
for(int i=1;i<80000;i++)dp[i]=1e18+1;
for(int i=365;i<80000;i+=365)dp[i]=dp[i-365]+1;
for(int i=31;i<80000;i++)dp[i]=min(dp[i],dp[i-31]+1);
for(int i=7;i<80000;i++)dp[i]=min(dp[i],dp[i-7]+1);
int t;
cin>>t;
while(t--)solve();
}
版权声明:本文为Scorpion_xty原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。