【问题标题】:how is this cpp code running without a main function?这个 cpp 代码如何在没有 main 函数的情况下运行?
【发布时间】:2016-07-21 07:15:06
【问题描述】:

zig_zag提交的topcoder问题BearNSWE

#include <set>
#include <map>
#include <stack>
#include <queue>
#include <cmath>
#include <ctime>
#include <vector>
#include <cstdio>
#include <string>
#include <climits>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <algorithm>

using namespace std;

double xx, yy;

class BearNSWE
{
public:
    double totalDistance(vector <int> a, string dir)//no main function
  {
    int n = a.size();
    double ans = 0.0;
    xx = yy = 0.0;
    for (int i = 0; i < n; i++)
    {
      if (dir[i] == 'N')
      {
        yy = yy + a[i];
      }
      else if (dir[i] == 'S')
      {
        yy = yy - a[i];
      }
      else if (dir[i] == 'W')
      {
        xx = xx - a[i];
      }
      else if (dir[i] == 'E')
      {
        xx = xx + a[i];
      }
      ans = ans + a[i];
    }
    return ans + sqrt(xx*xx + yy*yy);
  }
};

【问题讨论】:

  • 你编译并链接它看它是否运行?

标签: c++ main


【解决方案1】:

topcoder runner 需要一个具有特定方法接口的类,它在问题陈述中指定。对于这个具体问题,是here。

Topcoder 将使用附加源编译此代码 - 一个运行示例的 main 方法 - 添加。

如果您想在 topcoder 编辑器中本地测试您的代码(或者只是自动生成代码以运行它),有几个可用的插件,例如 ExampleBuilder。

【讨论】:

    【解决方案2】:

    这只是一个带有类定义的 .cpp 文件。 main() 函数位于项目的其他位置。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-06-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多