發表文章

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 ...

a024.最大公因數(GCD)

圖片
a024.最大公因數(GCD) |  題目連結 :  https://zerojudge.tw/ShowProblem?problemid=a024 |  解題思路 :                例如找出75 ,30的最大公因數             (1) 畫出75*30的矩形               (2)以短邊30為邊長,切割出2個30*30的正方形,矩形變成15*30               (3)以短邊15為周長,切割出2個15*15的正方形               (4)矩形無法再切割,最大公因數為15                           例如找出68、30的最大公因數                                             (1)若x%y = 0,則y為最大公因數               (2)否則x轉成y,y轉成x%y |  程式碼 :  <基礎解> /* a024. 最大公因數(GCD) skyblue https://zerojudge.tw/ShowProblem?problemid=a024 ...

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 */

d066.上學去吧!

d066.上學去吧! |  題目連結 :  https://zerojudge.tw/ShowProblem?problemid=d066 |  解題思路 : if - else判斷 (如程式中註解) |  程式碼 :  /* d066. 上學去吧! https://zerojudge.tw/ShowProblem?problemid=d066 skyblue AC (3ms, 344KB) */ #include <bits/stdc++.h> using namespace std; int main(){ //hh為時,mm為分 int hh; int mm; scanf("%d%d", &hh, &mm); //8:00到17:00前一定在校 if(hh >= 8 && hh < 17){ printf("%s", "At School"); } //7:30以後才算到校 else if(hh == 7 && mm >= 30){ printf("%s", "At School"); } //若好17:00算放學 else if(hh == 17){ printf("%s", "Off School"); } else{ printf("%s", "Off School"); } return 0; }

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 ...

a147.Print it all

a147.Print it all |  題目連結 :  https://zerojudge.tw/ShowProblem?problemid=a147 |  解題思路 : 用while迴圈重複輸入n,for迴圈中,如果小於n的i值不可被7整除則輸出 |  程式碼 :  /* a147. Print it all skyblue https://zerojudge.tw/ShowProblem?problemid=a147 AC (6ms, 304KB) */ #include <bits/stdc++.h> using namespace std; int main(){ int n; while(cin >> n){ if(n>0){ for(int i=1; i<n; i++){ if(i%7 != 0){ cout << i <<" "; } } cout << "\n"; } else if(n == 0) break; } return 0; }

a053.計分程式

a053.計分程式 |  題目連結 :  https://zerojudge.tw/ShowProblem?problemid=a053 |  解題思路 : 設題數為n,判斷n的區間,輸出相對應分數 |  程式碼 :  /* a053 計分程式 skyblue https://zerojudge.tw/ShowProblem?problemid=a053 AC (2ms, 356KB) */ #include <bits/stdc++.h> using namespace std; int main(){ //輸入輸出加速語法 ios::sync_with_stdio(0); cin.tie(0); int n; cin >> n; if(n<=10) cout << n*6 <<"\n"; if((n>= 11) && (n<=20)) cout << 60 + (n-10)*2 <<"\n"; if((n>= 21) && (n<=40)) cout << 80 + (n-20) <<"\n"; if(n>40) cout << 100<<"\n"; return 0; }