代码:
#include
using namespace std;
const int MAXN = 10010;
int arr[MAXN];
int main(){
int n, a, ans;
cin >> n;
while(n --){
cin >> a;
arr[a] ++;
if(arr[a] > arr[ans] || (arr[a] == arr[ans] && a < ans))
ans = a;
}
cout << ans << endl;
return 0;
}
#include
#include
using namespace std;
int main(){
string s;
int cnt = 1, sum = 0;
cin >> s;
for(int i = 0; i < s.length() - 1; i ++){
if(s[i] == '-')
continue;
sum += cnt * (s[i] - '0');
++ cnt;
}
sum = sum % 11;
if(s[s.length() - 1] == 'X' && sum == 10)
cout << "Right" ;
else if(s[s.length() - 1] != 'X' && s[s.length() - 1] == sum + '0')
cout << "Right";
else{
if(sum == 10)
s[s.length() - 1] = 'X';
else
s[s.length() - 1] = sum + '0';
cout << s;
}
return 0;
}
代码:
#include
#include
using namespace std;
const int MAXN = 1010;
int arr[MAXN];
int main(){
int n, ans = 0;
cin >> n;
for(int i = 0; i < n; i ++)
cin >> arr[i];
for(int i = 0; i < n; i ++){
int t = arr[i];
for(int j = i; j < n; j ++){
t = min(t, arr[j]);
ans = max(ans, (j - i + 1) * t);
}
}
cout << ans << endl;
return 0;
}
代码:
#include
using namespace std;
const int MAXN = 1010, MOD = 1e9 + 7;
int arr[MAXN][MAXN];
int main(){
//预先处理组合数
for(int i = 0; i < MAXN; i ++)
for(int j = 0; j <= i; j ++)
if(j) arr[i][j] = (arr[i - 1][j] + arr[i - 1][j - 1]) % MOD;
else arr[i][j] = 1;
int n, ans = 0;
cin >> n;
//枚举 01个数
for(int i = 2; i <= n - 2; i ++){
ans = (ans + (long long)arr[n - 1][i] * (i - 1) * (n - i - 1)) % MOD;
}
cout << ans;
return 0;
}
代码:
#include
using namespace std;
const int MAXN = 60;
//记录两次bfs
bool vis1[MAXN][MAXN], vis2[MAXN][MAXN];
char g[MAXN][MAXN];
int fx[4][2] = {{0, 1}, {0, -1}, {1, 0}, {-1, 0}};
int n, m;
//判断r c这个格子能够向 i 方向走
bool check(int r, int c, int i){
if(g[r][c] == '+')
return true;
else if(g[r][c] == 'S' || g[r][c] == 'T')
return true;
else if(g[r][c] == '-' && i < 2)
return true;
else if(g[r][c] == '|' && i > 1)
return true;
else if(g[r][c] == '.' && i == 2)
return true;
else
return false;
}
//正向bfs
void dfs1(int r, int c){
vis1[r][c] = true;
for(int i = 0; i < 4; i ++){
int a = r + fx[i][0];
int b = c + fx[i][1];
if(a >= 0 && a < n && b >= 0 && b < m && g[a][b] != '#' && !vis1[a][b] && check(r, c, i))
dfs1(a, b);
}
}
//反向bfs
void dfs2(int r, int c){
vis2[r][c] = true;
for(int i = 0; i < 4; i ++){
int a = r + fx[i][0];
int b = c + fx[i][1];
//留意 i ^ 1就得到了相反的方向 我们一开始定义的方向 0右 1左 2下 3上
if(a >= 0 && a < n && b >= 0 && b < m && g[a][b] != '#' && !vis2[a][b] && check(a, b, i ^ 1))
dfs2(a, b);
}
}
int main(){
int r1, c1, r2, c2;
cin >> n >> m;
for(int i = 0; i < n; i ++){
for(int j = 0; j < m; j ++){
cin >> g[i][j];
if(g[i][j] == 'S')
r1 = i, c1 = j;
if(g[i][j] == 'T')
r2 = i, c2 = j;
}
}
dfs1(r1, c1);
//正向bfs不能够到达终点
if(!vis1[r2][c2])
cout << "I'm stuck!" ;
else{
//反向bfs
dfs2(r2, c2);
int ans = 0;
//计算满足要求的方格数量
for(int i = 0; i < n; i ++)
for(int j = 0; j < m; j ++)
if(vis1[i][j] && !vis2[i][j])
++ ans;
cout << ans << endl;
}
return 0;
}