解题思路:
注意事项:
这个是实时影响的,前一个值会影响后一个值,所以只需要一个数组就够了
参考代码:
import java.sql.SQLOutput; import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); int[] candies = new int[5]; int[] result = new int[5]; for (int i = 0; i < 5; i++) { candies[i] = scanner.nextInt(); } for (int i = 0; i < 5; i++) { int share = candies[i] / 3; candies[i] = share; candies[(i + 1) % candies.length] += share; candies[(i + candies.length - 1) % candies.length] += share; // if (i == 0 || i == candies.length - 1){ // candies[candies.length - 1 - i] += share; // if (i == 0){ // candies[1] += share; // }else { // candies[candies.length - 2] += share; // } // } else { // candies[i+1] += share; // candies[i-1] += share; // } } for (int candy : candies) { System.out.printf("%5d", candy); } } }
0.0分
0 人评分