Comet OJ - 2019 六一欢乐赛

传送门

#A:

思路:等差数列求和,看成俩次1+2+…+ n,多加的n减去,所以 ans = n*(n+1) - n。

AC代码:

 1 #include<iostream>
 2 #include<algorithm>
 3 #include<string>
 4 using namespace std;
 5 int main()
 6 {
 7     int n;
 8     while(cin >> n)
 9     {
10         cout << n*(n+1) - n << endl ;
11     }
12     return 0;
13 }

#B:

思路:n 最大只有 14,所以暴力搜索每个数选和不选的情况。

AC代码:

 1 #include<iostream>
 2 #include<cstdio>
 3 #include<algorithm>
 4 #include<cstring>
 5 using namespace std;
 6 int ans;
 7 int n;
 8 int a[20];
 9 int vis[20];
10 
11 int gcd(int a,int b)
12 {
13     if(b == 0)
14     return a;
15     else return gcd(b,a%b);
16 }
17 void dfs(int x)
18 {
19     if(x == n)
20     {
21         for(int i = 0;i < n;i++)
22             for(int j = i + 1;j < n;j++)
23                 if( vis[i] && vis[j] && gcd(a[i],a[j]) != 1 )
24                     return;
25         int cnt = 0;
26         for(int i = 0;i < n;i++)
27             if(vis[i])
28                 cnt++;
29         ans = max(ans,cnt);
30         return;
31     }
32     vis[x] = 1;
33     dfs(x + 1);
34     vis[x] = 0;
35     dfs(x + 1);
36 }
37 
38 int main()
39 {
40     int t;
41     cin >> t;
42     while(t--)
43     {
44         memset(vis,0,sizeof(vis));
45         scanf("%d",&n);
46         for(int i = 0;i < n;i++)
47             scanf("%d",&a[i]);
48         ans = 0;
49         dfs(0);
50         cout << ans << endl;
51     }
52     return 0;
53 }

#C

 

思路:先比较长度,然后从头到尾检索比较字符串a和b,如果不相等,将b滞后一位再比较(具体看代码),最后滞后量等于2,说明可以输出1,否则输出0.

AC代码:

 1 #include<cstdio>
 2 #include<iostream>
 3 #include<string>
 4 using namespace std;
 5 int main()
 6 {
 7     int t;
 8     cin >> t;
 9     while(t--)
10     {
11         string a,b;
12         cin >> a >> b;
13         if(a.size() - b.size() != 2) cout << "0" <<endl;
14         else
15         {
16             int f = 0;
17             for(int i = 0,j = 0;i < a.size();i++)
18             {
19                 if(a[i] != b[i - f]) f++;
20             }
21             if(f == 2) cout << "1" <<endl;
22             else cout << "0" <<endl;
23         }
24     }
25     return 0;
26 }

#D:

思路:签到题,直接模拟。

AC代码:

#include<iostream>
using namespace std;
int main()
{
    int a[14] = {0};
    int n;
    for(int i = 0;i < 18;i++)
    {
        cin >> n;
        a[n]++;
    }
    int ans = 0;
    for(int i = 0;i < 14;i++)
    {
        if(i)
        ans += a[i]%2;
        else ans += a[i];
    }
    cout << ans;
    return 0;
}

#F:

思路:这也是一道搜索题,首先每次变化后直接搜索能种树的位置肯定超时,所以我们要搜索每次变化后不能用的位置。容易推出最初的种树位置一共有 ans =(n - 1) *(m - 1)种,在总数 ans 减去 每次变化后 失去的位置即可。

AC代码:

#include<cstdio>
#include<algorithm>
#include<iostream>
#include<string>
#include<cstring>
using namespace std;
const int maxn = 1e3+5;
int dy[4] = {0,1,0,-1};
int dx[4] = {-1,0,1,0};
int mp[maxn][maxn];
int q;
int n,m;
bool check(int x,int y)
{
    if(x <= n && y <= m && x > 0 && y > 0 && mp[x][y] == 0)
    return true;
    else return false;
}
int dfs(int x,int y)
{
    int ans = 0;
    if(check(x,y) && check(x+1,y) && check(x,y+1) && check(x+1,y+1)) ans++;
    if(check(x,y) && check(x-1,y) && check(x,y-1) && check(x-1,y-1)) ans++;
    if(check(x,y) && check(x+1,y) && check(x,y-1) && check(x+1,y-1)) ans++;
    if(check(x,y) && check(x-1,y) && check(x,y+1) && check(x-1,y+1)) ans++;
    return ans;
}

int main()
{
    cin >> n >> m >> q;
    int ans = (n - 1) * (m - 1);
    memset(mp,0,sizeof(mp));
    while(q--)
    {
        int a,b;
        cin >> a >> b;
        ans -= dfs(a,b);
        mp[a][b] = 1;
        cout << ans << endl;
    }
    return 0;
}

猜你喜欢

转载自www.cnblogs.com/Carered/p/10961670.html
今日推荐