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;
}
时间: 2024-10-10 10:47:25

LRJ的相关文章

lrj计算几何模板

整理了一下大白书上的计算几何模板. 1 #include <cstdio> 2 #include <algorithm> 3 #include <cmath> 4 using namespace std; 5 //lrj计算几何模板 6 struct Point 7 { 8 double x, y; 9 Point(double x=0, double y=0) :x(x),y(y) {} 10 }; 11 typedef Point Vector; 12 const

// 62.是否有利润奖--lrj private boolean isProfitsAward; // 63.利润奖--lrj_price private String profitsAward;

// 62.是否有利润奖--lrj private boolean isProfitsAward; // 63.利润奖--lrj_price private String profitsAward; 如果 这二个名称相同(profitsAward,isProfitsAward),报错,getIs,setIs public boolean getIsProfitsAward() { return isProfitsAward; } public void setIsProfitsAward(boo

C++语言学习——LRJ入门经典笔记

1.scanf的输入格式,空格.TAB和回车符都是无关紧要,所以按Enter键并不意味着输入结束. 告诉程序输入结束的方式: 在windows下,输入完毕后先按Enter键,再按Ctrl+Z键,最后再按Enter键. 在linux下,输入完毕后按Ctrl+D键 2.gcc 编译选项 -Wall指出警告 -ansi 判断是否和ANSI冲突 -pedantic 比ansi更加严格 -lm 链接数学库, C++编译器会自动链接 -DDEBUG 编译时定义符号DEBUG,可以换成其它,如-DLOCAL将

lrj紫书第五章

UVA-1592 1 // UVa1592 Database 2 // Rujia Liu 3 // 本程序只是为了演示STL各种用法,效率较低.实践中一般用C字符串和哈希表来实现. 4 5 #include<iostream> 6 #include<cstdio> 7 #include<vector> 8 #include<string> 9 #include<map> 10 #include<sstream> 11 using n

Uva 122 树的层次遍历 Trees on the level lrj白书 p149

是否可以把树上结点的编号,然后把二叉树存储在数组中呢?很遗憾如果结点在一条链上,那将是2^256个结点 所以需要采用动态结构 首先要读取结点,建立二叉树addnode()+read_input()承担这样的工作 然后遍历二叉树,读取结点编号输出bfs() 这道题有内存池应用的背景 附链接  http://blog.csdn.net/shawngucas/article/details/6574863 #include <cstdio> #include <cstring> #inc

【枚举】【lrj黑书】奇怪的问题(古老的智力题)

题目描述: 请回答下面的 10 个问题,你的回答应保证每题惟有你的选择是正确的. ⑴ 第一个答案是b 的问题是哪一个?(a )2 ( b ) 3 ( c ) 4 ( d ) 5 ( e ) 6⑵ 恰好有两个连续问题的答案是一样的,它们是:( a ) 2 , 3 ( b ) 3, 4 ( c ) 4 ,5 ( d ) 5 ,6 ( e ) 6 ,7 ⑶ 本问题答案和哪一个问题的答案相同?( a ) 1 ( b ) 2 ( c ) 4 ( d ) 7 ( e ) 6⑷ 答案是a 的问题的个数是:(

lrj 9.4.1 最长上升子序列 LIS

p275 d(i)是以Ai为结尾的最长上升子序列的长度 <算法竞赛入门经典-训练指南>p62 问题6 提供了一种优化到 O(nlogn)的方法. 在O(nlogn)的算法分析中(从“假设已经计算出的两个状态...”开始), 用g(i)表示d值为i的最小状态编号,状态编号就是数组下标 g(1) <= g(2) <= g(3) <= ... <= g(n) 可以用反证法: 假设 i < j, g(i) > g(j) g(j)代表 d(x) = j 的最小 x,对

lrj 9.2.3

int INF = -(1<<30); // 记忆化搜索 min[0] = 0; int dp(int i) { if(maxv[i] != -1) return maxv[i]; maxv[i] = -INF; for(int j = 1; j <= n; j++) if(i >= V[j]) maxv[i] = max(maxv[i], dp(i-V[j]) + 1); return maxv[i]; } printf("%d\n", dp[S]); //

LRJ 3-7

#define _CRT_SECURE_NO_WARNINGS #include <cstdio> int main() { int T; int m, n; char dna[55][1005]; // 4 <= m <= 50, 4 <= n <= 1000 scanf("%d", &T); while (T--) { scanf("%d%d", &m, &n); for (int i = 0; i &