标签:iostream nbsp efi force algo return style ret 字符串
给定一个N,和一个字符串,已知字符串是由不大于N的十进制数拼接而成,从左到右分别代表N^k, N^(k-1)...., N^1, N^0的权数,求这个字符串代表的数的最小值
嗯 注意前导零
1 #include <cmath> 2 #include <cstdio> 3 #include <cstring> 4 #include <iostream> 5 #include <algorithm> 6 #include <queue> 7 #define INF 0x3f3f3f3f 8 using namespace std; 9 typedef long long LL; 10 LL N, M; 11 LL ans; 12 const int maxk = 66; 13 string s; 14 string ss; 15 16 void add(int i, int j) { 17 LL tmp = 0; 18 for (int k = i; k <= j; k++) { 19 tmp = tmp * 10 + s[k] - ‘0‘; 20 } 21 ans = ans + tmp * M; 22 } 23 24 int main() { 25 LL tmp = 0; 26 cin >> ss; 27 int l = ss.length(); 28 for (int i = 0 ; i <= l - 1; i++) { 29 N = N * 10 + ss[i] - ‘0‘; 30 } 31 M = 1; 32 cin >> s; 33 int len = s.length(); 34 for (int i = len - 1; i >= 0; i -= l) { 35 if (i >= l - 1) { 36 int k; 37 if (s.substr(i - l + 1, l) >= ss) { 38 add(i - l + 2, i); 39 k = i - l + 2; 40 } else { 41 add(i - l + 1, i); 42 k = i - l + 1; 43 } 44 while (k <= i && s[k] == ‘0‘) { 45 k++; 46 } 47 if (k == i + 1) { 48 i = i + l - 1; 49 } else { 50 i = k + l - 1; 51 } 52 } else { 53 add(0, i); 54 } 55 M *= N; 56 } 57 printf("%lld\n", ans); 58 return 0; 59 }
Codeforces-758D Ability To Convert
标签:iostream nbsp efi force algo return style ret 字符串
原文地址:http://www.cnblogs.com/xFANx/p/7577501.html