2020年5月月赛丙组——上海市计算机学会竞赛平台
T1
戴口罩
#include <bits/stdc++.h>
using namespace std;int main() {
int t=5,k=5,n,z=1;
cin>>n;
while (k!=n){
if (z==1||z==2)k+=7;
k--;
t++;
if (z==7)z=1;
else z++;
}
cout<<t;
return 0;
}
T2
计算GPA
#include <bits/stdc++.h>
using namespace std;double k, n;
int main() {
string a;
cin >> a;
for (int i = 0; i < a.length(); ++i) {
switch(a[i]) {
case 'A': k += 4; break;
case 'B': k += 3; break;
case 'C': k += 2; break;
case 'D': k += 1; break;
case '+': k += 0.3f; break;
default:
if(a[i] == '-')k -= 0.3f;
}
}
for(int i=0;i<a.size();++i){
if(a[i]>'='&&a[i]<='D')n++;
}
printf("%.2lf",k/n);
return 0;
}
T3
扫雷
#include <bits/stdc++.h>
using namespace std;
int a[101][101];
const int dr[][2] = {{0, 1}, {0, -1}, {-1, 0}, {1, 0}, {-1, -1}, {-1, 1}, {1, -1}, {1, 1}};void f(int x, int y) {
int k = 0;
for (int i = 0; i < 8; ++i) {
if (a[x + dr[i][0]][y + dr[i][i]] == -2)
k++;
}
if (a[x][y] == -2)
a[x][y] = k;
} // End of function fint main() {
int n, m;
char q;
cin >> n >> m;
for (int i = 1; i <= n; ++i) {
for (int j = 1; j <= m; ++j) {
cin >> q;
if (q == '.')
a[i][j] = -3;
else if (q == '*')
a[i][j] = -4;
}
}
for (int i = 1; i <= n; ++i) {
for (int j = 1; j <= m; ++j) {
f(i, j);
if (a[i][j] == -4)
cout << '*';
else
cout << a[i][j];
}
cout << endl;
}
return 0;
} // End of main function
T4
增长与翻倍
#include <bits/stdc++.h>
using namespace std;
int main() {
integer variables s, t, and k initialized to zero.
cin>>s>>t;
while loop that continues until the values of s and t are equal.
if either t is at least twice the value of s or t is an even number, then divide t by two.
else decrement the value of t by one.
increment the counter k by one each iteration.
once the loop terminates output the value of k.
return 0;
}
T5
最大回撤
#include <bits/stdc++.h>
using namespace std;stack
s;
int main() {
int n,p;
int maxn=0;
cin>>n;
s.push(-100100);
for (int i=0;i<n;i++){
cin>>p;
if (p>s.top())s.push(p);
else {
int sum=s.top();
int k=sum-p;
maxn=max(maxn,k);
}
}
cout<<maxn;
return 0;
}
