HDU 5500+HDU 1097 (找规律)

来源:互联网 发布:情绪影响肠胃 知乎 编辑:程序博客网 时间:2024/05/20 12:51

队内选拔赛两道水题……然而一直卡在第一题的规律唉……

HDU 5500:

题意:你可以从数列中任一个位置取一个数字放到数列首部,问到达顺序为1~n所需要的最小操作数

其实就是将两个连续数字间的其他数字移走,不用去在意移走后数字的先后顺序,因为在相同的操作数内总有办法使之达到我们所需要的数列;因为最大的数总是在最后,因此我们从后往前找连续的数位,剩下的就是需要移动的数字;
如:2 1 4 3 5 从后往前,连续数位为5 4,需要移动3 1 2;

#include <iostream>#include <cstdio>using namespace std;const int N=50;int n;int a[N];int main(){    int t;    scanf("%d",&t);    while(t--){        scanf("%d",&n);        for(int i=1;i<=n;i++){            scanf("%d",&a[i]);        }        int cnt=0,j=n;        for(int i=n;i>=1;i--){            if(a[i]==j) j--;            else cnt++;        }        printf("%d\n",cnt);    }}
HDU 1097:

第一眼看到我以为要快速幂呢,后来其实还是就是找规律;
题意:给a,b,求a^b的个位;
a的个位1~10分别考虑一下即可,枚举;

#include <iostream>#include <cstdio>using namespace std;typedef long long ll;int main(){    ll a,b;    while(~scanf("%lld %lld",&a,&b)){        ll ans=0;        ll a1=a%10;        ll b1=0;        if(a1==1||a1==5||a1==6){            ans=a1;        }        else if(a1==2){            b1=b%4;            if(b1==0){                ans=6;            }            else if(b1==1){                ans=2;            }            else if (b1==2){                ans=4;            }            else if(b1==3){                ans=8;            }        }        else if(a1==3){            b1=b%4;            if(b1==0){                ans=1;            }            else if(b1==1){                ans=3;            }            else if (b1==2){                ans=9;            }            else if(b1==3){                ans=7;            }        }        else if(a1==4){            b1=b%2;            if(b1==0){                ans=6;            }            else if(b1==1){                ans=4;            }        }        else if(a1==7){            b1=b%4;            if(b1==0){                ans=1;            }            else if(b1==1){                ans=7;            }            else if (b1==2){                ans=9;            }            else if(b1==3){                ans=3;            }        }        else if(a1==8){            b1=b%4;            if(b1==0){                ans=6;            }            else if(b1==1){                ans=8;            }            else if (b1==2){                ans=4;            }            else if(b1==3){                ans=2;            }        }        else if(a1==9){            b1=b%42;            if(b1==0){                ans=1;            }            else if(b1==1){                ans=9;            }        }        printf("%lld\n",ans);       }}
0 0
原创粉丝点击