2021.1.28寒假打卡Day21

求n个数的最小公倍数

#include<iostream>
using namespace std;

int gcd(int a,int b){
    int r=a%b;
    if(r==0) return b;
    else return gcd(b,r);
}

int nlcm(int a[],int b){
    while(b!=1){
        a[b-2]=a[b-1]*a[b-2]/gcd(a[b-1],a[b-2]);
        --b;
    }
    return a[0];
}

int main(){
    int t;cin>>t;
    while(t--){
        int n,num[100];
        cin>>n;
        for(int i=0;i<n;i++) cin>>num[i];
        int ans=nlcm(num,n);
        cout<<ans<<endl;
    }
    return 0;
}

n的n次幂的个位数

找规律

#include<iostream>
using namespace std;

int solve(unsigned long long n){
    int x=n%10,ans;
    if(x==0){
        ans=0;
    }
    else if(x==1){
        ans=1;
    }
    else if(x==2){
        int y=n%4-1;
        if (y==0) ans=2;
        else if(y==1) ans=4;
        else if(y==2) ans=8;
        else ans=6;
    }
    else if(x==3){
        int y=n%4-1;
        if (y==0) ans=3;
        else if(y==1) ans=9;
        else if(y==2) ans=7;
        else ans=1;
    }
    else if(x==4){
        int y=n%2-1;
        if (y==0) ans=4;
        else ans=6;
    }
    else if(x==5){
        ans=5;
    }
    else if(x==6){
        ans=6;
    }
    else if(x==7){
        int y=n%4-1;
        if (y==0) ans=7;
        else if(y==1) ans=9;
        else if(y==2) ans=3;
        else ans=1;
    }
    else if(x==8){
        int y=n%4-1;
        if (y==0) ans=8;
        else if(y==1) ans=4;
        else if(y==2) ans=2;
        else ans=6;
    }
    else if(x==9){
        int y=n%2-1;
        if (y==0) ans=9;
        else ans=1;
    }
    return ans;
}

int main(){
    int t;
    cin>>t;
    unsigned long long n;
    while(t--){
        cin>>n;
        int ans=solve(n);
        cout<<ans<<endl;
    }
    return 0;
}

快速幂

#include<iostream>
using namespace std;

unsigned long long rapidpow(unsigned long long a,unsigned long long b){
    unsigned long long ans=1;
    while(b){
        ans=rapidpow(a*a,b/2);
        if(b/2==1) ans*=a;
    }
    return ans;
}

int main(){
    unsigned long long a,b;
    cin>>a>>b;
    while(a&&b){
        unsigned long long ans=rapidpow(a,b);
        cout<<ans<<endl;
        cin>>a>>b;
    }
    return 0;
}

一个新的斐波那契数列

Problem Description
现在,有一个新的斐波那契数列,定义如下:

  • F(0) = 7,
  • F(1) = 11,
  • F(n) = F(n-1) + F(n-2) (n>=2).

Input
输入包含多组测试样例,每组测试样例包含一个整数n(n < 1,000,000).

Output
如果F(n)能够被3整除,请输出"yes",否则请输出"no"。

Sample Input

0
1
2
3
4
5

Sample Output

no
no
yes
no
no
no

找规律

#include<iostream>
using namespace std;

int main(){
    int n;
    while(cin>>n){
        int x=(n+1)%8;
        if(x==3||x==7) cout<<"yes"<<endl;
        else cout<<"no"<<endl;
    }
    return 0;
}

解方程

Problem Description
给定方程 8x4 + 7x3 + 2x2 + 3x + 6 == Y,请计算x在[0,100]范围内的解。

Input
输入数据首先是一个正整数T(1<=T<=100),表示有T组测试数据。
接下来T行,每行包含一个实数Y ( fabs(Y) <= 1e10 )。

Output
请计算并输出方程在范围[0,100]内的解,结果精确到小数点后4位。
如果无解,则请输出“No solution!”

Sample Input

2
100
-4

Sample Output

1.6152
No solution!

二分法

#include<iostream>
#include<math.h>
using namespace std;

double solve(double y){
    double l=0,r=100;
    while(l<r){
        double mid=(l+r)/2;
        double f=mid*(mid*(mid*(mid*8+7)+2)+3)+6;
        if(fabs(f-y)<=1e-4)
            return mid;
        else if(f<y)
            l=mid;
        else if(f>y)
            r=mid;
    }
    return -1;
}

int main(){
    int t;
    cin>>t;
    while(t--){
        double y;
        cin>>y;
        double ans=solve(y);
        if(ans==-1) printf("No solution!\n");
        else printf("%.4lf\n",ans);
    }
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值