P1611题解

· · 个人记录

#include <iostream>
#include <string>
#include <cstring>
#include <map>
using namespace std;
/*
1.移动数位
    a.移动一位怎么处理?
    取余为最后一位,需要乘以一个数用于处理位权问题,每次乘的数都是原来的十分之一
    b.移动到前面一位应该怎么处理?
    之前移动的数字乘以10后给新数字留空位
2.去重
  采用数组存储已有m方式,避免重复

*/
typedef long long LL;
const int N = 2e6+10;
int arr[10];
int cur = 0;
LL ans = 0;
int a,b;
bool check(int n, int m){
    for(int i=1;i<=cur;i++){
        if(arr[i]==m) return false;
    }
    arr[++cur] = m;
    if(n>=a&&n<m&&m<=b) 
        return true;
    return false;
}
void calc(int t){
    int tmp = t;
    //位权1
    int w = 1;
    while(t){
        t/=10;
        w*=10;
    }
    w/=10;
    //位权2
    int pw = 1;
    t = tmp;
    int pre = 0;
    while(t){
        pre = t % 10 * pw + pre;
        t/=10;
        int m = pre*w + t;
        if(check(tmp, m)){
            ans++;  
        } 
        w/=10;
        pw*=10;
    }
}
int main()
{   
    cin>>a>>b;
    for(int i=a;i<=b;i++){
        memset(arr,0,sizeof(arr));
        cur = 0;
        calc(i);
    }
    cout<<ans<<endl;
    return 0;
}