CH3-UVA1586

思路

这道题在我看来真是很水, 我的想法就是用一个变量C来记录当前要计算的字符, 用s表示该字符后面字符串的长度.那么要有写特例就是当s == 0的时候并不表示后面的数字长度为0,而是为1.给省略了.针对这个情况再做处理就行.

//
// Created by sixleaves on 2016/11/19.
//
#include 
#include 
#include 
#define MAXSIZE 126
char input[MAXSIZE];


float moral(char ch) {
    float m = 0.0;
    switch (ch) {
        case 'C':
            m = 12.01;
            break;
        case 'H':
            m = 1.008;
            break;
        case 'O':
            m = 16.00;
            break;
        case 'N':
            m = 14.01;
            break;
        default:
            m = 0;
    }
    return m;
}

// read lenghth of len char starting from s
int readInt(char *in, int s, int len) {

    if (0 == len) return 1;

    char *p = in + s;
    int v = 0;
    for (int i = 0; i < len; i++) {
        v = v * 10 + (*p - '0');
        p++;
    }
    return v;
}

int main () {

    int n;

    while (scanf("%d", &n) == 1) {

        for (int i = 0; i < n; i++) {

            memset(input, 0, sizeof(input));
            scanf("%s", input);
            int len = strlen(input);
            int s = 0, c = 0;
            int v = 0;
            float sum  =0.0;
            for (int j = 1; j < len; j++) {

                if (isdigit(input[j])) {

                    s++;

                }else {

                    sum += moral(input[c]) * readInt(input, c + 1, s);
                    s = 0;
                    c = j;
                }
            }

            sum += moral(input[c]) * readInt(input, c + 1, s);
            printf("%.3f\n", sum);

        }

    }

}
package CH3.ArraryAndStrings;

import java.util.Scanner;

/**
 * Created by sixleaves on 2016/11/19.
 */
public class UVA1586 {


    static float moral(char ch) {
        float m = 0.0f;
        switch (ch) {
            case 'C':
                m = 12.01f;
                break;
            case 'H':
                m = 1.008f;
                break;
            case 'O':
                m = 16.00f;
                break;
            case 'N':
                m = 14.01f;
                break;
            default:
                m = 0;
        }
        return m;
    }

    // read lenghth of len char starting from s
    static int readInt(String in, int s, int len) {
        if (0 == len) return 1;
        int index = s;
        int v = 0;
        for (int i = 0; i < len; i++) {
            v = v * 10 + (in.charAt(index) - '0');
            index++;
        }
        return v;
    }

    static public void main(String...args) {

        Scanner cin = new Scanner(System.in);
        int n;
        n = cin.nextInt();
        String nextLine = cin.nextLine();
        for (int i = 0; i < n; i++) {
            String line = cin.nextLine();
            int len = line.length();
            int c = 0, s = 0;
            float sum = 0.0f;
            for (int j = 1; j < len; j++) {

                if (Character.isDigit(line.charAt(j))) {
                    s++;
                }else {
                    sum += moral(line.charAt(c)) * readInt(line, c + 1, s);
                    s = 0;
                    c = j;
                }
            }
            if (line.length() >= 1)
                sum += moral(line.charAt(c)) * readInt(line, c + 1, s);
            System.out.println(String.format("%.3f", sum));

        }

    }
}
API:
  • Java的Scanner再读取Int和String时候的注意事项.再调用nextInt后, 需要调用nextLine先把\n读掉, 此时读出来的是空行, 那是因为这个字符串确实是空的, 以\n直接结尾.如果是在Windows下就是\r\n.之后再使用nextLine读取字符串才能读到正确的值.

The problem is the '\n' character that follows your integer. When you call nextInt, the scanner reads the int, but it does not consume the '\n' character after it; nextLine does that. That is why you get an empty line instead of the string that you were expecting to get.

Let's say your input has the following data:

12345
hello

Here is how the input buffer looks initially (^ represents the position at which the Scanner reads the next piece of data):

1  2  3  4  5 \n h  e  l  l  o \n
^

After nextInt, the buffer looks like this:

1  2  3  4  5 \n h  e  l  l  o \n
              ^

The first nextLine consumes the \n, leaving your buffer like this:

1  2  3  4  5 \n h  e  l  l  o \n
                 ^

Now the nextLine call will produce the expected result. Therefore, to fix your program, all you need is to add another call to nextLine after nextInt, and discard its result:

k = in.nextInt();
in.nextLine(); // Discard '\n'
input = in.nextLine();
  • Java中写的浮点型默认是double, 要加上f表示是float.
  • Scanner和C中的scanf一样, 是阻塞方式的读取.

你可能感兴趣的:(CH3-UVA1586)