这个 cpp 代码 运行 没有主要功能怎么办?
how is this cpp code running without a main function?
topcoder 问题 BearNSWE 由 zig_zag
提交
#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);
}
};
这只是一个具有 class 定义的 .cpp 文件。 main() 函数位于项目的其他位置。
topcoder runner 需要一个具有特定方法接口的 class,它在问题陈述中指定。对于这个具体问题,就是here.
Topcoder 将使用附加源编译此代码 - 添加了运行示例的主要方法。
如果您想在 topcoder 编辑器中本地测试您的代码(或者只是为 运行 自动生成代码),有几个可用的插件,例如 ExampleBuilder。
topcoder 问题 BearNSWE 由 zig_zag
提交#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);
}
};
这只是一个具有 class 定义的 .cpp 文件。 main() 函数位于项目的其他位置。
topcoder runner 需要一个具有特定方法接口的 class,它在问题陈述中指定。对于这个具体问题,就是here.
Topcoder 将使用附加源编译此代码 - 添加了运行示例的主要方法。
如果您想在 topcoder 编辑器中本地测试您的代码(或者只是为 运行 自动生成代码),有几个可用的插件,例如 ExampleBuilder。