两道机试题



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>}


}


  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值