ACM-1001

高精度大数据计算,这道题很拟真,数据问题做了半天。


//
//  main.cpp
//  ACMUsing
//
//  Created by Nsamper on 2017/1/4.
//  Copyright © 2017年 Nsamper. All rights reserved.
//

#include 
#include 
#include 
#include 
#include 

//1001
using namespace std;
string r;
string ans;
int n;
long pointNum;

string eraseZero(string str) {
    long i;
    pointNum = 0;
    long point = str.length();
    while (str[0] == '0') {
        str.erase(str.begin());
    }
    for (i = 0; i < str.length(); i++) {
        if (str[i] == '.') {
            point = i;//小数点的位置
            break;
        }
    }
    if (point == str.length()) {
        return str;
    }
    for (i = str.length()-1; i > point; i--) {
        if (str[i] != '0') {
            break;
        } else {
            str.erase(str.begin()+i);
            point--;
        }
    }
    return str;
}

void removePoint() {
    int i = 0;
    for (i = 0; i < r.length(); i++) {
        if (r[i] == '.') {
            pointNum = r.length() - i - 1;//小数的个数
            r.erase(r.begin()+i);
        }
    }
}

string multify(string a, string b) {
    int i,j;
    int lena,lenb;
    int tmp[1000];
    string tempStr;
    int x,y = 0;
    
    reverse(a.begin(), a.end());
    reverse(b.begin(), b.end());
    
    memset(tmp, 0, sizeof(tmp));
    
    lena = (int)a.length();
    lenb = (int)b.length();
    
    for (i = 0; i < lena; i ++) {
        for (j = 0; j < lenb; j ++) {
            x = a[i]-'0';
            y = b[j]-'0';
            tmp[i + j] += x * y;
        }
    }
    for (i = 0; i < lena + lenb - 1; i ++) {
        x = tmp[i] % 10;
        y = tmp[i] / 10;
        tmp[i] = x;
        tmp[i + 1] += y;
    }
    if (y > 0) {
        i++;
    }
    for (j = 0; j < i; j ++) {
        tempStr += tmp[j]+'0';
    }
    reverse(tempStr.begin(), tempStr.end());
    return tempStr;
}

void work(string a, int len) {
    ans = "1";
    while (len) {
        if (len&1) {
            ans = multify(ans, a);
        }
        a = multify(a, a);
        len >>= 1;
    }
}

void eraseZeroAndPrint(string str) {
    str = eraseZero(str);
    if (str[str.length()-1] == '.') {
        str.erase(str.length()-1);
    }
    cout<> r >> n) {
        str = "";
        r = eraseZero(r);
        removePoint();
        for(i=0;i ans.length()) {
            i = pointNum*n - (int)ans.length();
            str+='.';
            while (i > 0) {
                str+='0';
                i--;
            }
            for (j = 0; j < ans.length(); j ++) {
                str+= ans[j];
            }
        } else {
            for (j = 0; j < ans.length(); j ++) {
                if (j == ans.length() - pointNum*n) {
                    str+='.';
                }
                str+=ans[j];
            }
        }
        eraseZeroAndPrint(str);
    }
    return 0;
}


你可能感兴趣的:(ACM-1001)