7月21日考试总结
T5:P1223 排队接水
这道题考试时没有看懂题目,没有代码思路,只输出了样例
错误代码:
#include<bits/stdc++.h>
using namespace std;
const int N=1010;
int a[N];
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int n;
cin>>n;
for(int i=1; i<=n; i++){
cin>>a[i];
}
cout<<fixed<<setprecision(2);
cout<<"3 2 7 8 1 4 9 6 10 5\n291.90";
return 0;
}
思路分析:
题目简化过来就是让你把排队接水人的时间从小到大排序一遍,再输出现在排列人员之前的序号,并for循环i从1到n,ans+=a数组中的前n-i个数,保留两位小数输出ans/n。
AC代码
#include<bits/stdc++.h>
using namespace std;
const int N=1010;
struct db{
int id,z;
}a[N];
bool f(db b,db c){
return b.z<c.z;
}
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int n;
cin>>n;
for(int i=1; i<=n; i++){
cin>>a[i].z;
a[i].id=i;
}
sort(a+1,a+n+1,f);
double t=0;
for(int i=1; i<=n; i++){
cout<<a[i].id<<" ";
for(int j=1; j<=n-i; j++){
t+=a[j].z;
}
}
cout<<"\n";
cout<<fixed<<setprecision(2)<<t/n;
return 0;
}
T6 P1454 圣诞夜的极光:
错误原因:写题时考虑的是用一个X数组来存烟花的行,再用一个Y数组来存烟花的列,再用公式来判断输出的得数是否>2,如果>2就ans++,输出ans%10,归根结底思路没理清写了一堆废代码,仅输出一个样例。
错误代码:(废物代码)
#include<bits/stdc++.h>
using namespace std;
int x[110],y[110];
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int n,m;
cin>>n>>m;
for(int i=1; i<=n; i++){
for(int j=1; j<=m; j++){
char c;
cin>>c;
if(c=='#'){
x[i]=i;
y[j]=j;
}
}
}
int ans=0;
for(int i=1; i<=n; i++){
for(int j=1; j<=m; j++){
if((x[i]-x[i+1])+(y[j]-y[j-1])>2){
ans++;
}
}
}
cout<<ans%10;
return 0;
}
思路分析:
题目就是让我们用递归十二个方向并用公式判断周边的字符是否为同一个图案,如果递归彻底无法进行,图形块数+1。
AC代码:
#include<bits/stdc++.h>
using namespace std;
char a[110][110];
bool vis[110][110];
long long n,m;
void dfs(long long x,long long y){
vis[x][y]=1;
for(int i=-2; i<=2; i++){
for(int j=-2; j<=2; j++){
long long nx=x+i,ny=y+j;
if(abs(x-nx)+abs(y-ny)>2 || nx<1 || nx>n || ny<1 || ny>m || a[nx][ny]=='-' || vis[nx][ny]==1){
continue;
}
dfs(nx,ny);
}
}
}
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
cin>>n>>m;
for(int i=1; i<=n; i++){
for(int j=1; j<=m; j++){
cin>>a[i][j];
}
}
long long ans=0;
for(int i=1; i<=n; i++){
for(int j=1; j<=m; j++){
if(a[i][j]=='#' && vis[i][j]==0){
ans++;
dfs(i,j);
}
}
}
cout<<ans;
return 0;
}