L1-046. 整除光棍(模拟除法)
生活随笔
收集整理的這篇文章主要介紹了
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
總結
以上是生活随笔為你收集整理的L1-046. 整除光棍(模拟除法)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Fibonacci数列时间复杂度之美妙
- 下一篇: 服务器出现 nginx 502 Bad