1001 Reverse Root

本人刚刚开始学习C++,有很多不明白之处,但是单纯看书学习又觉得枯燥。想想还是要 在实践中学习,边做边学,缺什么知识就补什么知道。所以开始OJ做题。以此做个记录。
以下是题目:
1001. Reverse Root
Time limit: 2.0 second Memory limit: 64 MB
The problem is so easy, that the authors were lazy to write a statement for it!
Input
The input stream contains a set of integer numbers Ai (0 ≤ Ai ≤ 1018). The numbers are separated by any number of spaces and line breaks. A size of the input stream does not exceed 256 KB.
Output
For each number Ai from the last one till the first one you should output its square root. Each square root should be printed in a separate line with at least four digits after decimal point.
Sample
input
1427 0

876652098643267843
5276538
output
2297.0716
936297014.1164
0.0000
37.7757

以下是AC的CODE

#include <cstdio>
#include <iostream>
#include <vector>
#include <cmath>
 using namespace std;
int main()
 {
    int i=0,j;
    double a;
    vector<double> x;
    while(cin>>a)
    {
       x.push_back(sqrt(a));
       i++;
    }

    for(j=i-1;j>=0;j--)
     printf("%.4f\n", x[j]);
     return 0;
}

本题一开始没有用VECTOR库,虽然CODEBLOCKS通过了,但是OJ一直报错。Runtime error (access violation),为什么会报错,这个问题还没有解决。留做思考
以下是报错的CODE

#include <iostream>
#include <cstdio>
#include <cmath>

using namespace std;

int main()
{
  double a[3000];
  int i=0 ,j;
  double b;
  while (cin>>a[i])
     ++i;
    for (j=i-1;j>=0;j--)
   {
    b=sqrt(a[j]);
     printf("%.4lf\n",b);
   }
  return 0;
}

猜你喜欢

转载自blog.csdn.net/ggao78/article/details/82657227