L1-046. 整除光棍(模拟除法)

题意:

这里所谓的“光棍”,并不是指单身汪啦~ 说的是全部由1组成的数字,比如1、11、111、1111等。传说任何一个光棍都能被一个不以5结尾的奇数整除。比如,111111就可以被13整除。 现在,你的程序要读入一个整数x,这个整数一定是奇数并且不以5结尾。然后,经过计算,输出两个数字:第一个数字s,表示x乘以s是一个光棍,第二个数字n是这个光棍的位数。这样的解当然不是唯一的,题目要求你输出最小的解。

提示:一个显然的办法是逐渐增加光棍的位数,直到可以整除x为止。但难点在于,s可能是个非常大的数 —— 比如,程序输入31,那么就输出3584229390681和15,因为31乘以3584229390681的结果是111111111111111,一共15个1。

输入格式:

输入在一行中给出一个不以5结尾的正奇数x(< 1000)。

输出格式:

在一行中输出相应的最小的s和n,其间以1个空格分隔。

#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<cctype>
#include<cmath>
#include<iostream>
#include<sstream>
#include<iterator>
#include<algorithm>
#include<string>
#include<vector>
#include<set>
#include<map>
#include<stack>
#include<deque>
#include<queue>
#include<list>
#define lowbit(x) (x & (-x))
const double eps = 1e-8;
inline int dcmp(double a, double b){
    if(fabs(a - b) < eps) return 0;
    return a > b ? 1 : -1;
}
typedef long long LL;
typedef unsigned long long ULL;
const int INT_INF = 0x3f3f3f3f;
const int INT_M_INF = 0x7f7f7f7f;
const LL LL_INF = 0x3f3f3f3f3f3f3f3f;
const LL LL_M_INF = 0x7f7f7f7f7f7f7f7f;
const int dr[] = {0, 0, -1, 1, -1, -1, 1, 1};
const int dc[] = {-1, 1, 0, 0, -1, 1, -1, 1};
const int MOD = 1e9 + 7;
const double pi = acos(-1.0);
const int MAXN = 20 + 10;
const int MAXT = 10000 + 10;
using namespace std;
int main(){
    string xx;
    cin >> xx;
    int len = xx.size();
    int x = 0;
    for(int i = 0; i < len; ++i){
        x = x * 10 + xx[i] - '0';
    }
    int cnt = 0;
    string s;
    string ans;
    while(1){
        ans = "";
        ++cnt;
        s += "1";
        if(cnt < len) continue;
        int sum = 0;
        for(int i = 0; i < len; ++i){
            sum = sum * 10 + 1;
        }
        int st = len;
        if(x > sum){
            sum = sum * 10 + 1;
            ++st;
        }
        if(cnt < st) continue;
        while(st <= cnt){
            int shang = sum / x;
            ans += shang + '0';
            sum %= x;
            if(st == cnt) break;
            ++st;
            sum = sum * 10 + 1;
        }
        if(sum == 0){
            break;
        }
    }
    cout << ans << " " << cnt << endl;
    return 0;
}

  

原文地址:https://www.cnblogs.com/tyty-Somnuspoppy/p/8653641.html