程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> 更多編程語言 >> 編程解疑 >> java-   j = c[i] - a是什麼意思?

java-   j = c[i] - a是什麼意思?

編輯:編程解疑
   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;
    }
}

最佳回答:


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

稍微看了一下,該函數首先是統計c[n]數組中二十六個英文字母的格式,然後安裝字母順序存放到b[26]數組中。

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved