Official
A - Pawn on a Grid Editorial by en_translator
According to the input format, the problem is equivalent to: “given \(H\) strings of length \(W\) each, find the sum of occurrences of #
in each string.” This can be implemented with a for statement and a if statement, or with the count function.
Sample code in C++ (if statement):
#include <bits/stdc++.h>
using namespace std;
int main() {
int h,w,ans=0;
string s;
cin >> h >> w;
for(int i=0;i<h;i++){
cin>>s;
for(int j=0;j<w;j++){
if(s[j]=='#')ans++;
}
}
cout << ans <<endl;
return 0;
}
Sample code in Python (if statement):
h,w = map(int, input().split())
ans=0
for i in range(h):
s=input()
for j in range(w):
if s[j]=='#':
ans += 1
print(ans)
Sample code in C++ (count function):
#include <bits/stdc++.h>
using namespace std;
int main() {
int h,w,ans=0;
string s;
cin >> h >> w;
for(int i=0;i<h;i++){
cin>>s;
ans += count(s.begin(), s.end(), '#'); // only this line differes from the if-statement one
}
cout << ans <<endl;
return 0;
}
Sample code in Python (count function):
h,w = map(int, input().split())
ans=0
for i in range(h):
ans += input().count('#')
print(ans)
posted:
last update: