The 14th Jilin Provincial Collegiate Programming Contest
题目连接:https://codeforces.com/gym/102800
Problem A. Chord
思路:这个题其实不难,就是把琴键都存入到一个二维数组中,根据输入的琴键,查找其所在位置(即下标),最后计算差值即可。
题解
#include<bits/stdc++.h>
using namespace std;
char a[12][5] = {{"C"},{"C#"},{"D"},{"D#"},{"E"},{"F"},{"F#"},{"G"},{"G#"},{"A"},{"A#"},{"B"}},b[2];
int t,c[5],m,n;
int main(){
cin >> t;
while(t--){
for(int i=0;i<3;i++){
cin >> b;
for(int j=0;j<12;j++){
if(strcmp(b,a[j])==0) {
c[i] = j;
break;
}
}
}
if(c[1]-c[0]<0)m=12+c[1]-c[0];
else m = c[1]-c[0];
if(c[2]-c[1]<0)n=12+c[2]-c[1];
else n = c[2]-c[1];
if(m==4&&n==3)cout <<"Major triad" << endl;
else if(m==3&&n==4)cout <<"Minor triad" << endl;
else cout << "Dissonance" << endl;
}
return 0;
}
其中,注意存入的字符型二维数组
①每行元素用 { } ;
②每行元素其实是一个字符串,所以用 “ ” 引用。
Problem B. Problem Select
思路:只要得到字符串结尾的数字,并将其存入到整形数组中,最后排序输出即可。
题解
#include<bits/stdc++.h>
using namespace std;
#define N 1001
int t,n,k,x,b[N];
char a[N];
int main(){
cin >> t;
while(t--){
cin >> n >> k;
for(int i=0;i<n;i++){
cin >> a;
int j=0;
while(!(a[j]>47&&a[j]<58))j++;
x = a[j] - '0';
while(a[++j]){
x = x*10 + (a[j]-'0');
}
b[i] = x;
}
sort(b,b+n);
for(int i=0;i<k;i++){
cout << b[i] << " ";
}
cout << endl;
}
return 0;
}
Problem C. String Game
思路:自己尝试动手做了这道题,但做完后,发现思路其实不正确,看题解,发现用的是DP,
题解
#include<bits/stdc++.h>
using namespace std;
const int N=1e5+5;
typedef long long ll;
string s,t;
ll dp[5005][5005];
const int mod=1e9+7;
int main(){
while(cin>>s>>t){
int n=s.size(),m=t.size();
for(int i=0;i<=n;i++)dp[0][i]=1;
for(int j=1;j<=m;j++)
for(int i=1;i<=n;i++){
dp[j][i]=0;
}
for(int i=1;i<=m;i++){
for(int j=1;j<=n;j++){
dp[i][j]=dp[i][j-1];
if(t[i-1]==s[j-1]){
dp[i][j]+=dp[i-1][j-1];
}
dp[i][j]%=mod;
}
}
cout<<dp[m][n]<<endl;
}
}
Problem E. Shorten the Array
Problem L. Swimmer
题解
#include<bits/stdc++.h>
using namespace std;
const int N=1e6+5;
typedef long long ll;
int x[N];
int n,m,q;
int p,k;
int main(){
cin>>n>>m>>q;
for(int i=1;i<=n;i++)scanf("%d",&x[i]);
while(q--){
scanf("%d%d",&p,&k);
ll l=1LL*p*x[k];
if (l<=m){
printf("%lld\n",l);
continue;
}else{
ll t=l/m;
if (t&1){
printf("%lld\n",1LL*m-(l-m*t));
}else{
printf("%lld\n",l-m*t);
}
}
}
}
其他没有写的题解 ,参考此处