奇怪的程式碼增加了
阿新 • • 發佈:2020-10-21
小編原以為自己可以拿到50分的好成績,結果只拿到了30分,小編當時很疑惑,考後看了很久也沒看出來錯在了哪裡,你看出來了嗎?
} else { int last = 0, sum = 0; for (register int i = 1; i <= m; i++) { k[i] = read(), v[i] = read(); if (k[i] == 1) { last = i; sum = 0; } else { sum += v[i]; } } int C, K, V; for (register int i = 1; i <= q; i++) { C = read(), K = read(), V = read(); if (C < last) {///在左無影響 k[C] = K, v[C] = V; printf ("%d\n", sum); } else if (C == last && K == '1') {//不變 printf ("%d\n", sum); } else if (C > last && K == '0') {//在右加答案 sum += (V - v[C]); k[C] = 0, v[C] = V; printf ("%d\n", sum); } else if (C == last && K == '0') {//換切點 k[C] = 0, v[C] = V; sum += V; for (register int j = last - 1; j >= 1; j--) { if (k[j] == '1') { last = j; break; } else { sum += v[j]; } } printf ("%d\n", sum); } else if (C > last && K == '1') {// k[C] = 1, v[C] = V; for (register int j = m; j >= 1; j--) { if (k[j] == '1') { last = j; break; } else { sum += v[j]; } } printf ("%d\n", sum); } } }
答案揭曉:
} else { int last = 0, sum = 0; for (register int i = 1; i <= m; i++) { k[i] = read(), v[i] = read(); if (k[i] == 1) { last = i; sum = 0; } else { sum += v[i]; } } int C, K, V; for (register int i = 1; i <= q; i++) { C = read(), K = read(), V = read(); if (C < last) {///在左無影響 k[C] = K, v[C] = V; printf ("%d\n", sum); } else if (C == last && K == 1) {//不變 printf ("%d\n", sum); } else if (C > last && K == 0) {//在右加答案 sum += (V - v[C]); k[C] = 0, v[C] = V; printf ("%d\n", sum); } else if (C == last && K == 0) {//換切點 k[C] = 0, v[C] = V; sum += V; for (register int j = last - 1; j >= 1; j--) { if (k[j] == 1) { last = j; break; } else { sum += v[j]; } } printf ("%d\n", sum); } else if (C > last && K == 1) {// sum = 0; k[C] = 1, v[C] = V; for (register int j = m; j >= 1; j--) { if (k[j] == 1) { last = j; break; } else { sum += v[j]; } } printf ("%d\n", sum); } } }
學到了嗎? 如果學會了請把學廢了打在評論區裡23333