ipc_11_快乐 happy

Sample Input 1

9 4
2 3 1 1 4 1 2 4 1
5 3 6 6

Sample Output 1

15

多个数组遍历解决:

package kaoshi;

import java.util.Scanner;

public class Happy {

    public static void main(String[] args) {
        int n,m,happy_value=0;
        int zg,pre = 0;
        Scanner sc= new Scanner(System.in);
        n=sc.nextInt();
        m=sc.nextInt();
        
        int[] a= new int[9];
        int[] b= new int[4];
        int[] c= new int[4];
        for(int i=0;i<n;i++) {
            a[i]=sc.nextInt();
        }
        for(int i=0;i<m;i++) {
            b[i]=sc.nextInt();
        }
        for(int i=0;i<n;i++) {
            if(i<n-1) {
                if(a[i]!=a[i+1]&&c[a[i]-1]!=-1) {
                    happy_value+=b[a[i]-1];
                    c[a[i]-1]=-1;
                    
                }else if(a[i]==a[i+1]||c[a[i]-1]!=-1) {
                    c[a[i]-1]=-1;
                }
                
            }
            if(i>=n-1) {
                if(a[i-1]!=a[i]&&c[a[i]-1]!=-1) {
                    happy_value+=b[a[i]-1];
                    c[a[i]-1]=-1;
                    
                }else if(a[i-1]==a[i]||c[a[i]-1]!=-1) {
                    c[a[i]-1]=-1;
                }
            }
            
        }
        System.out.println(happy_value);
        
        
    }

}

 

posted @ 2020-11-06 20:18  凝气强者  阅读(89)  评论(0编辑  收藏  举报