離散數學練習(林大OJ)

度數序列

在這裏插入圖片描述

#include <bits/stdc++.h>
using namespace std;
int main()
{
    int n;
    while(cin>>n)
    {
        int sum=0;
        for(int i=0;i<n;i++)
        {
            int a;
            cin>>a;
            if(a%2==1)
                sum++;
        }
        if(sum%2==0)
            printf("yes\n");
        else
            printf("no\n");
    }
    return 0;
}

平面圖

在這裏插入圖片描述

#include <bits/stdc++.h>
using namespace std;
int main()
{
    int n,r;
    while(cin>>n>>r)
    {
        cout<<n+r-2<<endl;
    }
    return 0;
}

樹的邊數

在這裏插入圖片描述

#include <bits/stdc++.h>
using namespace std;
int main()
{
    int t;
    while(cin>>t)
    {
        cout<<2*(t-1)<<endl;
    }
    return 0;
}

錯排

在這裏插入圖片描述

#include <bits/stdc++.h>
using namespace std;
int fun(int n);
int main()
{
    int n;
    while(~scanf("%d",&n))
    {
        int res=fun(n);
        printf("%d\n",res);
    }
    return 0;
}
int fun(int n)
{
    int k;
    if(n==0||n==1)
        k=0;
    else if(n==2)
        k=1;
    else
        k=(fun(n-1)+fun(n-2))*(n-1);
    return k;
}

數字編碼

在這裏插入圖片描述

#include <bits/stdc++.h>
using namespace std;
int main()
{
    int n;
    while(cin>>n)
    {
        printf("%.0lf\n",(pow(6,n)+pow(8,n))/2);
    }
    return 0;
}

最大公約數-離散數學

在這裏插入圖片描述

#include <bits/stdc++.h>
using namespace std;
long long int gcd(long long int a,long long int b);
int main()
{
    int n,m;
    while(cin>>n>>m)
    {
        if(gcd(m,n)==1)
            cout<<"yes"<<endl;
        else
            cout<<"no"<<endl;
    }
    return 0;
}
long long int gcd(long long int a,long long int b)
{
    return b?gcd(b,a%b):a;//相當於 b!=0執行;前 b==0執行:後 //歐幾里得算法 遞歸
}

非降路徑

在這裏插入圖片描述

#include <bits/stdc++.h>
using namespace std;
long long int fun(long long int n);
long long int c(long long int n,long long int r);
int main()
{
    long long int a,b,m,n;
    cin>>a>>b>>m>>n;
    cout<<c(m-a+n-b,m-a)<<endl;
    return 0;
}
long long int fun(long long int n)
{
    long long int k;
    if(n==0||n==1)
        k=1;
    else
        k=fun(n-1)*n;
    return k;
}
long long int c(long long int n,long long int r)
{
    long long int res;
    res=fun(n)/(fun(n-r)*fun(r));
    return res;
}

2元完全正則樹

在這裏插入圖片描述

#include <bits/stdc++.h>
using namespace std;
int main()
{
    int n;
    cin>>n;
    int res=1;
    int ans;
    for(int i=1;i<=n;i++)
    {
        res=res+pow(2,i);
        if(n==i)
            ans=pow(2,i);
    }
    cout<<res<<" "<<res-1<<" "<<ans<<endl;
    return 0;
}

計算連通分支

在這裏插入圖片描述

#include <bits/stdc++.h>
using namespace std;
int main()
{
    int n,m,r;
    cin>>n>>m>>r;
    cout<<n-m+r-1;
    return 0;
}

求懸掛頂點

在這裏插入圖片描述

#include <bits/stdc++.h>
using namespace std;
int main()
{
    int m,t;
    cin>>m>>t;
    cout<<2*m-20*t<<endl;
    return 0;
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章