Skip to content

實作一

b964. 1. 成績指標(2016年3月)

使用:
陣列排序
for
if

O(n log(n)
#include<bits/stdc++.h>
using namespace std;
int main(){
    int n,v[100];
    cin>>n;
    int low=-1;
    int upp=101;
    for(int i=0;i<n;i++){
        cin>>v[i];
        if(v[i]<60 && v[i]>low){
            low=v[i];
        }
        else if(v[i]>=60 && v[i]<upp){
            upp=v[i];
        }
    }
    sort(v,v+n);

    for(int i=0;i<n;i++){
        cout<<v[i]<<" ";
    }

    cout<<"\n";

    if(low!=-1){
        cout<<low;
    }
    else{
        cout<<"best case";
    }
    cout<<"\n";
    if(upp!=101){
        cout<<upp;
    }
    else{
        cout<<"worst case";
    }

}

c294. APCS-2016-1029-1三角形辨別(2016年10月)

使用:
陣列排序
if

O(n log(n)
#include <bits/stdc++.h>
using namespace std;
#define a v[0]
#define b v[1]
#define c v[2]
#define nn "\n"
#define nnn " "

int main(){
    int v[100];
    cin>>v[0]>>v[1]>>v[2];
    sort(v,v+3);
    cout<<a<<nnn<<b<<nnn<<c;
    cout<<"\n";
    if(a+b<=c){
        cout<<"No";
    }
    else if(a*a+b*b<c*c){
        cout<<"Obtuse";
    }
    else if(a*a+b*b==c*c){
        cout<<"Right";
    }
    else if(a*a+b*b>c*c){
        cout<<"Acute";
    }
}

c290. APCS 2017-0304-1秘密差(2017年3月)

使用:
for
字元陣列

O(N)
#include <bits/stdc++.h>
using namespace std;

int main(){
    char v[1000000];
    cin>>v;
    int x=0,y=0;
    for(int i=0;i<strlen(v);i+=2){
        x+=v[i]-'0';
    }
    for(int i=1;i<strlen(v);i+=2){
        y+=v[i]-'0';
    }
    cout<<abs(x-y);
}

c461. apcs 邏輯運算子 (Logic Operators)(2017年10月)

使用:
if
and or xor
xor判定true要是1

O(N)
#include <bits/stdc++.h>
using namespace std;
#define nn "\n"

int main(){
    int x,y,z,t=1;
    cin>>x>>y>>z;
    if(x!=0){
        x=1;
    }
    if(y!=0){
        y=1;
    }
    if((x and y)==z){
        cout<<"AND"<<nn;
        t=0;
    }
    if((x or y)==z){
        cout<<"OR"<<nn;
        t=0;
    }
    if((x xor y)==z){
        cout<<"XOR"<<nn;
        t=0;
    }
    if(t){
        cout<<"IMPOSSIBLE";
    }
}

e283. APCS 類似題 - 小崴的特殊編碼(2018年6月)

使用:
for
if

O(N)
#include <bits/stdc++.h>
using namespace std;
int main(){
    ios::sync_with_stdio(0);
    cin.tie(0);
    int n;
    while(cin>>n){
        for(int i=0;i<n;i++){
            int a,b,c,d;
            cin>>a>>b>>c>>d;
            if(a==0 && b==1 && c==0 && d==1){
                cout<<"A";
            }  
            if(a==0 && b==1 && c==1 && d==1){
                cout<<"B";
            }   
            if(a==0 && b==0 && c==1 && d==0){
                cout<<"C";
            }   
            if(a==1 && b==1 && c==0 && d==1){
                cout<<"D";
            } 
            if(a==1 && b==0 && c==0 && d==0){
                cout<<"E";
            } 
            if(a==1 && b==1 && c==0 && d==0){
                cout<<"F";
            }

        }
        cout<<endl;
    }
}

e313. 最少相異字母(2018年10月)

使用:
for
字元陣列 or string

O(N) string
#include <bits/stdc++.h>
using namespace std;
int main(){
    string c,mic;
    int n,mi=1000000;
    cin>>n;
    for(int i=0;i<n;i++){
        cin>>c;
        int v[100],subsum=0;
        for(int j=0;j<=25;j++){
            v[j]=0;
        }
        for(int j=0;j<c.size();j++){
            v[c[j]-'A']++;
        }
        for(int j=0;j<=25;j++){
            if(v[j]>0){
                subsum++;
            }
        }
        if (subsum<mi || (subsum==mi && c<mic)) {
            mic=c;
            mi=subsum;
        }
    }
    cout<<mic;
}
O(N) 字元陣列
#include <bits/stdc++.h>
using namespace std;
int main(){
    char c[10000],mic[10000];
    int n,mi=1000000;
    cin>>n;
    for(int i=0;i<n;i++){
        cin>>c;
        int v[100],subsum=0;
        for(int j=0;j<=25;j++){
            v[j]=0;
        }
        for(int j=0;j<strlen(c);j++){
            v[c[j]-'A']++;
        }
        for(int j=0;j<=25;j++){
            if(v[j]>0){
                subsum++;
            }
        }
        if (subsum<mi || (subsum==mi && strcmp(c,mic)<0)) {
            strcpy(mic, c);
            mi = subsum;
        }
    }
    cout<<mic;
}

e286. 籃球比賽(2019年06月)

使用:
for

O(N)
#include <bits/stdc++.h>
using namespace std;
#define nn "\n"

int main(){
    int wx=0,wy=0;
    int x1=0,x2=0;
    int y1=0,y2=0;
    
    for(int i=0;i<4;i++){
        int x;
        cin>>x;
        x1+=x;
    }
    for(int i=0;i<4;i++){
        int x;
        cin>>x;
        y1+=x;
    }
    for(int i=0;i<4;i++){
        int x;
        cin>>x;
        x2+=x;
    }
    for(int i=0;i<4;i++){
        int x;
        cin>>x;
        y2+=x;
    }

    cout<<x1<<":"<<y1<<nn;
    cout<<x2<<":"<<y2<<nn;
    if(x1>y1){
        wx+=1;
    }
    else if(x1<y1){
        wy+=1;
    }
    if(x2>y2){
        wx+=1;
    }
    else if(x2<y2){
        wy+=1;
    }

    if(wx>wy){
        cout<<"Win";
    }
    else if(wx<wy){
        cout<<"Lose";
    }
    else{
        cout<<"Tie";
    }
}

h026. 202001_1 猜拳(2020年1月)

使用:
陣列(非必要)

函數(非必要)

陣列O(N)
#include <bits/stdc++.h>
using namespace std;
#define nn "\n"

int won(int x,int y){
    if(x==0 && y==2){
        return 1;
    }
    if(x==2 && y==5){
        return 1;
    }
    if(x==5 && y==0){
        return 1;
    }
    if(x==y){
        return 2;
    }
    return 3;
}

int w(int x){
    if(x==0)return 5;
    if(x==2)return 0;
    if(x==5)return 2;
}


int main(){
    int f[100],s[100];
    cin>>f[1];
    s[0]=-1;
    int n;
    cin>>n;
    for(int i=1;i<=n;i++){
        cin>>s[i];
    }
    for(int i=1;i<=n;i++){
        cout<<f[i]<<" ";
        if(won(f[i],s[i])==1){
            cout<<": Won at round "<<i;
            return 0;
        }
        else if(won(f[i],s[i])==3){
            cout<<": Lost at round "<<i;
            return 0;
        }
        else{
            if(s[i]==s[i-1]){
                f[i+1]=w(s[i]);
            }
            else{
                f[i+1]=f[i];
            }
        }

    }
    cout<<": Drew at round "<<n;
}

f579. 1. 購物車(2020年7月)

使用:
and
for

O(N)
#include <bits/stdc++.h>
using namespace std;
#define nn "\n"
int main(){
    int n,m,ans=0;
    cin>>n>>m;
    int w;
    cin>>w;
    for(int i=0;i<w;i++){
        int subx=0,suby=0;
        for(;true;){
            int x;
            cin>>x;
            if(x==0){
                break;
            }
            if(x==n){
                subx++;
            }
            if(x==-n){
                subx--;
            }
            if(x==m){
                suby++;
            }
            if(x==-m){
                suby--;
            }
        }
        if(subx and suby){
            ans++;
        }
    }
    cout<<ans;
}

f312. 1. 人力分配(2020年10月)

使用:
for
max(非必要)

O(N)
#include <bits/stdc++.h>
using namespace std;
#define nn "\n"

int main(){
    int ma=-INT_MAX;
    int a1,a2,a3;
    int b1,b2,b3;
    cin>>a1>>a2>>a3;
    cin>>b1>>b2>>b3;
    int n;
    cin>>n;
    for(int i=0;i<=n;i++){//i、(n-i)
        int a=0,b=0;
        a=a1*(i*i)+a2*i+a3;
        b=b1*((n-i)*(n-i))+b2*(n-i)+b3;
        ma=max(a+b,ma);
    }
    cout<<ma;
}

f605. 1. 購買力(2021年1月)

使用:
for

O(N)
#include <bits/stdc++.h>
using namespace std;

int main(){
    int n,m;
    cin>>n>>m;
    int ans1=0,ans2=0;
    for(int i=0;i<n;i++){
        int x,y,z;
        cin>>x>>y>>z;
        if(x>z)swap(x,z);
        if(y>z)swap(y,z);
        if(x>y)swap(x,y);

        if(z-x>=m){
            ans1++;
            ans2+=(x+y+z)/3;
        }

    }
    cout<<ans1<<" "<<ans2;
}

g275. 1. 七言對聯(2021年9月)

使用:
for
陣列

O(N)
#include <bits/stdc++.h>
using namespace std;

int main(){
    ios::sync_with_stdio(0);
    cin.tie(0);

    int n;
    cin>>n;
    for(int w=0;w<n;w++){
        int a[8],b[8];
        int t=0;
        for(int i=0;i<7;i++){
            cin>>a[i];
        }
        for(int i=0;i<7;i++){
            cin>>b[i];
        }
        if(a[1]==a[3]  || b[1]==b[3]  || a[1]!=a[5] || b[1]!=b[5] ){
            cout<<"A";
            t=1;
        }
        if(a[6]!=1 || b[6]!=0 ){
            cout<<"B";
            t=1;
        }
        if(a[1]==b[1] || a[3]==b[3] || a[5]==b[5]){
            cout<<"C";
            t=1;
        }
        if(t==0){
            cout<<"None";
        }
        cout<<endl;
    }

}

g595. 1. 修補圍籬(2021年11月)

使用:
for
陣列
if

O(N)
#include <bits/stdc++.h>
using namespace std;
int main(){
    int n,ans=0;
    cin>>n;
    int v[1000];
    v[0]=1000;
    v[n+1]=1000;
    for(int i=1;i<=n;i++){
        cin>>v[i];
    }
    for(int i=1;i<=n;i++){
        if(v[i]==0){
            ans+=min(v[i-1],v[i+1]);
        }
    }
    cout<<ans;
}

h081. 1. 程式交易(2022年1月)

使用:
for

O(N)
#include <bits/stdc++.h>
using namespace std;
int main(){
    int n,m,ans=0;
    cin>>n>>m;
    int pr=1000;
    bool buy=false;
    for(int i=0;i<n;i++){
        int x;
        cin>>x;
        if(!buy && pr-m>=x){
            pr=x;
            buy=true;
        }
        if(buy && pr+m<=x){
            ans+=x-pr;
            pr=x;
            buy=false;
        }
    }
    cout<<ans;
}

i399. 1. 數字遊戲(2022年6月)

使用:
for
陣列

O(N)
#include <bits/stdc++.h>
using namespace std;
int main(){
    int a,b,c;
    cin>>a>>b>>c;
    int v[10];
    for(int i=1;i<=9;i++)v[i]=0;
    v[a]++;
    v[b]++;
    v[c]++;
    int ma=0;
    for(int i=9;i>=1;i--){
        ma=max(ma,v[i]);
    }
    cout<<ma<<" ";
    for(int i=9;i>=1;i--){
        if(v[i]>0){
            cout<<i<<" ";
        }
    }
}

i428. 1. 巴士站牌(2022年10月)

使用:
for
max(非必要)

O(N)
#include <bits/stdc++.h>
using namespace std;
int main(){
    int n;
    cin>>n;
    int x,y,px=0,py=0;
    int ans_max=-1,ans_min=INT_MAX;
    cin>>px>>py;
    for(int i=1;i<n;i++){
        cin>>x>>y;
        ans_max=max(ans_max,abs(x-px)+abs(y-py));
        ans_min=min(ans_min,abs(x-px)+abs(y-py));
        px=x,py=y;
    }
    cout<<ans_max<<" "<<ans_min;
}

j605. 1. 程式考試(2023年1月)

使用:
for

O(N)
#include <bits/stdc++.h>
using namespace std;
int main(){
    int n;
    cin>>n;
    int low=0;
    int maxvalue=-1,maxindex=-1,ans=0;
    for(int i=0;i<n;i++){
        int x,y;
        cin>>x>>y;
        if(y==-1){
            low++;
        }
        if(y>maxvalue){
            maxvalue=y;
            maxindex=x;
        }
    }
    ans=maxvalue-n-low*2;
    if(ans<0)ans=0;
    cout<<ans<<" "<<maxindex;
}

k731. 1. 路徑偵測(2023年6月)

使用:
for
if

O(N)
#include <bits/stdc++.h>
using namespace std;
int main(){
    int n,a=0,b=0,c=0;
    cin>>n;
    int prx=0,pry=0;
    int x,y;
    int t=0;//1 東 2 南 3 西 4 北
    cin>>prx>>pry;
    if(prx>0){
        t=1;
    }
    if(prx<0){
        t=3;
    }
    if(pry>0){
        t=2;
    }
    if(pry<0){
        t=4;


    }
    
    for(int i=1;i<n;i++){
        cin>>x>>y;
        if(t==1){
            if(x<prx){
                c++;
                t=3;
            }
            else if(y<pry){
                t=2;
                b++;
            }
            else if(y>pry){
                t=4;
                a++;
            }
        }
        else if(t==2){
            if(x<prx){
                b++;
                t=3;
            }
            else if(x>prx){
                a++;
                t=1;
            }
            else if(y>pry){
                c++;
                t=4;
            }
        }
        else if(t==3){
            if(x>prx){
                c++;
                t=1;
            }
            else if(y<pry){
                a++;
                t=2;
            }
            else if(y>pry){
                b++;
                t=4;
            }
        }
        else if(t==4){
            if(x<prx){
                a++;
                t=3;
            }
            else if(x>prx){
                b++;
                t=1;
            }
            else if(y<pry){
                c++;
                t=2;
            }
        }
        prx=x;
        pry=y;
    }
    cout<<a<<" "<<b<<" "<<c;
}

m370. 1. 機械鼠(2023年10月)

使用:
for

O(N)
#include <bits/stdc++.h>
using namespace std;
int main(){
    int n,m;
    cin>>n>>m;//起始、總數
    int ma=-1000;
    int mi=1000;
    int r=0,l=0;
    for(int i=0;i<m;i++){
        int x;
        cin>>x;
        if(x>n){
            r++;
            ma=max(ma,x);
        }
        else if(x<n){
            l++;
            mi=min(mi,x);
        }
    }
    if(r>l){
        cout<<r<<" "<<ma;
    }
    else{
        cout<<l<<" "<<mi;
    }
}

m931. 1. 遊戲選角(2024年1月)

使用:
struct (不必要)
陣列排序

struct
#include <bits/stdc++.h>
using namespace std;
struct st{
    int x;
    int y;
    int z;
};
bool cmp(st a,st b){
    return a.z>b.z;
}
int main(){
    int n;
    cin>>n;
    st v[100];
    for(int i=0;i<n;i++){
        cin>>v[i].x>>v[i].y;
        v[i].z=pow(v[i].x,2)+pow(v[i].y,2);
    }
    sort(v,v+n,cmp);
    cout<<v[1].x<<" "<<v[1].y;
}
兩次max(無struct)
#include <bits/stdc++.h>
using namespace std;

int main(){
    int n;
    cin>>n;
    int v[30][3]={};

    for(int i=0;i<n;i++){
        for(int j=0;j<2;j++){
            cin>>v[i][j];
        }
    }


    for(int i=0;i<n;i++){
        v[i][2]=v[i][0]*v[i][0]+v[i][1]*v[i][1];
    }
    int ma1=-1;
    int ma2=-1;
    int ma2i=-1;
    for(int i=0;i<n;i++){
        ma1=max(ma1,v[i][2]);
    }
    for(int i=0;i<n;i++){
        if(v[i][2]>ma2 && v[i][2]<ma1){
            ma2=v[i][2];
            ma2i=i;
        }
    }

    cout<<v[ma2i][0]<<" "<<v[ma2i][1];


    return 0;
}

o076. 1. 特技表演 2024/6

for
max(不必要)

#include<bits/stdc++.h>
using namespace std;
int main(){
    int n;
    cin>>n;
    int ans=0,sum=1;
    int pr=0;
    cin>>pr;
    for(int i=1;i<n;i++){
        int x;
        cin>>x;
        if(x<pr){
            sum++;
        }
        else{
            sum=1;
        }
        ans=max(sum,ans);
        pr=x;
    }
    cout<<ans;
    return 0;
}

難但可接受o711. 1. 裝飲料 2024/10

for
max
if

#include<bits/stdc++.h>
using namespace std;
int main(){
//    istringstream cin("1 \
4 6 8 5 \
200");

//    istringstream cin("2 \
5 10 12 8 \
400 600");

//    istringstream cin("5 \
16 44 28 17 \
2560 1280 1536 1024 10448");
    int n;
    cin>>n;
    int w1,w2,h1,h2;
    cin>>w1>>w2>>h1>>h2;
    int c1=0,c2=0;
    int f1=w1*w1*h1;
    int f2=w2*w2*h2;
    int ans=0;

    for(int i=0;i<n;i++){
        int x;
        cin>>x;

        if(c1<f1){
            if(c1+x<f1){
                ans=max(ans,x/(w1*w1));
                c1+=x;
            }
            else{
                c2+=x-(f1-c1);
                if(c2>=f2){
                    ans=max(ans,(f1-c1)/(w1*w1)+h2);
                    c2=f2;
                    break;
                }
                else{
                    ans=max(ans,(f1-c1)/(w1*w1)+c2/(w2*w2));
                }
                c1+=x;
            }
        }
        else{
            if(c2+x<f2){
                ans=max(ans,x/(w2*w2));
            }
            else{
                ans=max(ans,(f2-c2)/(w2*w2));
                break;
            }
        }
    }
    cout<<ans;
    return 0;
}

q181. 1. 等紅綠燈 2025/1

for
if
%餘數

#include<bits/stdc++.h>
using namespace std;
#define stop system("pause");
int main(){
//    istringstream cin("10 10 \
1 \
14");
//    istringstream cin("4 3 \
3 \
12 16 25");
    int g,r;
    cin>>g>>r;
    int n,ans=0;
    cin>>n;
    for(int i=0;i<n;i++){
        int x;
        cin>>x;
        if(x%(g+r)>=g){
            ans+=r-(x%(g+r)-g);
        }
    }
    cout<<ans;
    return 0;
}

q836. 1. 小心陷阱 2026/6/15

while
if

#include<bits/stdc++.h>
using namespace std;
#define int long long
#define stop system("pause")

signed main(){
    ios::sync_with_stdio(0);
    cin.tie(0);
//    istringstream cin("1 \
3 1 \
4 1")
//    istringstream cin("7 \
3 2 \
2 3");
    int n;
    cin>>n;
    int x1,x2,y1,y2;
    cin>>x1>>x2>>y1>>y2;
    int sum=0;
    while(n>0){
        sum+=n;
        if(sum%x1==0){
            n-=x2;
        }
        if(sum%y1==0){
            n-=y2;
        }
    }
    cout<<sum;
    return 0;
}