题目:
求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; }
时间: 2024-10-01 06:07:35