AcWing 1211. 蚂蚁感冒
原题链接
简单
作者:
冷丁Hacker
,
2020-09-11 19:13:00
,
所有人可见
,
阅读 585
蚂蚁干膜 (不一样的题解)(写的不好 也想求赞)
解法1 过程纯模拟
#include<bits/stdc++.h>
using namespace std;
const int N = 55;
//int a[N];
int n;
struct node {
float dis;
int dre;
int gan;
node() {
dis = 0;
dre = 0;
gan = 0;
}
node(int a, int b, int c) {
dis = a;
dre = b;
gan = c;
}
}mayi[N];
int main() {
int first;
cin >> n;
for (int i = 1; i <= n; i++) {
int num,a,b,c;
cin >> num;
a = abs(num);
if (num < 0)b = -1;
else b = 1;
if (i == 1) {
c = 1;
}
else c = 0;
mayi[i] = node(a, b, c);
}
for (int i = 1; i <= 100; i++) {
for (int j = 1; j <= n; j++) {
if (mayi[j].dre == -1) {
mayi[j].dis-=0.5;
}
else mayi[j].dis+=0.5;
}
for (int k = 1; k <= n; k++) {
for(int o=k+1;o<=n;o++){
if (mayi[k].dis == mayi[o].dis) {
if (mayi[k].gan == 1 || mayi[o].gan == 1) {
mayi[k].gan = 1;
mayi[o].gan = 1;
mayi[k].dre = -mayi[k].dre;
mayi[o].dre = -mayi[o].dre;
}
}
}
}
}
int res = 0;
for (int i = 1; i <= n; i++) {
if (mayi[i].gan == 1)
res ++ ;
}
cout << res << endl;
return 0;
}
解法二 y总找规律
#include<bits/stdc++.h>
using namespace std;
const int N = 55;
int n;
int x[N];
int main()
{
cin >> n;
for (int i = 0; i < n; i++)
cin >> x[i];
int left = 0, right = 0;
for (int i = 1; i < n; i++) {
if (abs(x[i]) < abs(x[0]) && x[i] > 0)
left++;
else if (abs(x[i]) > abs(x[0]) && x[i] < 0)
right++;
}
if (x[0] < 0 && left == 0 || x[0]>0 && right == 0)
cout << 1 << endl;
else cout << 1 + right + left << endl;
return 0;
}