头像

jyc20101031

小学 2017级




离线:1天前


最近来访(1)
用户头像
gongcharlie

活动打卡代码 AcWing 3762. 二进制矩阵

#include <iostream>
#include <cstring>
#include <algorithm>

using namespace std;

int n, m;
char g[110][110];

void pL(int i, int j, int k)
{
    if (!k) cout << i << ' ' << j << ' ' << i + 1 << ' ' << j << ' ' << i << ' ' << j + 1 << endl;
    else if (k == 1) cout << i << ' ' << j - 1 << ' ' << i << ' ' << j << ' ' << i + 1 << ' ' << j << endl;
    else if (k == 2) cout << i - 1 << ' ' << j << ' ' << i << ' ' << j << ' ' << i << ' ' << j - 1 << endl;
    else cout << i - 1 << ' ' << j <<' ' << i <<' ' << j << ' ' << i << ' ' << j + 1 << endl;
}

int main()
{
    int T;
    cin >> T;
    while (T -- )
    {
        cin >> n >> m;
        int res = 0;
        for (int i = 1; i <= n; i ++ )
        {
            cin >> g[i] + 1;
            for (int j = 1; j <= m; j ++ ) if (g[i][j] == '1') res += 3;
        }
        cout << res << endl;
        for (int i = 1; i <= n; i ++ ){
            for (int j = 1; j <= m; j ++ ){
                if (g[i][j] == '1')
                {
                    if (i < n && j < m) pL(i, j, 0), pL(i, j + 1, 1), pL(i + 1, j, 3);
                    else if (i == n && j == m) pL(i, j, 2), pL(i - 1, j, 1), pL(i, j - 1, 3);
                    else if (i == n) pL(i, j, 3), pL(i - 1, j, 0), pL(i, j + 1, 2);
                    else pL(i, j, 1), pL(i, j - 1, 0), pL(i + 1, j, 2);
                }
            }
        }
    }
}


活动打卡代码 AcWing 3763. 数字矩阵

#include <iostream>
#include <cstring>
#include <algorithm>

using namespace std;

int main()
{
    int T;
    cin >> T;
    while (T -- )
    {
        int n, m;
        cin >> n >> m;
        int cnt = 0, sum = 0, minx = 999999999;
        for (int i = 0; i < n ; i ++ ){//遍历n×m个格子
            for (int j = 0; j < m ; j ++ ){
                int x;
                cin >> x;
                sum += abs(x);//假设是最好请况
                minx = min(minx, abs(x));//找最小
                if (x < 0) cnt ++ ;//负数数量加1
            }
        }
        //因为负数数量为偶数时可以成对,也就可以变成最好情况,否则就牺牲最小的绝对值使其变成负数
        //而最小数的绝对值减去了2乘它
        if (cnt % 2) cout << sum - 2 * minx << endl;//上面说的情况
        else cout << sum << endl;
    }
}


活动打卡代码 AcWing 3776. 水果拼盘

#include <iostream>
#include <cstring>
#include <algorithm>

using namespace std;

int main() 
{
    int t;
    cin >> t;
    while(t --)
    {
        int a, b, c, d, e, f, ans = 0;
        cin >> a >> b >> c >> d >> e >> f;
        if(e > f){
            ans += (min(a, d) * e);
            int tmp = min(a, d);
            a -= tmp, d -= tmp;
            ans += (min({b, c, d}) * f);
        }else{
            ans += (min({b, c, d}) * f);
            int tmp = min({b, c, d});
            b -= tmp, c -= tmp, d -= tmp;
            ans += (min(a, d) * e);
        } 
        cout << ans << endl;
    }
}




常规做法(参考y大佬)

没想到的中等难度思维题

时间复杂度

最多循环100×10×10次

参考文献

C++ 代码

#include <iostream>
#include <cstring>
#include <algorithm>

using namespace std;

int main()
{
    int T;
    cin >> T;
    while (T -- )
    {
        int n, m;
        cin >> n >> m;
        int cnt = 0, sum = 0, minx = 999999999;
        for (int i = 0; i < n ; i ++ ){//遍历n×m个格子
            for (int j = 0; j < m ; j ++ ){
                int x;
                cin >> x;
                sum += abs(x);//假设是最好请况
                minx = min(minx, abs(x));//找最小
                if (x < 0) cnt ++ ;//负数数量加1
            }
        }
        //因为负数数量为偶数时可以成对,也就可以变成最好情况,否则就牺牲最小的绝对值使其变成负数
        //而最小数的绝对值减去了2乘它
        if (cnt % 2) cout << sum - 2 * minx << endl;//上面说的情况
        else cout << sum << endl;
    }
}


活动打卡代码 AcWing 3773. 兔子跳

#include <iostream>
#include <cstring>
#include <algorithm>

using namespace std;

int main()
{
    int T;
    cin >> T;
    while (T -- )
    {
        int n, x;
        cin >> n >> x;
        int s = 0;
        int f = 0;
        for (int i = 0; i < n; i ++ ){
            int a;
            cin >> a;
            if (a == x) f = 1;
            s = max(a, s);
        }
        if (f) cout << 1 << endl;
        else if (x < s) cout << 2 << endl;
        else cout << (x + s - 1) / s << endl;
    }
}


活动打卡代码 AcWing 3770. 最小消耗

#include <iostream>
#include <cstring>
#include <algorithm>

using namespace std;

int main()
{
    int T;
    cin >> T;
    while (T -- )
    {
        int n, a, b, c, h;
        int ans = 0;
        string s;
        cin >> n >> a >> b >> c;
        h = a;
        cin >> s;
        if(b + c < h) a = b + c;
        if(h + c < b) b = h + c;
        for (int i = 0 ; s[i] ; i ++ ){
            if(s[i] == '0') ans += a;
            else ans += b;
        }
        cout << ans << endl;
    }
}



参考文献

C++ 代码

#include <iostream>
#include <cstring>
#include <algorithm>

using namespace std;

int main()
{
    int t ;
    long long s ;
    cin >> t ;
    while (t -- )
    {
        s = 0 ;
        long long n, m ;
        cin >> n >> m ;
        for (int i = 1; i <= n; i ++ ){
            if((m % i == 0) && (m / i <= n)) s ++ ; 
        }
        cout << s << endl ;
    }
}



参考文献

C++ 代码

#include <iostream>
#include <cstring>
#include <algorithm>

using namespace std;

int main()
{
    int n ;
    cin >> n ;
    while (n -- )
    {
        int a = 0 ;
        string s ;
        cin >> s ;
        for (int i = 0; i < s.size() ; i ++ ){
            cout << s[i] ;
            if (s[i] == '1'){
                a ++ ;
                if(a == 5) i ++ , a = 0 ;
            } 
            else a = 0 ;
        }
        cout << endl ;
    }
}


活动打卡代码 AcWing 3759. 第k个字符串

#include <iostream>
#include <cstring>
#include <algorithm>
using namespace std;
int main()
{
    int n, k;
    int t;
    cin >> t;
    while (t -- )
    {
        cin >> n >> k;
        string s(n, 'a');
        for (int i = n - 1; i ; i -- ){
            if(k > n - i) k -= n - i;
            else{
                s[i - 1] = s[n - k] = 'b';
                break;
            }
        }
        cout << s << endl;
    }
}


活动打卡代码 AcWing 3761. 唯一最小数

#include <iostream>
#include <cstring>
#include <algorithm>
using namespace std;
int s[10000000];
int main()
{
    int t;
    cin >> t;
    while (t -- )
    {
        int n;
        cin >> n;
        memset(s, 0, (n + 1) * 4);
        int a[n];
        int k = -1;
        for (int i = 0; i < n; i ++ ) cin >> a[i], s[a[i]] ++ ;
        for (int i = 0; i < n; i ++ ){
            if(s[a[i]] == 1){
                if(k == -1 || a[k] > a[i]) k = i;
            }
        }
        if(k != -1) k ++ ;
        cout << k << endl;
    }
}