import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
//a=1,b=2,B=aa,aaa=3*3*1,aa=2*2*1,计算权值
public class Baidu_1 {
public static void main(String args[]){
Scanner sc = new Scanner(System.in);
List<String> list = new ArrayList<String>();
int k = sc.nextInt();
for(int i=0;i<k;i++){
list.add(sc.next());
}
Baidu_1.fun(list);
}
public static void fun(List<String> list){
for(int i=0;i<list.size();i++){
String str =list.get(i);
int count=0;//计算连续相等个数
int sum=0;
//因为数组下标会超界,循环到倒数第二位,最后一位单独列出。
for(int j=0;j<str.length()-1;j++){
//判断当前字符是大写小写,并与后面一位比较是否相同,相同继续累加count
if(str.charAt(j)>=97&&str.charAt(j)==str.charAt(j+1)){
count++;
continue;
}else if(str.charAt(j)>=97&&str.charAt(j)==str.charAt(j+1)+32){
count++;
continue;
}else if(str.charAt(j)<97&&str.charAt(j)==str.charAt(j+1)){
count+=2;
continue;
}else if(str.charAt(j)<97&&str.charAt(j)==str.charAt(j+1)-32){
count+=2;
continue;
//不同的话,就计算sum值,count更新,注意,当前位置的大小写判断
}else if(str.charAt(j)>=97){
count++;
sum=sum+(str.charAt(j)-96)*count*count;
count=0;
}else{
count+=2;
sum=sum+(str.charAt(j)-64)*count*count;
count=0;
}
}
//最后一位计算sum值,不需要在判断和前面的是否连续(即count值),因为上面已经判断
//count已经代表了
if(str.charAt(str.length()-1)>=97){
count++;
sum = sum+(str.charAt(str.length()-1)-96)*count*count;
}else if(str.charAt(str.length()-1)<97){
count+=2;
sum = sum+(str.charAt(str.length()-1)-64)*count*count;
}
System.out.println(sum);
}
}
}
2.
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
public class Baidu_2 {
<span style="white-space:pre"> </span>public static void main(String[] args){
<span style="white-space:pre"> </span>Scanner sc = new Scanner(System.in);
<span style="white-space:pre"> </span>int k =sc.nextInt();
<span style="white-space:pre"> </span>for(int i=0;i<k;i++){
<span style="white-space:pre"> </span>int count =sc.nextInt();
<span style="white-space:pre"> </span>List<Integer> list = new ArrayList<Integer>();
<span style="white-space:pre"> </span>for(int j=0;j<count;j++){
<span style="white-space:pre"> </span>list.add(sc.nextInt());
<span style="white-space:pre"> </span>}
<span style="white-space:pre"> </span>
<span style="white-space:pre"> </span>Baidu_2.fun(list);
<span style="white-space:pre"> </span>}
<span style="white-space:pre"> </span>}
<span style="white-space:pre"> </span>
<span style="white-space:pre"> </span>public static void fun(List<Integer> list){
<span style="white-space:pre"> </span>int sum = 0;
<span style="white-space:pre"> </span>//第一个个山峰和最后一个山峰单独计算
<span style="white-space:pre"> </span>sum =list.get(0)*2;
<span style="white-space:pre"> </span>for(int i=1;i<list.size();i++){
<span style="white-space:pre"> </span>sum+=Math.abs(list.get(i)-list.get(i-1))*2;
<span style="white-space:pre"> </span>}
<span style="white-space:pre"> </span>sum+=list.get(list.size()-1)*2;
<span style="white-space:pre"> </span>
<span style="white-space:pre"> </span>System.out.println(sum);
<span style="white-space:pre"> </span>}
}