加载中...

找规律 (适用于输入为一个简单的数,并且输出简单)

如果是图题 试试找四个角
如果是公式题 试试打出前面几个答案

long long f(long long x){
    if(x==1)return 1;
    return f(x/2)+f(x/2+x%2);
}
signed main(){
    int n;
    for(int i=1;i<100;i++)
    cout<<f(i)<<endl;
    return 0;
}

找到规律,否则需要记忆化搜索


#include<bits/stdc++.h>
using namespace std;
map<long long,long long>mp;
long long f(long long x){
    if(x==1)return 1;
    if(mp[x])return mp[x];//mp[x]不为0,说明之前写过 之前记忆过 直接返回
    return mp[x]=f(x/2)+f(x/2+x%2);
}
int main(){
    long long x;
    cin>>x;
    cout<<f(x);
}

螺旋折线 https://www.acwing.com/problem/content/description/1239/

通过坐标轴 找到规律

#include <iostream>
#include <cstring>
#include <algorithm>
using namespace std;
#define int long long 
typedef long long LL;

signed main(){
    int x,y;cin>>x>>y;
    if(abs(x)<=y){//上边
        int n=y;
        cout << 1LL*(2*n-1)*(2*n)+(x-(-n));
    }
    else if(abs(y)<=x){//右边
        int n=x;
        cout << 1LL*(2*n)*(2*n)+abs(n-y);
    }
    
    else if(abs(y)<=abs(x)){//左边 注意这里和下面不能调换位置
        int n=abs(x);
        cout << 1LL*(2*n-1)*(2*n-1)+( y -(-(n-1)));
        
    }
    else if(abs(x)<=abs(y)+1){//下边
        int n=abs(y);
        cout << 1LL*(2*n)*(2*n+1)+n-x;
    }
    return 0;
}
posted @ 2022-02-08 19:28  liang302  阅读(101)  评论(0编辑  收藏  举报