复制代码

小米 2018.04.10 春招AK 代码

题目 自己百度一下,我直接贴一份AK 代码

 

A 求一个数的每一位n^2

#include<bits/stdc++.h>
using namespace std;
 
typedef unsigned long long ll; 
 
ll n ;
bool pp(ll n)
{
    ll tmp = 0;
    while(n!=0)
    {
        tmp+=(n%10)*(n%10);
        n/=10;
    }
    if(tmp==1)return true;
    if(tmp >=10) return pp(tmp);
    else return false;    
}

int main()
{
    
    freopen("in","r",stdin);\
         freopen("out","w",stdout); 
    while(cin>>n)
    {
        if(pp(n))
        {
            cout<<"true"<<endl;
        }
        else cout<<"false"<<endl;
    }
 
    return 0;
}
View Code

 

B下楼梯,方式 DP

#include<bits/stdc++.h>
using namespace std;
#define LOACL  freopen("in","r",stdin);\
         freopen("out","w",stdout); 
  
#define FOR(i, a, b)  for(int i=(a); i<(b); i++)
#define REP(i, a, b)  for(int i=(a); i<=(b); i++)
#define DOWN(i, a, b) for(int i=(a); i>=(b); i--)

typedef long long ll; 
typedef double dl; 
ll dp[55];
int n;
int main()
{
    LOACL
    dp[0]=dp[1]=1;
    REP(i,2,50)
    {
        dp[i]+=dp[i-1];
        dp[i]+=dp[i-2];
    }
    while(cin>>n)
    {
        cout<<dp[n]<<endl;
    }


    return 0;
}
View Code

 

C.二分查数字,数字是 x+x/10 +x/100

#include<bits/stdc++.h>
using namespace std;
#define LOACL  freopen("in","r",stdin);\
         freopen("out","w",stdout); 
 
const int   inf = 987654321;
const int    sz = (int)1e6 + 5;
const int   mod = (int)1e9 + 7;
const int sqrtn = 300; 

//#define add(u,v,w) (e[++tot]=(edge){v,head[u],1},head[u]=tot;) 
#define CLR(arr,val) memset(arr,val,sizeof(arr)) 
#define DBG(x) cout<<(#x)<<"="<<x<<endl
#define DBG2(x,y) cout<<(#x)<<"="<<x<<"\t"<<(#y)<<"="<<y<<endl
#define DBG3(x,y,z) cout<<(#x)<<"="<<x<<"\t"<<(#y)<<"="<<y<<"\t"<<(#z)<<"="<<z<<endl

#define FOR(i, a, b)  for(int i=(a); i<(b); i++)
#define REP(i, a, b)  for(int i=(a); i<=(b); i++)
#define DOWN(i, a, b) for(int i=(a); i>=(b); i--)
 
typedef unsigned long long ll; 
typedef double dl; 

ll pp(ll n)
{
    ll tmp = n;
    while(n)
    {
        n/=10;
        tmp+=n;
    }
    return tmp;
}

int main()
{
  //  LOACL
       ll n,t=0;
    cin>>n;
    if(n<10)
    {
        cout<<n<<endl; 
        return 0;
    }
    ll l=1,r = n,mid;
    while(l<r)
    {
        mid = (l+r)/2;
        t = pp(mid); 
        if(t==n) break;
        else if(t<n)l=mid+1;
        else r=mid; 
    }

    if(t==n) cout<<mid<<endl;
    else cout<<-1<<endl; 
    return 0;
}
View Code

 

小伙子,blog 各位大佬保佑我,我明天去霸面腾讯,give么help

 

posted @ 2018-04-10 20:31  pg633  阅读(213)  评论(0编辑  收藏  举报