您的位置:首页 > 编程语言 > C语言/C++

北大ACM作业练习 1001

2015-08-02 12:36 316 查看
新手程序员自立开贴做做北大ACM的题目

题目要求:

Description
Problems involving the computation of exact values of very large magnitude and precision are common. For example, the computation of the national debt is a taxing experience for many computer systems.

This problem requires that you write a program to compute the exact value of Rn where R is a real number ( 0.0 < R < 99.999 ) and n is an integer such that 0 < n <= 25.

Input
The input will consist of a set of pairs of values for R and n. The R value will occupy columns 1 through 6, and the n value will be in columns 8 and 9.
Output
The output will consist of one line for each line of input giving the exact value of R^n. Leading zeros should be suppressed in the output. Insignificant trailing zeros must not be printed. Don't print the decimal point if the
result is an integer.
Sample Input
95.123 12
0.4321 20
5.1234 15
6.7592  9
98.999 10
1.0100 12

Sample Output
548815620517731830194541.899025343415715973535967221869852721
.00000005148554641076956121994511276767154838481760200726351203835429763013462401
43992025569.928573701266488041146654993318703707511666295476720493953024
29448126.764121021618164430206909037173276672
90429072743629540498.107596019456651774561044010001
1.126825030131969720661201

Hint
If you don't know how to determine wheather encounted the end of input:

s is a string and n is an integer
C++
while(cin>>s>>n)

{

...

}

c

while(scanf("%s%d",s,&n)==2) //to  see if the scanf read in as many items as you want

/*while(scanf(%s%d",s,&n)!=EOF) //this also work    */

{

...

}


C++

#include <iostream>

#include <vector>

#include <string>

#define ARRAY_SIZE 1000

using namespace std;

int difArray(string Array, int *a);

void priArray(string a);

void priArray(int *a, int b);

void vulTimes(int *a, int *b,int *c);

int piTimes(int a, int b);

int powerBignumber(int *a, int b, int*c,int d);

//FUNCTION MAIN

int main() {

 int intStyle3[ARRAY_SIZE] = { 0 };

 int answerArray2[2 * ARRAY_SIZE] = { 0 };

 int floatL3 = 0;

 int piAnswer = 0, piAnswer2 = 0;

 int pow = 0;

 string input3;

 cout << "Please input a couple of power :" << endl;

 cin >> input3 >> pow;

 floatL3 = difArray(input3, intStyle3);

 piAnswer2 = powerBignumber(intStyle3,pow,answerArray2,floatL3);

 priArray(answerArray2, piAnswer2);

 return 0;

}

int difArray(string Array, int *a) {

 int strSize = Array.size();

 int num = 0;

 int point = 0;

 for (int i = strSize - 1; i != -1; i--) {

  if (Array[i] == '.') {

   i--;

   point = num;

  }

  a[num] = int(Array[i]) - 48 ;

  num++;

 }

 return point;

}

//FUNCTION PRINT

void priArray(int *a, int b) {

 int num = 0,num1 = 0;

 for (int i1 = 0; i1 != ARRAY_SIZE; i1++) {

  if (a[i1] != 0) {

   num1 = i1;

   break;

  }

 }

 for (int i = ARRAY_SIZE - 1; i != -1; --i) {

  if (a[i] != 0) {

   num = i;

   break;

  }

 }

 if (num + 1 <= b) {

  cout << '.';

  for (int j2 = b - num -1 ; j2 != 0; j2--) {

   cout << 0;

  }

  for (int j1 = num; j1 != -1; j1--) {

   cout << a[j1];

  }

 }

 else {

  for (int j = num; j != num1 -1; j--) {

   if (j == b - 1) {

    cout << '.';

   }

   cout << a[j];

  }

  cout << endl;

 }

}

//FUNCTION TIMES

void vulTimes(int *a, int *b,int *c) {

 int cin = 0;

 for (int i = 0; i != ARRAY_SIZE - 1; i++) {

  for (int j = 0; j != ARRAY_SIZE - 1 ; j++) {

   c[i+j] = (b[i] * a[j]) + c[i+j] + cin;

   if (c[i + j] / 10 != 0) {

    cin = c[i + j] / 10;

    c[i + j] = c[i + j] % 10;

   }

   else

    cin = 0;

  }

 }

}

int piTimes(int a, int b) {

 int sum = a + b;

 return sum;

}

//FUNCTION POW

int powerBignumber(int *a, int b, int*c,int d) {

 int Temp1[ARRAY_SIZE] = { 0 };

 for (int j = 0; j != ARRAY_SIZE -1 ; j++) {

  Temp1[j] = a[j];

 }

 for (int i = 1; i != b ; i++) {

  if (b == 0) { ; }

  else {

   for (int j3 = 0; j3 != ARRAY_SIZE - 1; j3++) {

    c[j3] = 0;

   }

   vulTimes(a,Temp1, c);

   for (int j2 = 0; j2 != ARRAY_SIZE - 1; j2++) {

    Temp1[j2] = c[j2];

   }

  }

 }

 return d*b;

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  C++ ACM