Lanqiao Cup official website fill-in-the-blank question (sum of distances)

Question description

This question is a fill-in-the-blank question. You only need to calculate the result and use the output statement in the code to output the filled-in result.

The distance between two letters is defined as the distance between their positions in the alphabet. For example, the distance between A and C is 2, and the distance between L and Q is 5.

For a string, we call the sum of the distances between two characters in the string the internal distance of the string.

For example: ZOO the inner distance of is 22, where the distance between Z and O is 11.

Excuse me, LANQIAO what is the internal distance?

operating restrictions

  • Maximum running time: 1s
  • Maximum running memory: 128M
import java.util.*;

public class Main {
    public static void main(String[] args) {
        int sum=0;
        String str="LANQIAO";
        for(int i=0;i<str.length();i++){
          for(int j=i+1;j<str.length();j++){
            sum=sum+Math.abs((int)(str.charAt(j)-'A')-(int)(str.charAt(i)-'A'));
          }
        }
        System.out.println(sum);
    }
}

Guess you like

Origin blog.csdn.net/s44Sc21/article/details/132780170