给定二维空间中的点列表,您希望在Haskell中执行一个函数来找到两个最近点之间的距离.示例:输入:项目[(1,5),(3,4),(2,8),( - 1,2),( - 8.6),(7.0),(1.5),(5.5),(4.8 ),(7.4)]输出:2.0
假设列表中两个最远点之间的距离最多为10000.
这是我的代码:
import Data.List
import System.Random
sort_ :: Ord a => [a] -> [a]
sort_ [] = []
sort_ [x] = [x]
sort_ xs = merge (sort_ left) (sort_ right)
where
(left, right) = splitAt (length xs `div` 2) xs
merge [] xs = xs
merge xs [] = xs
merge (x:xs) (y:ys)=
if x <= y then
x : merge xs (y:ys)
else y : merge (x:xs) ys
project :: [(Float,Float)] -> Float …Run Code Online (Sandbox Code Playgroud) algorithm haskell functional-programming hugs closest-points
我正在制作ACM竞赛的问题,以确定具有无向图G和属于每个组件的顶点的连通组件的数量.已经完成了DFS算法,计算了无向图的连接组件的数量(问题的难点),但我想不出任何东西来指示属于每个组件的节点或者有节点的记录.
输入:第一行输入将是一个整数C,表示测试用例的数量.每个测试用例的第一行包含两个整数N和E,其中N表示图中的节点数,E表示其中的边数.然后遵循E行,每行有2个整数I和J,其中I和J表示节点I和节点J之间存在边(0≤I,J
输出:在每个测试用例的第一行中必须显示以下字符串"Case G:P(s)connected(s)",其中G表示测试用例的数量(从1开始),P表示连接的组件数量在图中.然后是X行,每行包含属于连接组件的节点(按从小到大的顺序)用空格分隔.每个测试用例后应打印一个空行.输出应写在"output.out"中.
例:
输入:
2
6 9
0 1
0 2
1 2
5 4
3 1
2 4
2 5
3 4
3 5
8 7
0 1
2 1
2 0
3 4
4 5
5 3
7 6
Run Code Online (Sandbox Code Playgroud)
输出:
Case 1: 1 component (s) connected (s)
0 1 2 3 4 5
Case 2: 3 component (s) connected (s)
0 1 2
3 4 5
6 7
Run Code Online (Sandbox Code Playgroud)
这是我的代码:
#include <stdio.h>
#include <vector>
#include …Run Code Online (Sandbox Code Playgroud) 我正在使用levenshtein算法来满足这些要求:
当找到N个字符的单词时,在我的字典数据库中建议更正的单词是:
N个字符的每个字典单词,与找到的单词有1个字符差异.示例:找到单词:bearn,dictionary word:bears
N + 1个字符的每个字典单词,其N个字符等于找到的单词.示例:找到的单词:bear,dictionary word:bears
N-1个字符的每个字典单词,其N-1个字符等于找到的单词.示例:找到单词:bears,dictionary word:bear
我在C++中使用Levenshtein算法的这种实现来找到一个单词的Levenshtein数为1(这是所有三种情况下的Levenshtein数),但是我如何选择这个单词来建议呢?我读到了Boyer-Moore-Horspool和Knuth-Morris-Pratt,但我不确定他们中的任何一个是如何有用的.
#include <string>
#include <vector>
#include <algorithm>
using namespace std;
int levenshtein(const string &s1, const string &s2)
{
string::size_type N1 = s1.length();
string::size_type N2 = s2.length();
string::size_type i, j;
vector<int> T(N2+1);
for ( i = 0; i <= N2; i++ )
T[i] = i;
for ( i = 0; i < N1; i++ ) {
T[0] = i+1;
int corner = i;
for ( j = 0; j < N2; …Run Code Online (Sandbox Code Playgroud) 我正在使用jcreatorLE和JDK 1.6来运行我的程序.我不知道为什么在我尝试运行时出现错误.有人可以向我解释一下原因吗?
这是服务器的代码:
import java.io.*;
import java.net.*;
class ServidorTCP {
// variable to wait for connections
private static ServerSocket servidor = null;
// Variable to process client connections
private static Socket conexion = null;
// To send data to the client
private static DataOutputStream salida = null;
// Read the client
private static DataInputStream entrada = null;
public static void main(String args[]) {
// args [0] is the port number to be listened to
int puerto = new Integer(args[0]).intValue();
// …Run Code Online (Sandbox Code Playgroud) 对于图论作业,我要求按照以下格式计算项目的关键路线和时序裕度:
输入:输入的第一行是一个整数 C,它表示测试用例的数量(对项目活动进行建模的图表)。每个测试用例的第一行分别包含两个整数N和M,其中N代表项目中的节点数和活动量M。然后是m行,每行有3个整数I、J和D,其中I和J代表活动的开始和结束节点。
该条目应从程序文件夹中的文件“entrada.in”中读取。如果您的程序提供了通过图形界面从任何路径读取文件的机会(即无需写入完整路径),则可以视为奖励。
输出:
在每个测试用例的第一行必须显示以下字符串“Case G:Duration Total P”,其中G代表测试用例的数量(从1开始),P代表项目总持续时间。然后应在 X 行上表达项目的关键路线的活动,遵循与输入相同的格式(表示持续时间的整数除外),但此外,边是有序(因为第一优先级应该是从低到高的家庭节点,以及从第二低到最高的终端节点)。然后必须遵循与非关键活动相对应的“Y”线,遵循上面列出的相同顺序。对于每个非关键活动,应显示 4 个整数:I、J、T 和 F,其中 T 和 F 分别代表每个活动的总松弛时间和自由松弛时间。此外,如果活动标有红旗,则必须在行尾添加 R。应避免虚拟活动不属于输出的关键路径。
每个测试用例之后应该打印一个空行。输出应写入文件“salida.out”。
例子:

我需要告诉我一些如何做我所需要的事情的想法,我不是在寻求解决方案,只是需要一点帮助(例如伪代码),感谢所有人
我正在用正式语言理论学习图灵机我的课程,教授建议运行以下算法来详细查看"TM"背后的逻辑,但是在尝试编译时不起作用告诉我以下错误.
C:\Documents and Settings\Melkhiah.EQUIPO01\Mis documentos\Downloads\Tarea3 Discretas\TM.c||In function `Tape* insert_tape(Tape*, Direction, char)':|
C:\Documents and Settings\Melkhiah.EQUIPO01\Mis documentos\Downloads\Tarea3 Discretas\TM.c|44|error: invalid conversion from `void*' to `Tape*'|
C:\Documents and Settings\Melkhiah.EQUIPO01\Mis documentos\Downloads\Tarea3 Discretas\TM.c||In function `Tape* create_tape(char*)':|
C:\Documents and Settings\Melkhiah.EQUIPO01\Mis documentos\Downloads\Tarea3 Discretas\TM.c|68|error: invalid conversion from `void*' to `Tape*'|
C:\Documents and Settings\Melkhiah.EQUIPO01\Mis documentos\Downloads\Tarea3 Discretas\TM.c||In function `Transition* get_transition(char*)':|
C:\Documents and Settings\Melkhiah.EQUIPO01\Mis documentos\Downloads\Tarea3 Discretas\TM.c|80|error: invalid conversion from `void*' to `Transition*'|
C:\Documents and Settings\Melkhiah.EQUIPO01\Mis documentos\Downloads\Tarea3 Discretas\TM.c||In function `List* insert_list(List*, char*)':|
C:\Documents and Settings\Melkhiah.EQUIPO01\Mis documentos\Downloads\Tarea3 Discretas\TM.c|93|error: invalid conversion from `void*' …Run Code Online (Sandbox Code Playgroud) 我正在实现一种算法来确定无向图是否为二分图.基于这个伪代码制作了我的实现,它适用于连接的图形,但是当它被断开时,只是程序指示错误的答案.我认为如果它没有连接,那么每个不相交的子图需要一个循环.但我坚持这一点.我如何能够为我打印正确答案的代码?
#include <cstdio>
#include <vector>
#include <queue>
#include <iostream>
using namespace std;
#define MAX 1000
int numberVertex, numberEdges;
int particion[MAX], visited[MAX];
vector< int > adjacencyMatrix[MAX];
bool bfs()
{
int i, origin, destination, begin;
queue< int > queueVertex;
begin = 0;
queueVertex.push(begin);
particion[begin] = 1; // 1 left,
visited[begin] = 1; // set adjacencyMatrixray
while(!queueVertex.empty())
{
origin = queueVertex.front(); queueVertex.pop();
for(i=0; i < adjacencyMatrix[origin].size(); i++)
{
destination = adjacencyMatrix[origin][i];
if(particion[origin] == particion[destination])
{
return false;
}
if(visited[destination] == 0) …Run Code Online (Sandbox Code Playgroud) 我正在阅读"编程挑战:编程竞赛培训手册"一书,正在实现一个问题,我不理解运算符的使用c >> 1和比较if(n&1),有人可以帮助我知道它们的意思?
这是示例代码
#include <stdio.h>
#define MAX_N 300
#define MAX_D 150
long cache[MAX_N/2][2];
void make_cache(int n,int d,int mode)
{
long tmp[MAX_D];
int i,count;
for(i=0;i<MAX_D;i++) tmp[i]=0;
tmp[0]=1;count=0;
while(count<=n)
{
count++;
for(i=(count&1);i<=d;i+=2)
{
if(i)
tmp[i] = tmp[i-1] + tmp[i+1];
else if(!mode)
tmp[0]=tmp[1];
else
tmp[0]=0;
}
if((count&1)==(d&1))
cache[count>>1][mode]=tmp[d];
}
}
int main()
{
int n,d,i;
long sum;
while(1)
{
scanf("%d %d",&n,&d);
if(n&1)
sum=0;
else if(d==1)
sum=1;
else if(n<(d<<1))
sum=0;
else if(n==(d<<1))
sum=1;
else
{
make_cache(n,d,0);
make_cache(n,d,1);
sum=0;
for(i=0;i<=(n>>1);i++)
sum+=cache[i][0]*cache[(n>>1)-i][1];
}
printf("%ld\n",sum);
} …Run Code Online (Sandbox Code Playgroud) 我正在尝试在R中使用rbern函数,但它会显示以下消息
rbern (10, 0.1)
Error: Could not find the "rbern"
Run Code Online (Sandbox Code Playgroud)
我甚至试图加载库(Rlab)并且不会让我,我做错了?我记得几天顺利使用相同的功能.我需要帮助.
library(Rlab)
Error en library(Rlab) : there is no package called ‘Rlab’
Run Code Online (Sandbox Code Playgroud) 我试图学习twitter的bootstrap CSS框架,向用户发出一些警告,我想,在向用户显示警报后,警报会在3秒后消失.我制作这段代码,但不起作用,无法关闭警报,也不明白我做错了什么:
<!DOCTYPE html>
<head>
<title>Alert in boostrap</title>
<link rel="stylesheet" href="http://netdna.bootstrapcdn.com/twitter-bootstrap/2.3.1/css/bootstrap-combined.min.css">
<script type="text/javascript" src="http://netdna.bootstrapcdn.com/twitter-bootstrap/2.3.1/js/bootstrap.min.js"></script>
<script type="text/javascript">
window.setTimeout(function() {
$(".alert-message").fadeTo(500, 0).slideUp(500, function(){
$(this).remove();
});
}, 3000);
</script>
</head>
<body >
<br><br>
<div class="container">
<h1>This is a test</h1>
<div id="alert_message" class="alert" style="width:200px" >
<span class="close" data-dismiss="alert">×</span>
<span><strong>Atention!</strong> Responsive Design with Twitter Bootstrap.</span>
</div>
</div>
</body>
</html>
Run Code Online (Sandbox Code Playgroud)
ps:我是网络编程艺术的新手,如果问题或错误非常明显或愚蠢,请有一点耐心.非常感谢大家
c++ ×3
graph-theory ×3
algorithm ×2
c ×2
backtracking ×1
bipartite ×1
graph ×1
haskell ×1
html5 ×1
hugs ×1
java ×1
javascript ×1
jquery ×1
networking ×1
operators ×1
path ×1
r ×1
sockets ×1
statistics ×1