APCS

vector http://mropengate.blogspot.tw/2015/07/cc-vector-stl.html

#include<bits/stdc++.h>
using namespace std;

int main(){
    vector<int> ary(3,1);
    ary[0]=2;

    for(int i=0;i<ary.size();i++){
        cout<<ary[i]<<" ";
    }
    cout<<endl;

    ary.push_back(19);

    for(int i=0;i<ary.size();i++){
        cout<<ary[i]<<" ";
    }
    cout<<endl;

    cout<<"結尾: "<<ary.back()<<endl;
    cout<<"開頭: "<<ary.front()<<endl;

    ary.insert ( ary.begin()+2 , 7 );

    for(int i=0;i<ary.size();i++){
        cout<<ary[i]<<" ";
    }
    cout<<endl;

}

d498: 我不說髒話 https://zerojudge.tw/ShowProblem?problemid=d498

#include <iostream>

using namespace std;

int main()
{
int n;
cin>>n;
for(int i=1;i<=n;i++)
{

cout << "I don't say swear words!"<< endl;
}
}

d143: 11172 - Relational Operators https://zerojudge.tw/ShowProblem?problemid=d143

#include<bits/stdc++.h>
using namespace std;

main() {

    int t,a,b;
    while(cin>>t){
        for(int i=0;i<t;i++){
            cin>>a>>b;
            if(a>b){
                cout<<">"<<endl;
            }else if(a<b){
                cout<<"<"<<endl;
            }else{
                cout<<"="<<endl;
            }
            }
        }

    }

d649: 數字三角形 https://zerojudge.tw/ShowProblem?problemid=d649

#include <bits/stdc++.h>
using namespace std;

int main(){
    int n;
    while(cin>>n){

        for(int i =0; i<n ;i++){
            for(int j=n-i;j>1;j--){
                cout<<"_";
            }
            for(int j=0;j<=i;j++){
                cout<<"+";
            }
            cout<<endl;
        }
    }
}

d827: 買鉛筆 https://zerojudge.tw/ShowProblem?problemid=d827

#include <bits/stdc++.h>
using namespace std;

int main(){
    int n;
    cin>>n;
    cout<<n/12*50+n%12*5;

}

d485: 我愛偶數 https://zerojudge.tw/ShowProblem?problemid=d485

#include <bits/stdc++.h>
using namespace std;

int main(){

    int a,b,cnt=0;
    cin>>a>>b;

    if(a%2)a++;
    if(b%2)b--;

    cnt=(b-a)/2+1;

    cout<<cnt;

}

d065: 三人行必有我師 https://zerojudge.tw/ShowProblem?problemid=d065

#include<bits/stdc++.h>
using namespace std;

int main(){

    int a[3];
    for(int i=0;i<3;i++){
        cin>>a[i];
    }
    sort(a,a+3);
    cout<<a[2];

}

d060: 還要等多久啊?https://zerojudge.tw/ShowProblem?problemid=d060

#include<bits/stdc++.h>
using namespace std;

int main(){

    int t;

    cin>>t;

    if(t>25){
        cout<<60-t+25;
    }else{
        cout<<25-t;
    }
}

d478: 共同的數 - 簡易版 https://zerojudge.tw/ShowProblem?problemid=d478

#include <bits/stdc++.h>
using namespace std;

int main(){

    int n,m;
    int a[10000];
    int b[10000];
    int ans;
    int acnt=0;
    int bcnt=0;

    while(cin>>n>>m){

    for(int g=0;g<n;g++){
        for(int i = 0; i<m; i++){
            cin>>a[i];
        }

        for(int i = 0; i<m; i++){
            cin>>b[i];
        }

        sort(a,a+m);
        sort(b,b+m);

        ans=0;
        acnt=0;
        bcnt=0;
        while(!(acnt>=m||bcnt>=m)){

            if(a[acnt]==b[bcnt]){
                ans++;
                acnt++;
                bcnt++;
            }
            else if(a[acnt]>b[bcnt]){
                bcnt++;
            }else{
                acnt++;
            }
        }

        cout<<ans<<endl;
    }
}
}

038: 數字翻轉 https://zerojudge.tw/ShowProblem?problemid=a038

#include <bits/stdc++.h>
using namespace std;

int main(){

int n;
int ans=0;
cin>>n;
while(n/10){
ans=ans*10+(n%10);
n=n/10;
}
ans=ans*10+n;
cout<<ans;

}

a686: 蝸牛往上爬 https://zerojudge.tw/ShowProblem?problemid=a686

#include <bits/stdc++.h>
using namespace std;

int main(){

    int days,x,y,z;
    int t;
    cin>>t;

    for(int i=0;i<t;i++){
        cin>>x>>y>>z;
        if(y-z<=0)
        {
            cout<<"Poor Snail"<<endl;
        }
        else
        {
            days=01;
            x=x-y;
            while(x>0){
            x=x-(y-z);
            days++;
        }
        cout<<days<<endl;
    }
}

}

d212: 東東爬階梯 https://zerojudge.tw/ShowProblem?problemid=d212

DP 寫法可以AC

#include <bits/stdc++.h>
using namespace std;


int main()
{
    int i,n;
    long long arr[101];

    arr[1] = 1;
    arr[2] = 2;

    for(i=3;i<100;i++)
        arr[i] = arr[i-1] + arr[i-2];

    while(cin>>n)
        printf("%lld\n",arr[n]);

    return 0;
}

DFS寫法只能過測試,正式會TLE

    #include<bits/stdc++.h>
    using namespace std;

    int goal,ans;

    void dfs(int n){

        if(n>goal){

            return;
        }
        if(n==goal){
                ans++;

            return;
        }
        dfs(n+1);
        dfs(n+2);

    }

    int main(){

        while(cin>>goal){
                ans=0;
           if(goal==1){
            ans=1;
           }else{
               dfs(1);
               dfs(2);
           }
           cout<<ans<<endl;

        }

    }

b572: 忘了東西的傑克 https://zerojudge.tw/ShowProblem?problemid=b572

#include<bits/stdc++.h>
using namespace std;

int main(){

    int t;
    int h1, h2, m1, m2, walkTime;
    while (cin >> t) {
        for (int i= 0; i < t;i++){
            cin >> h1 >> m1 >> h2 >> m2 >> walkTime;
            //發時時間-現在時間>=走路時間
            //發車時間怎麼計算
            int time1 = h1 * 60 + m1;
            int time2 = h2 * 60 + m2;
            if (time2 - time1 >= walkTime) {
                cout << "Yes" << endl;
            } else {
                cout << "No" << endl;
            }
        }
    }
}

c004: 10812 - Beat the Spread! https://zerojudge.tw/ShowProblem?problemid=c004

#include<bits/stdc++.h>
using namespace std;

int main(){

    int t;
    int a,b;
    int flag;

    while(cin>>t){
        for(int i=0;i<t;i++){
            cin>>a>>b;
            int gap = abs(a-b);
            flag=false;
            for(int i=a;i>=b;i--){
                if(2*i-b==a){
                    flag=true;
                    cout<<i<<" "<<i-b<<endl;
                    break;
                }
            }
            if(!flag){
                cout<<"impossible"<<endl;
            }
        }
    }
}

d887: 1.山脈種類(chain) https://zerojudge.tw/ShowProblem?problemid=d887

#include<bits/stdc++.h>
using namespace std;

int ans;
int n;

void dfs(int now,int height){

    if(height<0||height>n){
        return;
    }

    if(now==n*2){
        if(height==0){
            ans++;
        }
        return;
    }
    dfs(now+1,height+1);
    dfs(now+1,height-1);
}


int main(){

    while(cin>>n){
        ans=0;

        dfs(1,1);
        cout<<ans<<endl;
    }

}

d115: 數字包牌 https://zerojudge.tw/ShowProblem?problemid=d115


a059: 完全平方和

https://zerojudge.tw/ShowProblem?problemid=a059


#include<bits/stdc++.h>
using namespace std;

int main(){

    int t, a, b;
    int sum;

    while (cin >> t) {
        for (int i= 0; i < t;i++){
            cin >> a >> b;
            sum = 0;
            int n = ceil(sqrt(a));
            while (n * n <= b) {
                sum += n * n;
                n++;
            }
            cout << "Case " << i + 1 << ": " << sum << endl;
        }
    }

}

a587: 祖靈好孝順 ˋˇˊ https://zerojudge.tw/ShowProblem?problemid=a587

Greedy會錯... 要用DP

#include<bits/stdc++.h>
using namespace std;

struct Item{
    int v;
    int w;
    float vw;
};

bool cmp(Item a, Item b){
    if(a.vw>b.vw)
        return true;
    else
        return false;
}

main() {

    int t,backpack,price;
    Item items[100];

    while(cin>>t){
        price =0;
        for(int i=0;i<t;i++){
            cin>>items[i].w>>items[i].v;
            items[i].vw=items[i].v/items[i].w;
        }
        cin>>backpack;

        //=====================================

        sort(items,items+t,cmp);

         for(int i=0;i<t;i++){
            if(backpack-items[i].w>=0){
                backpack -= items[i].w;
                price += items[i].v;
            }else{
                break;
            }
         }
        cout<<price;
    }
}

DP解答 http://mypaper.pchome.com.tw/zerojudge/post/1323804656


c203: 13185 - DPA Numbers I https://zerojudge.tw/ShowProblem?problemid=c203

#include<bits/stdc++.h>
using namespace std;

int main(){
    int n;
    int sum;
    int t;
    while(cin>>t){
        for(int g=0;g<t;g++){
            sum=0;
            cin>>n;
            for(int i=1;i<n;i++){
                if(n%i==0){
                  sum+=i;
                }
            }

            if(sum==n){
                cout<<"perfect"<<endl;
            }else if(sum>n){
                cout<<"abundant"<<endl;
            }else{
                cout<<"deficient"<<endl;
            }
        }
    }
}

a012: 10055 - Hashmat the Brave Warrior (要用long long int) https://zerojudge.tw/ShowProblem?problemid=a012

#include<bits/stdc++.h>
using namespace std;

int main(){
    long long int  a,b;

    while(cin>>a>>b){
        cout<<abs(a-b)<<endl;
    }
}

c024: 10079 - Pizza Cutting (long long int, 找公式) https://zerojudge.tw/ShowProblem?problemid=c024

#include<bits/stdc++.h>
using namespace std;

int main(){
    long long int n,sum;
    while(cin>>n){
        if(n<0){
            break;
        }
        sum=1;
        for(int i=1;i<=n;i++){
            sum+=i;
        }
        cout<<sum<<endl;
    }

}

a536: 11689 - Soda Surpler https://zerojudge.tw/ShowProblem?problemid=a536 (換完後的汽水罐,等喝完後還可以再換,所以不是只能換一次這樣)

#include<bits/stdc++.h>
using namespace std;

int main(){
    int t,e,f,c,sum,colas;
    while(cin>>t){
        for(int i=0;i<t;i++){
            cin>>e>>f>>c;
            sum=0;
            colas = e+f;
            while(colas/c){
                sum += colas/c;
                colas =colas/c+colas%c;
            }

            cout<<sum<<endl;
        }
    }
}

c022: 10783 - Odd Sum https://zerojudge.tw/ShowProblem?problemid=c022

#include<bits/stdc++.h>
using namespace std;

int main(){
    int t,a,b,sum;
    while(cin>>t){
        for(int i=0;i<t;i++){
            cin>>a>>b;
            if(a%2==0)a++;
            if(b%2==0)b--;
            sum=0;
            for(int j=a;j<=b;j+=2){
                sum+=j;
            }
            cout<<"Case "<<i+1<<": "<<sum<<endl;
        }

    }
}

d038: 00900 - Brick Wall Patterns https://zerojudge.tw/ShowProblem?problemid=d038

#include<bits/stdc++.h>
using namespace std;

int main(){
    long long int t,ary[51],n;
    ary[1]=1;
    ary[2]=2;
    for(int i=3;i<=50;i++){
        ary[i]=ary[i-1]+ary[i-2];
    }

    while(cin>>n){
        if(n==0)break;
        cout<<ary[n]<<endl;
    }
}

a524: 手機之謎 https://zerojudge.tw/ShowProblem?problemid=a524

#include<bits/stdc++.h>
using namespace std;
int ans[10]={0},b[10]={0},n;

void dfs(int x)
{
    int i;
    if(x==n){
        for(i=0;i<n;i++)
            cout<<ans[i];
        cout<<endl;
        return;
    }
    for(i=n;i>=1;i--){
        if(b[i]==0){
            b[i]=1;
            ans[x]=i;
            dfs(x+1);
            b[i]=0;
        }
    }
}
int main()
{
 while(cin>>n)
 {
     for(int i=0;i<n;i++){
        b[i]=0;
     }
  dfs(0);
 }
 return 0;
}

d115: 數字包牌 https://zerojudge.tw/ShowProblem?problemid=a524

#include <bits/stdc++.h>
using namespace std;

int step[100],num[100];   /*  num陣列永遠儲存排序後的元素值*/
int n,m;

/*
currStep 表示目前取到第幾個
start 表示開始的索引值位置,儲存入step陣列
step陣列儲存所取的元素索引值
*/
void find(int currStep,int start){
  int j;
  if (currStep == m){
    for(int i=0;i<m;i++){
      cout << num[step[i]] << " ";
    }
    cout << endl;
  }else{
    for(j=start;j<n;j++){
      step[currStep]=j;
      find(currStep+1,j+1);
    }
  }
}


int main(){
  while (cin >> n){
    for(int i=0;i<n;i++){
      cin >> num[i];
    }
    cin >> m;
    sort(num,num+n);

    find(0,0);
    cout << endl;
  }
}

提款卡密碼 https://zerojudge.tw/ShowProblem?problemid=a065

#include <bits/stdc++.h>
using namespace std;

int main(){

    char str[8];

    while(cin>>str){
        for(int i=1;i<7;i++){
            cout<<abs(str[i]-str[i-1]);
        }
        cout<<endl;
    }

}

results matching ""

    No results matching ""