LRJ

//3-1

 1 #define _CRT_SECURE_NO_WARNINGS 
 2 
 3 #include <cstdio>
 4 
 5 int main()
 6 {
 7     int T;
 8     char score[85];
 9     scanf("%d", &T);
10     while (T-- > 0) {
11         scanf("%s", score);
12         int sum = 0;
13         int num = 1;
14         char prev = 'X';
15         for (int i = 0; score[i] != '\0'; i++) {
16             if (score[i] == 'O'){
17                 if (prev == 'O'){
18                     num++;
19                 }
20                 else {
21                     num = 1;
22                 }
23                 sum += num;
24                 prev = 'O';
25             }
26             else {
27                 prev = 'X';
28             }
29         }
30         printf("%d\n", sum);
31     }
32 
33     return 0;
34 }


3-2

#define _CRT_SECURE_NO_WARNINGS 

#include <cstdio>

bool isNum(char c){
    return c >= '1' && c <= '9';
}

double mass(char m){
    switch (m){
    case 'C': return 12.01;
    case 'H': return 1.008;
    case 'O': return 16.00;
    case 'N': return 14.01;
    default: return 0.0;
    }
}

int charToInt(char c) {
    if (c >= '0' && c <= '9')
        return c - '0';
    else
        return 0;
}

int main()
{
    int T;
    char molar[85];
    scanf("%d", &T);
    while (T-- > 0) {
        scanf("%s", molar);
        double sum = 0;
        bool prevIsNum = false;
        char m = 'X'; // m is the previous molecular (C, H, O, N), 'X' is the previous for the first molecular, mass('X') == 0
        for (int i = 0; molar[i] != '\0'; i++) {
            if (isNum(molar[i])){
                if (prevIsNum){
                    char hiDigit = molar[i - 1];
                    char loDigit = molar[i];
                    sum += mass(m) * (charToInt(hiDigit) * 10 + charToInt(loDigit));
                }
                else {

                }
                prevIsNum = true;
            }
            else { // C, H, O, N
                if (prevIsNum){
                    // if previous two letters are numbers, the mass is caculated elsewhere
                    if (!isNum(molar[i - 2])){
                        char loDigit = molar[i - 1];
                        sum += mass(m) * charToInt(loDigit);
                    }
                }
                else{
                    // add previous m
                    sum += mass(m);
                }
                m = molar[i];
                prevIsNum = false;
            }
        }

        // last letter is C/H/O/N
        if (!prevIsNum)
            sum += mass(m);

        printf("%.3f\n", sum);
    }

    return 0;
}

 

3-3

#define _CRT_SECURE_NO_WARNINGS 

#include <cstdio>

int main()
{
    int T;
    int n;
    scanf("%d", &T);
    while (T-- > 0) {
        scanf("%d", &n);
        int counts[10] = { 0 };
        for (int i = 1; i <= n; i++){
            int j = i;
            while (j > 0) {
                counts[j % 10]++;
                j /= 10;
            }
        }

        for (int i = 0; i < 10; i++){
            printf("%d", counts[i]);
            if (i == 9)
                printf("\n");
            else
                printf(" ");
        }
    }

    return 0;
}

 

3-4

#define _CRT_SECURE_NO_WARNINGS 

#include <cstdio>

bool sameStr(char *s1, char *s2, int len)
{
    for (int i = 0; i < len; i++)
        if (s1[i] != s2[i])
            return false;

    return true;
}

int main()
{
    int T;
    char s[85];
    scanf("%d", &T);
    while (T-- > 0) {
        scanf("%s", s);

        if (s[0] != '\0'){
            char first = s[0];
            
            // find a equal char
            int start = 1; // start point for searching
            while (true) {
                int idx = start;
                for (; s[idx] != '\0'; idx++){
                    if (first == s[idx])
                        break;
                }

                if (s[idx] != '\0'){
                    // compare s[0..idx-1] with s[idx..] of same length = idx
                    if (sameStr(s, s + idx, idx)){
                        printf("%d\n\n", idx);
                        break;
                    }

                    start = idx + 1;
                }
                else{ // not found
                    break;
                }
            }
        }
        else { // empty string

        }

    }

    return 0;
}

 

3-5

#define _CRT_SECURE_NO_WARNINGS 

#include <cstdio>

int main()
{
    int T = 1;
    char puzzle[5][5];
    char newline;
    int empty_row, empty_col;

    while (true) {

        for (int i = 0; i < 5; i++) {
            for (int j = 0; j < 5; j++) {
                scanf("%c", &puzzle[i][j]);
                if (i == 0 && j == 0 && puzzle[0][0] == 'Z'){
                    // end of test cases
                    return 0;
                }
                if (puzzle[i][j] == ' '){
                    empty_row = i;
                    empty_col = j;
                }

            }
            scanf("%c", &newline); // swallow the new line
        }

        // process moves
        char m;
        bool error = false;
        while (true){
            scanf("%c", &m);
            if (m == '0') break;
            switch (m){
            case 'A':
                if (empty_row == 0){
                    error = true;
                    break;
                }
                // swap with above
                puzzle[empty_row][empty_col] = puzzle[empty_row - 1][empty_col];
                puzzle[empty_row - 1][empty_col] = ' ';
                empty_row--;
                break;
            case 'B':
                if (empty_row == 4){
                    error = true;
                    break;
                }
                // swap with bottom
                puzzle[empty_row][empty_col] = puzzle[empty_row + 1][empty_col];
                puzzle[empty_row + 1][empty_col] = ' ';
                empty_row++;
                break;
            case 'L':
                if (empty_col == 0){
                    error = true;
                    break;
                }
                // swap with left
                puzzle[empty_row][empty_col] = puzzle[empty_row][empty_col - 1];
                puzzle[empty_row][empty_col - 1] = ' ';
                empty_col--;
                break;
            case 'R':
                if (empty_col == 4){
                    error = true;
                    break;
                }
                // swap with above
                puzzle[empty_row][empty_col] = puzzle[empty_row][empty_col + 1];
                puzzle[empty_row][empty_col + 1] = ' ';
                empty_col++;
                break;
            }
        }
        scanf("%c", &newline); // swallow the new line


        printf("Puzzle #%d:\n", T++);
        if (error){
            printf("This puzzle has no final configuration.\n");
        }
        else {
            for (int i = 0; i < 5; i++) {
                for (int j = 0; j < 5; j++) {
                    printf("%c", puzzle[i][j]);
                    if (j != 4) printf(" ");
                }
                printf("\n");
            }
        }
        printf("\n");
    }

}

 

3-6

#define _CRT_SECURE_NO_WARNINGS 

#include <cstdio>

struct grid{
    char c;
    int n;
};

struct grid puzzle[10][10];

int main()
{
    int r, c;
    char newline;
    int T = 1;
    while (true) {
        scanf("%d", &r); if (r == 0) break;
        scanf("%d", &c);
        scanf("%c", &newline); // swallow the new line

        int num = 1;
        for (int row = 0; row < r; row++){
            for (int col = 0; col < c; col++) {
                scanf("%c", &puzzle[row][col].c);
                if ((puzzle[row][col].c != '*') && // white
                    (row == 0 || col == 0 || puzzle[row - 1][col].c == '*' || puzzle[row][col - 1].c == '*')){ // eligible
                    puzzle[row][col].n = num++;
                }
                else if (puzzle[row][col].c == '*') { // black
                    puzzle[row][col].n = -1;
                }
                else { // illegible white
                    puzzle[row][col].n = 0;
                }
            }
            scanf("%c", &newline); // swallow the new line
        }

        printf("puzzle #%d:\n", T++);

        // Across words
        printf("Across\n");
        for (int row = 0; row < r; row++){
            int col = 0;
            while (col < c) {
                while (col < c && puzzle[row][col].n < 0) { // skip black
                    col++;
                }
                if (col < c) {
                    printf("%d.", puzzle[row][col].n);
                }
                while (col < c && puzzle[row][col].n >= 0) { // eligible and illegible white
                    printf("%c", puzzle[row][col].c);
                    col++;
                }
                printf("\n");
                while (col < c && puzzle[row][col].n < 0) { // skip black
                    col++;
                }
            }
            
        }

        // Down words
        printf("Down\n");
        for (int row = 0; row < r; row++){
            for (int col = 0; col < c; col++) {
                if ((puzzle[row][col].c != '*') &&
                    (row == 0 || puzzle[row - 1][col].c == '*')) {
                    printf("%d.", puzzle[row][col].n);
                    for (int dr = row; dr < r && puzzle[dr][col].c != '*'; dr++){
                        printf("%c", puzzle[dr][col].c);
                    }
                    printf("\n");
                }
            }
        }

        printf("\n"); // Separate output for successive input puzzles by a blank line.

        /*
        for (int row = 0; row < r; row++) {
            for (int col = 0; col < c; col++) {
                printf("%c ", puzzle[row][col].c);
            }
            printf("\n");
        }

        for (int row = 0; row < r; row++) {
            for (int col = 0; col < c; col++) {
                printf("%d ", puzzle[row][col].n);
            }
            printf("\n");
        }
        */
    }

    return 0;
}

 

posted @ 2016-07-16 13:32  PatrickZhou  阅读(337)  评论(0编辑  收藏  举报