題目:
求n的k次方,然後將答案用前三位和最後三位表示。
Sample Input
2
123456 1
123456 2
Sample Output
123...456
152...936
分析:
題目中其實有提示,用double來表示n的k次方,double神奇的地方在於能轉化為string類型的字符串。用到了sprintf這個函數。
代碼:
#include <cstdio> #include <iostream> #include <cstring> using namespace std; typedef long long ll; const int INF = 1000000000; #define MAX 200 int n, k; ll power_mod(ll a, ll n, ll mod) { if(n == 0) return 1LL; ll ans = power_mod(a, n/2, mod); ans = ans*ans%mod; if(n%2) ans = ans*a%mod; return ans; } double pow(double a, int n) { if(n == 0) return 1; double ans = pow(a, n/2); ans = ans*ans; if(n%2) ans = ans*a; while( ans > INF ) ans /= INF; return ans; } int main() { // freopen("input.txt", "r", stdin); int caseNum; scanf("%d", &caseNum); while(caseNum--) { scanf("%d %d", &n, &k); double head = pow( (double)n, k ); char str[MAX]; sprintf(str, "%lf", 1000*head); str[3] = '\0'; ll last = power_mod(n, k, 1000); printf("%s...%03lld\n", str, last); } return 0; }