發表文章

目前顯示的是有「陣列」標籤的文章

e541. 10474 - Where is the marble

e541. 10474 - Where is the marble |  題目連結 :  https://zerojudge.tw/ShowProblem?problemid=e541 |  解題思路 : 用lower_bound尋找大於或等於key的第一個位置 |  程式碼 :  /* e541. 10474 - Where is the marble https://zerojudge.tw/ShowProblem?problemid=e541 skyblue AC (2ms, 72KB) */ #include <stdio.h> #include <algorithm> using namespace std; int main(){ int n,q,kase = 0; while(scanf("%d%d", &n,&q) == 2 && n!=0){ printf("CASE# %d:\n", ++kase); //read data int a[1000] {0}; for(int i = 0; i<n; i++){ scanf("%d", &a[i]); } //sort data sort(a,a+n); while(q--){ int x; scanf("%d", &x); int p = lower_bound(a,a+n,x)-a; if(a[p] == x) printf("%d found at %d\n", x, p+1); else printf("%d not found\n", x); } } return 0; } /* 4 1 2 3 5 1 5 5 2 1 3 3 3 1 2 3 0 0 ...

f605. 1. 購買力

f605. 1. 購買力 |  題目連結 :  https://zerojudge.tw/ShowProblem?problemid=f605 |  解題思路 : 用min,max函式找出最大最小值,方便判斷相差多少 |  程式碼 :  /* f605. 1. 購買力 https://zerojudge.tw/ShowProblem?problemid=f605 skyblue AC (2ms, 348KB) */ #include <bits/stdc++.h> using namespace std; int main(){ int n, d; scanf("%d%d", &n, &d); int a,b,c; //times記錄物件數(買了多少東西) int times = 0; //total記錄總價錢 int total = 0; while(n--){ scanf("%d%d%d", &a,&b,&c); //用min,max函式找出最大最小值 int mi = min({a,b,c}); int ma = max({a,b,c}); //若最大最小值相差超過d,物件數+1,金額加三者的平均數 if(ma - mi >= d){ times ++; total += (a+b+c)/3; } } //輸出物件數及總價格 printf("%d %d", times,total); return 0; } /* 範例輸入 #1 1 3 24 27 21 範例輸出 #1 1 24 範例輸入 #2 3 4 24 33 42 51 48 60 77 77 77 範例輸出 #2 2 86 */

f071. 2. 刮刮樂 (Lottery)

f071. 2. 刮刮樂 (Lottery) |  題目連結 :  https://zerojudge.tw/ShowProblem?problemid=f071 |  解題思路 : (如程式中註解) |  注意 : 如範例測資3,27、36皆出現2次,故贏得金額為500+500+500+500 = 2000 |  程式碼 :  /* f071. 2. 刮刮樂 (Lottery) https://zerojudge.tw/ShowProblem?problemid=f071 skyblue AC (2ms, 348KB) */ #include <bits/stdc++.h> using namespace std; int main(){ //開3個陣列,分別記錄[幸運數字]、[號碼區的號碼]、[對應金額] int a[3]; int b[5]; int c[5]; //mon 為贏得的金額 int mon = 0; //分別輸入值 for(int i = 0; i<3; i++){ scanf("%d", &a[i]); } for(int i = 0; i<5; i++){ scanf("%d", &b[i]); } for(int i = 0; i<5; i++){ scanf("%d", &c[i]); } //若前兩個幸運數字與號碼區數字符合,mon += 對應金額 for(int i = 0; i<5; i++){ if(a[0] == b[i]){ mon += c[i]; } } for(int i = 0; i<5; i++){ if(a[1] == b[i]){ mon += c[i]; } } //yes代表第三個數是否有在號碼區中 //若有,yes質變為true bool ...

a104.排序

a104.排序 |  題目連結 :  https://zerojudge.tw/ShowProblem?problemid=a104 |  解題思路 : sort 函式 |  程式碼 :  /* a104. 排序 https://zerojudge.tw/ShowProblem?problemid=a104 sss 2023/10/3 AC (4ms, 316KB) */ #include <bits/stdc++.h> using namespace std; int main(){ int n; while(scanf("%d", &n) != EOF){ int a[1005] = {0}; for(int i=0; i<n; i++){ scanf("%d", &a[i]); } sort(a, a+n); printf("%d",a[0]); for(int i=1; i<n; i++){ printf(" %d", a[i]); } printf("\n"); } return 0; }