日期:2025年2月17日
ZeroJudge 題目連結:e970. 1. 粉專抽獎 (Lucky Draw)
解題想法
按照題目敘述的規則計算就好。
Python 程式碼
使用時間約為 24 ms,記憶體約為 4.3 MB,通過測試。
n = int(input()) # 留言量 n
arr = [0] + list(map(int, input().split())) # n 個隨機數字
b = arr[-1] # arr 最後一項是基數 b
tot = 0 # 索引值對應到之隨機數字加總
for i in range(1, n+1): # 依序測試 1 ~ n
if i%b == 1: tot += arr[i] # 如果 i 除以 b 的餘數等於 1,將 arr[i] 加到 tot
m = tot%n # 中獎留言索引值
if m == 0: m = n # 如果 m 等於 0,最後一則留言中獎,m 改為 n
print(f"{m:d} {arr[m]:d}") # 印出 m 及 arr[m]
C++ 程式碼
使用時間約為 3 ms,記憶體約為 376 kB,通過測試。
#include <iostream>
using namespace std;
int main() {
ios::sync_with_stdio(0); cin.tie(0);
int n; cin >> n; // 留言量 n
int arr[n+1] = {0}; // n 個隨機數字,為了配合題目的索引值,前面多一個 0
for(int i=1; i<=n; i++) cin >> arr[i];
int b = arr[n], tot = 0; // arr 最後一項是基數 b,索引值對應到之隨機數字加總
for(int i=1; i<=n; i++) { // 依序測試 1 ~ n
if (i%b == 1) tot += arr[i]; // 如果 i 除以 b 的餘數等於 1,將 arr[i] 加到 tot
}
int m = tot%n; // 中獎留言索引值
if (m == 0) m = n; // 如果 m 等於 0,最後一則留言中獎,m 改為 n
cout << m << " " << arr[m] << "\n"; // 印出 m 及 arr[m]
return 0;
}
沒有留言:
張貼留言