• 爬格子呀--第三章课后习题


    要开始期末复习了

    代码如下:
    Uva252迷宫

    #include<cstdio>
    #include<iostream>
    #include<string.h>
    using namespace std;
    
    char a[15][15];
    int b[15][15], r, c;
    
    bool check(int x, int y) {
        return a[x - 1][y] == '*' || a[x][y - 1] == '*';
    }
    
    int main() {
        int kase = 1;
        bool kk = true;
        //FILE*fp = fopen("stdout.txt", "w");
        while (scanf("%d", &r) && r != 0) {
            scanf("%d", &c);
            getchar();
            memset(a, '0', sizeof(a));
            memset(b, 0, sizeof(b));
            int num = 1;
            for (int i = 0; i < r; i++) {
                for (int j = 0; j < c; j++) {
                    scanf("%c", &a[i][j]);
                    if ((i == 0 || j == 0 || check(i, j)) && a[i][j] != '*')
                        b[i][j] = num++;
                }
                getchar();
            }
            if (!kk) {
                printf("
    ");
            }
            kk = false;
            printf("puzzle #%d:
    Across
    ", kase++);
            for (int i = 0; i < r; i++)
                for (int j = 0; j < c; j++)
                    if (b[i][j] != 0) {
                        int k = j;
                        printf("%3d.", b[i][k]);
                        for (; a[i][k] != '*'&&k < c; k++)
                            printf("%c", a[i][k]);
                        j = k;
                        printf("
    ");
                    }
            printf("Down
    ");
            for (int i = 0; i < r; i++)
                for (int j = 0; j < c; j++)
                    if (b[i][j] != 0) {
                        int k = i;
                        printf("%3d.", b[k][j]);
                        for (; a[k][j] != '*' && k < r; k++) {
                            printf("%c", a[k][j]);
                            b[k][j] = 0;
                        }
                        printf("
    ");
                    }
        }
        return 0;
    }

    Uva1368DNA序列

    #include<cstdio>
    #include<iostream>
    #include<map>
    #include<algorithm>
    #include<vector>
    using namespace std;
    typedef pair<char, int> P;
    
    struct cmd {
        bool operator()(const P& a, const P& b) {
            return a.second > b.second;
        }
    };
    
    int main() {
        int kase, m, n;
        scanf("%d", &kase);
        while (kase--) {
            scanf("%d %d", &m, &n);
            map<char, int>tar;
            char a[50][1000] = {}, ans[1000] = {};
            int num = 0;
            tar.insert({ 'A',0 });
            tar.insert({ 'G',0 });
            tar.insert({ 'C',0 });
            tar.insert({ 'T',0 });
            for (int i = 0; i < m; i++)
                scanf("%s", a[i]);
            for (int j = 0; j < n; j++) {
                for (int i = 0; i < m; i++) {
                    tar[a[i][j]]++;
                }
                vector<P>store(tar.begin(), tar.end());
                sort(store.begin(), store.end(), cmd());
                bool flag = true;
                int k;
                char c = store[0].first;
                for (k = 0; k < 3; k++) {
                    if (store[k].second != store[k + 1].second) {
                        flag = false;
                        break;
                    }
                    c = min(c, store[k + 1].first);
                }
                num += (m - store[0].second);
                if (k == 0)
                    ans[j] = store[0].first;
                else
                    ans[j] = c;
                for (map<char, int>::iterator it = tar.begin(); it != tar.end(); it++)
                    it->second = 0;
            }
            printf("%s
    %d
    ", ans, num);
        }
        return 0;
     }

    Uva202循环小数位数

    #include<cstdio>
    #include<iostream>
    #include<cstring>
    #include<cmath>
    #include<vector>
    #include<algorithm>
    using namespace std;
    typedef long long LL;
    
    int main() {
        int n, m, a, b;
        bool time = true;
        //FILE* fp;
        //fp = fopen("stdout.txt", "w");
        while (scanf("%d %d", &a, &b)!=EOF) {
    
            //if (a == 7777)
            //  break;
            //if (!time)
            //  printf("
    ");
            //time = false;
            n = a, m = b;
            vector<int>v, store;
            if (n == 0) {
                printf("%d/%d = 0.(0)
       1 = number of digits in repeating cycle
    
    ", n, m);
                //fprintf(fp, "%d/%d = 0.(0)
       1 = number of digits in repeating cycle
    
    ", n, m);
                continue;
            }
            store.push_back(n / m);
            n = n % m;
            bool flag = false;
            int kase = 0;
            int num, start;
            for (;;) {
                v.push_back(n);
                n *= 10;
                store.push_back(n / m);
                n = n % m;
                if (n == 0) {
                    flag = true;
                    break;
                }
                if (find(v.begin(), v.end(), n) != v.end()) {
                    start = find(v.begin(), v.end(), n) - v.begin();
                    num = v.size() - start;
                    break;
                }
            }
            printf("%d/%d = %d.", a, b, store[0]);
            //fprintf(fp,"%d/%d = %d.", a, b, store[0]);
            if (flag) {
                if (store[1] == 0 && store.size() == 2) {
                    printf("(0)
       1 = number of digits in repeating cycle
    
    ");
                    //fprintf(fp, "(0)
       1 = number of digits in repeating cycle
    
    ");
                    continue;
                }
                for (int i = 1; i < store.size(); i++)
                    printf("%d", store[i]);
                    //fprintf(fp,"%d", store[i]);
                printf("(0)
       1 = number of digits in repeating cycle
    
    ");
                //fprintf(fp,"(0)
       1 = number of digits in repeating cycle
    
    ");
            }
            else {
                for (int i = 1; i <= start; i++)
                    printf("%d", store[i]);
                    //fprintf(fp,"%d", store[i]);
                printf("(");
                //fprintf(fp,"(");
                for (int i = start+1; i <= (num > 50 ? 50 : store.size() - 1); i++)
                    printf("%d", store[i]);
                    //fprintf(fp,"%d", store[i]);
                if (num > 50)
                    printf("...)
       %d = number of digits in repeating cycle
    
    ", num);
                //fprintf(fp,"...)
       %d = number of digits in repeating cycle
    
    ", num);
                else
                    printf(")
       %d = number of digits in repeating cycle
    
    ", num);
                    //fprintf(fp,")
       %d = number of digits in repeating cycle
    
    ", num);
            }
        }
    //  fclose(fp);
        return 0;
    }

    Uva10340子序列

    #include<cstdio>
    #include<iostream>
    #include<string>
    using namespace std;
    
    int main() {
        string s1, s2;
        while (cin >> s1 >> s2) {
            string::iterator a, b, c;
            a = s1.begin();
            b = c = s2.begin();
            int num = 0;
            while (a != s1.end()) {
                for (c = b; c != s2.end(); c++) {
                    if (*c != *a)
                        continue;
                    else {
                        b = ++c;
                        num++;
                        break;
                    }
                }
                if (c == s2.end())
                    break;
                a++;
            }
            if (num != s1.size())
                printf("No
    ");
            else
                printf("Yes
    ");
        }
        return 0;
    }

    Uva227谜题

    #include<stdio.h>
    #include<iostream>
    #include<string>
    #include<string.h>
    #include<memory.h>
    using namespace std;
    char a[6][6];
    
    bool legal(int nx, int ny) {
        return nx >= 0 && nx < 5 && ny >= 0 && ny < 5;
    }
    
    void swapnum(int x,int y,int nx, int ny) {
        char c = a[x][y];
        a[x][y] = a[nx][ny];
        a[nx][ny] = c;
        return;
    }
    
    int main() {
        int kase = 0;
        //FILE* fp;
        //fp = fopen("stdout.txt", "w");
        while (1) {
            int x, y;
            char tmp;
            tmp = getchar();
            if (tmp == 'Z')
                break;
            if (tmp == ' ') {
                x = 0;
                y = 0;
            }
            a[0][0] = tmp;
            for (int j = 1; j < 5; j++) {
                scanf("%c", &a[0][j]);
            }
            for (int i = 1; i < 5; i++) {
                getchar();
                for (int j = 0; j < 5; j++) {
                    scanf("%c", &a[i][j]);
                }
            }
            for (int i = 0; i < 5; i++) {
                for (int j = 0; j < 5; j++) {
                    if (a[i][j] == ' ')
                        x = i, y = j;
                }
            }
            string cmd;
            char c;
            while (c = getchar()) {
                if (c == '0')
                    break;
                if (c == '
    ')
                    continue;
                else cmd.push_back(c);
            }
            getchar();
            bool flag = true;
            for (string::iterator it = cmd.begin(); it != cmd.end(); it++) {
                if (*it == 'A') {
                    int nx = x - 1;
                    int ny = y;
                    if (legal(nx, ny)) {
                        swapnum(x, y, nx, ny);
                        x = nx;
                        y = ny;
                        continue;
                    }
                    else {
                        flag = false;
                        break;
                    }
                }
                else if (*it == 'B') {
                    int nx = x + 1;
                    int ny = y;
                    if (legal(nx, ny)) {
                        swapnum(x, y, nx, ny);
                        x = nx;
                        y = ny;
                        continue;
                    }
                    else {
                        flag = false;
                        break;
                    }
                }
                else if (*it == 'L') {
                    int nx = x;
                    int ny = y - 1;
                    if (legal(nx, ny)) {
                        swapnum(x, y, nx, ny);
                        x = nx;
                        y = ny;
                        continue;
                    }
                    else {
                        flag = false;
                        break;
                    }
                }
                else {
                    int nx = x;
                    int ny = y + 1;
                    if (legal(nx, ny)) {
                        swapnum(x, y, nx, ny);
                        x = nx;
                        y = ny;
                        continue;
                    }
                    else {
                        flag = false;
                        break;
                    }
                }
            }
            if (kase)
                //fprintf(fp, "
    ");
                printf("
    ");
            //fprintf(fp,"Puzzle #%d:
    ", ++kase);
            printf("Puzzle #%d:
    ", ++kase);
            if (flag) {
                for (int i = 0; i < 5; i++) {
                    //fprintf(fp, "%c %c %c %c %c
    ", a[i][0], a[i][1], a[i][2], a[i][3], a[i][4]);
                    printf("%c %c %c %c %c
    ", a[i][0], a[i][1], a[i][2], a[i][3], a[i][4]);
                }
            }
            else {
                //fprintf(fp,"This puzzle has no final configuration.
    ");
                printf("This puzzle has no final configuration.
    ");
            }
            memset(a, ' ', sizeof(a));
            //fprintf(fp, "
    ");
        }
        //fclose(fp);
        return 0;
    }
    
    
    

    Uva1587盒子

    #include<cstdio>
    #include<iostream>
    #include<set>
    #include<utility>
    using namespace std;
    typedef pair<int, int> P;
    
    bool unsame(P p) {
        return p.first != p.second;
    }
    
    int main() {
        multiset<P>store;
        set<int>s;
        P tar;
        int num = 0;
        //FILE*fp;
        //fp = fopen("stdout", "w");
        while (scanf("%d%d", &tar.first, &tar.second) != EOF) {
            if (tar.first > tar.second) {
                P mid(tar.second, tar.first);
                store.insert(mid);
            }
            else
                store.insert(tar);
            num++;
            if (num == 6) {
                int kind = 1;
                set<P>::iterator it ,itt,ittt;
                it = itt = ittt = store.begin();
                itt++;
                while (itt != store.end()) {
                    if (*it == *itt) {
                        itt++;
                    }
                    else {
                        kind++;
                        it = itt;
                        itt++;
                    }
                }
                bool flag;
                if (kind == 1) {
                    flag = true;
                }
                else if (kind == 2) {
                    int dis = 0;
                    it = itt = ittt = store.begin();
                    while (itt != store.end()) {
                        if (*itt == *it) {
                            itt++;
                            continue;
                        }
                        else {
                            if ((unsame(*itt) && unsame(*it)) || ((*it).second != (*itt).first)) {
                                flag = false;
                                break;
                            }
                            else {
                                if (!unsame(*it))
                                    dis = 6 - distance(it, itt);
                                else
                                    dis = distance(it, itt);
                                if (dis == 3 || dis == 4)
                                    flag = true;
                                else
                                    flag = false;
                                break;
                            }
                        }
                    }
                }
                else if (kind == 3) {
                    it = itt = ittt = store.begin();
                    do {
                        itt++;
                    } while (*it == *itt);
                    ittt = itt;
                    do {
                        ittt++;
                    } while (*itt == *ittt);
                    if (distance(it, itt) == 2 && distance(itt, ittt) == 2) {
                        if ((*it).first == (*itt).first && (*it).second == (*ittt).first && (*itt).second == (*ittt).second) 
                            flag = true;
                        else
                            flag = false;
                    }
                    else
                        flag = false;
                }
                else
                    flag = false;
                if (flag)
                    printf("POSSIBLE
    ");
                    //fprintf(fp, "POSSIBLE
    ");
    
                else
                    printf("IMPOSSIBLE
    ");
                    //fprintf(fp, "IMPOSSIBLE
    ");
                store.clear();
                num = 0;
            }
        }
        //fclose(fp);
        return 0;
    }
    
    

    琬若1

    #include<cstdio>
    #include<iostream>
    using namespace std;
    
    int main() {
        int tar[1000];
        int n, mid;
        for (int i = 0; i < 20; i++) {
            scanf("%d", &tar[i]);
        }
        for (int i = 0; i < 20; i++) {
            tar[i] ^= 31337;
            mid = tar[i];
            printf("%c%c", mid >> 8, tar[i]);
        }
        return 0;
    }

    琬若2

    #include<cstdio>
    #include<iostream>
    #include<time.h>
    using namespace std;
    
    int main() {
        time_t now;
        now = time(NULL);
        int tsecond = now;
        int tminutes = tsecond / 60;
        int minutes = tminutes % 60;
        int thours = tminutes / 60;
        int hours = thours % 24;
        int tday = thours / 24;
        int day = tday - ((2011 - 1970 + 1) * 365 + 69) - 5 * 365;
        int tyear = tday / 365;
        int year = tday % 365;
    
        printf("%d ", tyear + 1970);
        if (day <= 30)
            printf("Mrach. %d", day);
        else if(day <= 61)
            printf("April.%d", day-61);
        //这点不想写了;
        else if(day>5*31+4*30)
            printf("Dec.%d ", day - 5 * 31 - 4 * 30);
        printf("%d:%d", (hours + 8) % 24, minutes);
        return 0;
    }

    Uva1225数数字

    #include<cstdio>
    #include<iostream>
    #include<cstring>
    using namespace std;
    int main() {
        int n, kase = 0;
        scanf("%d", &n);
        string s;
        while (kase++ < n) {
            int last;int tar[10] = {};s.clear();scanf("%d", &last);int mid;for (int i = 1; i <= last; i++) {
                int num = i;while (1) {mid = num % 10;tar[mid]++;if (num / 10 == 0)break;else
                    num /= 10;
            }
            }
            for (int i = 0; i < 10; i++) {
                if (i == 9) {
                    printf("%d
    ", tar[i]);
                    break;
                }
                printf("%d ", tar[i]);
            }
        }
        return 0;
    }

    Uva1586摩尔质量

    #include<cstdio>
    #include<iostream>
    #include<sstream>
    #include<map>
    #include<utility>
    #include<cstring>
    using namespace std;
    
    int main() {
        string s;
        int n, kase = 0;
        map<char, int>store;
        scanf("%d", &n);
        while (kase++ < n) {
            store.insert(make_pair('C', 0));
            store.insert(make_pair('H', 0));
            store.insert(make_pair('N', 0));
            store.insert(make_pair('O', 0));
            cin >> s;
            string ss;
            char c;
            string::iterator it = s.begin(), itt = s.begin();
            while (itt != s.end()) {
                if (isalpha(*it)) {
                    int num = 0;
                    for (itt = it + 1; itt != s.end(); itt++) {
                        if (isdigit(*itt)) {
                            num = num * 10 + (*itt - '0');
                        }
                        else if (itt - it == 1) {
                            num++;
                            break;
                        }
                        else
                            break;
                    }
                    store[*it] += num;
                    if (itt == s.end() && isalpha(*(itt - 1)))
                        store[*it] ++;
                    else {
    
                        it = itt;
                    }
                }
            }
            float ans = store['C'] * 12.01 + store['H'] * 1.008 + store['N'] * 14.01 + store['O'] * 16.00;
            printf("%.3f
    ", ans);
            store.clear();
        }
        return 0;
    }

    Uva1158高低档

    #include<cstdio>
    #include<iostream>
    #include<string.h>
    #include<string>
    #include<cmath>
    #include<algorithm>
    using namespace std;
    bool flag;
    int check(string sl, string ss) {
        int len = 0;
        string::iterator it = sl.begin(), itt = ss.begin();
        while (it != sl.end() && itt != ss.end()) {
            if (*it + *itt <= 'c') {
                it++, itt++;
    
            }
            else {
                itt = ss.begin();
                it = sl.begin() + len;
                len++;
            }
            if (itt == ss.end()) {
                flag = true;
                break;
            }
        }
        return sl.size() - len + 1;
    }
    
    int main() {
        char x1[1000], x2[1000];
        string sl, ss;
    //  FILE* fp;
    //  fp = fopen("stdout.txt", "w");
        while (cin>>sl>>ss) {
            int len;
            flag = false;
            int len1 = check(sl, ss);
            int len2 = check(ss, sl);
            if (flag)
                printf("%d
    ", max(sl.size(), ss.size()));
            else
                printf("%d
    ", sl.size() + ss.size() - max(len1, len2));
        }
        //fclose(fp);
        return 0;
    }

    Uva455周期串

    #include<stdio.h>
    #include<iostream>
    #include<string>
    #include<string.h>
    using namespace std;
    
    int main() {
        string s, none;
        int n, kase = 0;
        scanf("%d", &n);
    
    //  FILE *fp;
    //  fp = fopen("stdout.txt", "w");
        //if (fp == NULL)
        //  cout << 000000;
        bool flagg = true;
        while (kase++ < n) {
            //getline(cin, none);
            cin >> s;
            string ss;
            int len = s.size();
            for (int i = 1; i <= s.size(); i++) {
                ss = s.substr(0, i);
                int cnt = 0;
                bool flag = true;
                if (len%i != 0)
                    continue;
                else {
                    for (int j = 0; j < len; j += i) {
                        if (s.compare(j, i, ss) != 0) {
                            flag = false;
                            break;
                        }
                    }
                    if (flag) {
                        if (flagg) {
                            //fprintf(fp, "%d
    ", i);
                            printf("%d
    ", i);
                            flagg = false;
                            break;
                        }
                        else {
                        //  fprintf(fp, "
    %d
    ", i);
                            printf("
    %d
    ", i);
                            break;
                        }
                        //fprintf(fp,"%d
    
    ", i);
                        //printf("%d
    
    ", i);
                        //break;
                    }
                }
            }
        }
        //fclose(fp);
        return 0;
    }

    Uva1585最高得分

    #include<cstdio>
    #include<iostream>
    #include<string>
    using namespace std;
    
    int main() {
        int t = 0;
        int n;
        scanf("%d", &n);
        while (t++ < n) {
            string s;
            cin >> s;
            int ans = 0, num = 1;
            for (string::iterator it = s.begin(); it != s.end(); it++) {
                if (*it == 'O') {
                    ans += num++;
                }
                else {
                    num = 1;
                    continue;
                }
            }
            cout << ans << endl;
        }
        return 0;
    }

    Uva11809第三张压轴题

    #include<cstdio>
    #include<iostream>
    #include<string>
    using namespace std;
    
    int main() {
        int t = 0;
        int n;
        scanf("%d", &n);
        while (t++ < n) {
            string s;
            cin >> s;
            int ans = 0, num = 1;
            for (string::iterator it = s.begin(); it != s.end(); it++) {
                if (*it == 'O') {
                    ans += num++;
                }
                else {
                    num = 1;
                    continue;
                }
            }
            cout << ans << endl;
        }
        return 0;
    }

    Uva11882最大的数

    #include<cstdio>
    #include<iostream>
    #include<string.h>
    #include<string>
    #include<vector>
    #include<memory.h>
    using namespace std;
    char mp[20][20];
    int x[30], y[30], g[30][30], cnt, q[40], r, c, vis[30], dis[30];
    const int dir[4][2] = { {1,0},{-1,0},{0,1},{0,-1} };
    vector<int>node[30];
    string ans, cur;
    
    bool legal(int x,int y) {
        return x >= 0 && x < r&&y >= 0 && y < c&&mp[x][y] != '#';
    }
    
    int h(int pos) {
        int head = 0, rear = 0, cnt = 0;
        q[rear++] = pos;
        memset(dis, -1, sizeof(dis));
        dis[pos] = 0;
        while (head != rear) {
            int t = q[head++];
            for (int i = 0; i < node[t].size(); i++) {
                if (vis[node[t][i]])
                    continue;
                if (dis[node[t][i]] < 0) {
                    cnt++;
                    q[rear++] = node[t][i];
                    dis[node[t][i]] = dis[t] + 1;
                }
            }
        }
        return cnt + 1;
    }
    
    void dfs(int pos) {
        int l = h(pos);
        if (l + cur.size() < ans.size())
            return;
        if (l + cur.size() == ans.size()) {
            if (cur + "z" < ans)
                return;
        }
        cur += mp[x[pos]][y[pos]];
        if (l == 1) {
            if (cur.size() > ans.size() || cur.size() == ans.size() && cur > ans)
                ans = cur;
        }
        vis[pos] = 1;
        for (int i = 0; i < node[pos].size(); i++) {
            if (!vis[node[pos][i]])
                dfs(node[pos][i]);
        }
        cur.pop_back();
        vis[pos] = 0;
    }
    
    int main() {
        while (scanf("%d %d", &r, &c) == 2 && r) {
            cnt = 0;
            for (int i = 0; i < r; i++) {
                scanf("%s", mp[i]);
                for (int j = 0; j < c; j++) {
                    if (mp[i][j] != '#') {
                        x[cnt] = i;
                        y[cnt] = j;
                        g[i][j] = cnt++;
                    }
                }
            }
            for (int i = 0; i < cnt; i++)
                node[i].clear();
            for (int i = 0; i < cnt; i++) {
                for (int j = 0; j < 4; j++) {
                    int nx = x[i] + dir[j][0];
                    int ny = y[i] + dir[j][1];
                    if (legal(nx, ny))
                        node[i].push_back(g[nx][ny]);
                }
            }
            cur = ans = "";
            for (int i = 0; i < cnt; i++)
                dfs(i);
            cout << ans << endl;
        }
        return 0;
    }

    继续努力!

  • 相关阅读:
    opencvcvRound返回整数值四舍五入
    opencvcvCeil返回不小于参数的最小整数值
    opencvcvFloor返回不大于参数的最大整数值
    华为云构建云原生DevSecOps平台,保障软件供应链全流程安全可信
    云原生数据库白皮书,发布!
    云图说丨初识可信分布式身份服务
    4步教你学会使用LinuxAudit工具
    一文带你认知定时消息发布RocketMQ
    ModelBox开发体验:使用YOLOv3做口罩检测
    10种有用的Linux Bash_Completion 命令示例
  • 原文地址:https://www.cnblogs.com/romaLzhih/p/9489849.html
Copyright © 2020-2023  润新知