9.20腾讯笔试(第二题,100%)
//房子涂色,相邻的房子不能同色,求代价最小的涂法 import java.util.Scanner; /** * Created by lenovo on 2019/9/20. */ public class two { public static void main(String[] args) { Scanner read = new Scanner(System.in); int n = read.nextInt(); int[] cost = new int[3]; int i = 0; int[] now = new int[3]; while(i < n){ if(i == 0){ cost[0] = read.nextInt(); cost[1] = read.nextInt(); cost[2] = read.nextInt(); }else{ now[0] = read.nextInt();//r now[1] = read.nextInt();//g now[2] = read.nextInt();//b now[0] = Math.min(now[0] + cost[1], now[0] + cost[2]); now[1] = Math.min(now[1] + cost[0], now[1] + cost[2]); now[2] = Math.min(now[2] + cost[0], now[2] + cost[1]); cost[0] = now[0]; cost[1] = now[1]; cost[2] = now[2]; } i++; } int min; min = cost[0] < cost[1] ? cost[0] : cost[1]; min = min < cost[2] ? min : cost[2]; System.out.println(min); } }
#腾讯##笔试题目#