DD11. 幂运算
描述
输入描述
多组测试用例,请参考例题的输入处理 输入每行一个浮点数 R 其中0.0 < R <99.999, 一个整数 n 其中0 < n <=25输出描述
输出R的n次方示例1
输入:
95.123 12 0.1 1
输出:
548815620517731830194541.899025343415715973535967221869852721 0.1
C 解法, 执行用时: 2ms, 内存消耗: 232KB, 提交时间: 2018-09-18
#include <stdio.h> #include <string.h> int len; //total length of exponentiation result. int product[126] = {0}; // storing result, at most length 5*25 + 1 = 126 void multiply(int a[], int n) { int i; int carry = 0; // a carry number in multiplying for (i = 0; i < len; i++) { int temp = a[i]*n + carry; a[i] = temp % 10; carry = temp / 10; } while (carry) { a[i++] = carry % 10; carry /= 10; } len = i; } int main(int argc, char* argv[]) { int n; // power n char s[6]; // real number R, at most the length is 6 while (scanf("%s %d", s, &n) != EOF) { int position=0, i=0, num=0, j=0; for (i=0; i<strlen(s); i++) { if (s[i] == '.') { position = (strlen(s) - 1 - i) * n; // calculate decimal point position after R^n } else { num = num*10 + s[i] - 48; // transfer float to integer } } // product calculation product[0]=1; len = 1; for (i = 0; i < n; i++) { multiply(product, num); } // format output if (len <= position) // product is less than 1 { printf("0"); printf("."); // print decimal point for (i=0; i<position-len; i++) { printf("0"); // print zero between decimal point and decimal } j = 0; //while (product[j] == 0) // trim trailing zeros //{ // j++; //} for (i=len-1; i>=j; i--) { printf("%d", product[i]); } } else { j=0; while (product[j]==0 && j<position) // trim trailing zeros { j++; } for (i=len-1; i>=j; i--) { if (i+1 == position) // cause index in C language starts from 0 { printf("."); } printf("%d", product[i]); } } } }
C 解法, 执行用时: 2ms, 内存消耗: 328KB, 提交时间: 2019-05-13
#include <stdio.h> #include <string.h> int len; //total length of exponentiation result. int product[126] = {0}; // storing result, at most length 5*25 + 1 = 126 void multiply(int a[], int n) { int i; int carry = 0; // a carry number in multiplying for (i = 0; i < len; i++) { int temp = a[i]*n + carry; a[i] = temp % 10; carry = temp / 10; } while (carry) { a[i++] = carry % 10; carry /= 10; } len = i; } int main(int argc, char* argv[]) { int n; // power n char s[6]; // real number R, at most the length is 6 while (scanf("%s %d", s, &n) != EOF) { int position=0, i=0, num=0, j=0; for (i=0; i<strlen(s); i++) { if (s[i] == '.') { position = (strlen(s) - 1 - i) * n; // calculate decimal point position after R^n } else { num = num*10 + s[i] - 48; // transfer float to integer } } // product calculation product[0]=1; len = 1; for (i = 0; i < n; i++) { multiply(product, num); } // format output if (len <= position) // product is less than 1 { printf("0"); printf("."); // print decimal point for (i=0; i<position-len; i++) { printf("0"); // print zero between decimal point and decimal } j = 0; //while (product[j] == 0) // trim trailing zeros //{ // j++; //} for (i=len-1; i>=j; i--) { printf("%d", product[i]); } } else { j=0; while (product[j]==0 && j<position) // trim trailing zeros { j++; } for (i=len-1; i>=j; i--) { if (i+1 == position) // cause index in C language starts from 0 { printf("."); } printf("%d", product[i]); } } } }
C 解法, 执行用时: 2ms, 内存消耗: 352KB, 提交时间: 2018-09-07
#include <stdio.h> #include <string.h> int len; //total length of exponentiation result. int product[126] = {0}; // storing result, at most length 5*25 + 1 = 126 void multiply(int a[], int n) { int i; int carry = 0; // a carry number in multiplying for (i = 0; i < len; i++) { int temp = a[i]*n + carry; a[i] = temp % 10; carry = temp / 10; } while (carry) { a[i++] = carry % 10; carry /= 10; } len = i; } int main(int argc, char* argv[]) { int n; // power n char s[6]; // real number R, at most the length is 6 while (scanf("%s %d", s, &n) != EOF) { int position=0, i=0, num=0, j=0; for (i=0; i<strlen(s); i++) { if (s[i] == '.') { position = (strlen(s) - 1 - i) * n; // calculate decimal point position after R^n } else { num = num*10 + s[i] - 48; // transfer float to integer } } // product calculation product[0]=1; len = 1; for (i = 0; i < n; i++) { multiply(product, num); } // format output if (len <= position) // product is less than 1 { printf("0"); printf("."); // print decimal point for (i=0; i<position-len; i++) { printf("0"); // print zero between decimal point and decimal } j = 0; //while (product[j] == 0) // trim trailing zeros //{ // j++; //} for (i=len-1; i>=j; i--) { printf("%d", product[i]); } } else { j=0; while (product[j]==0 && j<position) // trim trailing zeros { j++; } for (i=len-1; i>=j; i--) { if (i+1 == position) // cause index in C language starts from 0 { printf("."); } printf("%d", product[i]); } } printf("\n"); } }
C++ 解法, 执行用时: 2ms, 内存消耗: 352KB, 提交时间: 2018-09-02
#include <iostream> #include <cstring> using namespace std; int getSmallNumBit(string &num){ int count = 0; for(int i = 0; i < num.length(); i++){ if(num[i] == '.'){ count = num.length() - i -1; while(i < num.length()-1){ num[i] = num[i+1]; i++; } } } if(count > 0) num.pop_back(); return count; } string multi(string a, string b){ int smallNumLen = getSmallNumBit(a) + getSmallNumBit(b); int res[1000]; int k = 0;//指示每次操作的起点 int lena = a.length(); int lenb = b.length(); memset(res,0, sizeof(res)); int t = 0;//进位 for(int i = lena - 1; i >= 0; i--){//乘数 t = 0; k = lena -1 - i; for(int j = lenb - 1; j >= 0; j--){ res[k] = res[k] + (a[i]-'0') * (b[j] - '0')+ t; t = res[k] / 10; res[k] = res[k] % 10; k++; } if(t > 0){ res[k] = t; } } if (res[k] == 0){ k--; } int start = 0; int end = k; while(start<end){ swap(res[start],res[end]); end--; start++; } end = k; char resChar[1000]; memset(resChar,0,sizeof(resChar)); int loc = 0; if(smallNumLen > 0){ while(end>=0){ if(loc == smallNumLen){ res[end+1] = '.' - '0'; break; }else{ res[end+1] = res[end]; end--; } loc++; } k++; } for(int i = 0; i <=k ; i++){ resChar[i] = (char)(res[i] + '0'); } return string(resChar); } void delEnd0(string &num){ if(num[num.length()-1] != '0') return; for(int i = num.length()-1; i >= 0; i--){ if(num[i] == '0'){ num.pop_back(); }else{ break; } } } int main(){ // string s = multi("512","2"); // delEnd0(s); // cout<<s<<endl; //freopen("/Users/xiaojian/CLionProjects/Bishi/in.txt","r",stdin); int n; string R; cin>>R>>n; string res = R; if(n==1){ cout<<res<<endl; return 0; } for(int i = 1; i < n; i++){ res = multi(res,R); } delEnd0(res); cout<<res<<endl; return 0; }
C 解法, 执行用时: 2ms, 内存消耗: 364KB, 提交时间: 2019-03-31
#include <stdio.h> #include <string.h> int len; //total length of exponentiation result. int product[126] = {0}; // storing result, at most length 5*25 + 1 = 126 void multiply(int a[], int n) { int i; int carry = 0; // a carry number in multiplying for (i = 0; i < len; i++) { int temp = a[i]*n + carry; a[i] = temp % 10; carry = temp / 10; } while (carry) { a[i++] = carry % 10; carry /= 10; } len = i; } int main(int argc, char* argv[]) { int n; // power n char s[6]; // real number R, at most the length is 6 while (scanf("%s %d", s, &n) != EOF) { int position=0, i=0, num=0, j=0; for (i=0; i<strlen(s); i++) { if (s[i] == '.') { position = (strlen(s) - 1 - i) * n; // calculate decimal point position after R^n } else { num = num*10 + s[i] - 48; // transfer float to integer } } // product calculation product[0]=1; len = 1; for (i = 0; i < n; i++) { multiply(product, num); } // format output if (len <= position) // product is less than 1 { printf("0"); printf("."); // print decimal point for (i=0; i<position-len; i++) { printf("0"); // print zero between decimal point and decimal } j = 0; //while (product[j] == 0) // trim trailing zeros //{ // j++; //} for (i=len-1; i>=j; i--) { printf("%d", product[i]); } } else { j=0; while (product[j]==0 && j<position) // trim trailing zeros { j++; } for (i=len-1; i>=j; i--) { if (i+1 == position) // cause index in C language starts from 0 { printf("."); } printf("%d", product[i]); } } } }