2 qq 34051382 qq_34051382 于 2016.03.19 10:35 提问

   j = c[i] - 'a'; 是什么意思?

import java.io.*;
 
public class Main {
    public static void main(String args[]) throws IOException {
        BufferedReader buf = new BufferedReader(
                new InputStreamReader(System.in));
        int n = Integer.parseInt(buf.readLine());
        String s = buf.readLine();
        char c[] = s.toCharArray();
        int count = 0, num = 0;
        char ch = '0';
        int i, k = 0, j;
        int b[] = new int[26];
        for (i = 0; i < n; i++)
            for (i = 0; i < n; i++) {
                j = c[i] - 'a';
                b[j]++;
            }
        for (j = 0; j < 26; j++) {
            if (b[j] % 2 != 0) {
                k++;
                ch = (char) (j + 'a');
            }
        }
        if (k >= 2)
            System.out.println("Impossible");
        else
            System.out.println(changes(c, ch, n));
    }
 
    public static int changes(char s[], char x, int n) {
        int i, change = 0, j, k;
        for (i = 0; i < n / 2; i++) {
            if (s[i] == x) {
                for (j = i; j < n - i - 1; j++)
                    if (s[n - i - 1] == s[j])
                        break;
                change += j - i;
                for (k = j; k > i; k--)
                    s[k] = s[k - 1];
                s[i] = s[n - i - 1];
            } else {
                for (j = n - i - 1; j >= i; j--)
                    if (s[i] == s[j])
                        break;
                change += n - i - 1 - j;
                for (k = j; k < n - i - 1; k++)
                    s[k] = s[k + 1];
                s[n - i - 1] = s[i];
            }
        }
        return change;
    }
}

3个回答

fickyou
fickyou   2016.03.19 10:42
已采纳

j = c[i] - 'a'就是计算c[i]和‘a’字符间的差值。

稍微看了一下,该函数首先是统计c[n]数组中二十六个英文字母的格式,然后安装字母顺序存放到b[26]数组中。

ftlisdcr
ftlisdcr   2016.03.19 10:49

ascii码
计算字符c[i]与字符‘a’之间ascii码的差值~~~~

xionglangs
xionglangs   Rxr 2016.03.20 09:55

解决你这个问题就要了解一句话对java来说char如果用"+"、"-"、"*"、"/" 运算符连接的化它都会转换成int数字,至于他的值就是他的ascii码。

Csdn user default icon
上传中...
上传图片
插入图片
准确详细的回答,更有利于被提问者采纳,从而获得C币。复制、灌水、广告等回答会被删除,是时候展现真正的技术了!