标签:代码 技术 tin += 不同 exti als oid out


首先阅读题目,看到关键词围成一个圆,就能想到约瑟夫问题的环问题,题目是要求我们统计在蔬果以后所有果树的的果子总量sum,苹果树在蔬果中掉落果子的棵树D(一棵树可能多次掉落果子,但是仍然仅仅记录为一棵),每连续三棵树树掉落的情况E
import java.util.Scanner;
public class Main_04 {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
int N = input.nextInt();
int [][]nums = new int[N][2];///数组第一个存储果子量,第二个存是否掉落
int i,j,h,temp,applenum = 0,D = 0;
for (i = 0;i < N;i++ ) {
boolean diao = false;
h = input.nextInt();
for (j = 0,applenum = 0;j < h ;j++) {
temp = input.nextInt();
if (temp>0 && applenum == 0)applenum = temp;//初始更新果子量
else if (temp > 0 && temp != applenum) {//如果与后面的果子正数不相等
diao = true;//掉果开关
//D++;
applenum = temp;//更新果子量
}
else if (temp < 0)applenum += temp;
}
nums[i][0] = applenum;
if(diao) {
nums[i][1] = 1;
D++;///则为掉果
}
}
int sum = 0;
int E = 0;
for (i = 0;i < N;i++) {
int f = i + 1;
int k = i + 2;
if (nums[i][1] == 1 && nums[k%N][1] == 1 && nums[f%N][1] == 1)E++;
sum += nums[i][0];
}
System.out.print(sum+" "+D+" "+E);
//求解sum
}
}
错误:第一次做出来的时候只有50分,经过和别人的代码比对,发现错误在于审题不清:原程序写的是当出现输入数中有正数且与原applenum中数不同时,即为掉果,统计成掉果的次数,而非掉果的苹果树棵树。
总结:当讨论到一圈,轮回时候,完全可以使用取余运算实现对于棵树的跳转。
标签:代码 技术 tin += 不同 exti als oid out
原文地址:https://www.cnblogs.com/SGRS/p/12257170.html