因此,我试图用一种方法来解决分而治之的原则和多次递归调用。可以,但是我编写的方法的输出有问题。
该方法的目的是返回数组中所有连续数字对的总和。我95%的人在那儿,但没有得到我期望的输出,并且多年来一直试图将我撞到桌子上,试图找出原因。
该数组是:
int[] array = { 11, 6, 87, 32, 15, 5, 9, 21 };
,方法是:
public int consecutivePairsSum_DivideAndConquer(int start, int end, int[] array) {
int leftSum;
int rightSum;
int middle = (start + end) / 2;
if (start == middle) {
return array[middle];
} else {
leftSum = array[start] + array[start + 1];
leftSum += consecutivePairsSum_DivideAndConquer(start, middle, array);
}
if (middle == end) {
return array[end];
} else {
rightSum = array[middle] + array[middle+1];
rightSum += consecutivePairsSum_DivideAndConquer(middle+1, end, array);
}
return leftSum + rightSum;
}
这是我的方法调用:
System.out.println(rF.consecutivePairsSum_DivideAndConquer(0, array.length-1, array));
我认为这与拆分数组的方式有关,但是没有大量的实验可以为我提供正确的输出。
预期输出:340
实际输出:330
任何欢迎的建议,这真让我发疯! :p
ps任何有用的链接,也可以找到我在其中可以找到可靠的有关递归的可靠在线教程/好书的链接(如果这在SO的范围之内,因为它不了解如何直接帮助解决编程问题)
答案 0 :(得分:2)
以下是算法的概述:
基本情况:如果您的数组少于两个元素,则结果为#include <stdio.h>
#include <unistd.h>
#include <string.h>
#include <stdlib.h>
#include <errno.h>
#include <time.h>
#include <sys/stat.h>
#include <sys/socket.h>
#include <netinet/in.h>
#define BUFFER_SIZE 9999
#define HTTP_METHOD "HTTP/1.1 "
#define HTTP__OK "200 OK\r\n"
#define HTTP__NOT_FOUND "404 Not Found\r\n"
#define SERVER_NAME "Server: ECE435\r\n"
/* Default port to listen on */
#define DEFAULT_PORT 8080 //modify port to listen on 8080
int main(int argc, char **argv) {
int socket_fd,new_socket_fd;
struct sockaddr_in server_addr, client_addr;
int port=DEFAULT_PORT;
int n;
socklen_t client_len;
char buffer[BUFFER_SIZE];
printf("Starting server on port %d\n",port);
/* Open a socket to listen on */
/* AF_INET means an IPv4 connection */
/* SOCK_STREAM means reliable two-way connection (TCP) */
socket_fd = socket(AF_INET, SOCK_STREAM, 0);
if (socket_fd<0) {
fprintf(stderr,"Error opening socket! %s\n",
strerror(errno));
exit(1);
}
/* Set up the server address to listen on */
/* The memset stes the address to 0.0.0.0 which means */
/* listen on any interface. */
memset(&server_addr,0,sizeof(struct sockaddr_in));
server_addr.sin_family=AF_INET;
/* Convert the port we want to network byte order */
server_addr.sin_port=htons(port);
/* Bind to the port */
if (bind(socket_fd, (struct sockaddr *) &server_addr,
sizeof(server_addr)) <0) {
fprintf(stderr,"Error binding! %s\n", strerror(errno));
fprintf(stderr,"Probably in time wait, have to wait 60s if you ^C to close\n");
exit(1);
}
/* Tell the server we want to listen on the port */
/* Second argument is backlog, how many pending connections can */
/* build up */
listen(socket_fd,5);
wait_for_connection:
/* Call accept to create a new file descriptor for an incoming */
/* connection. It takes the oldest one off the queue */
/* We're blocking so it waits here until a connection happens */
client_len=sizeof(client_addr);
new_socket_fd = accept(socket_fd,
(struct sockaddr *)&client_addr,&client_len);
if (new_socket_fd<0) {
fprintf(stderr,"Error accepting! %s\n",strerror(errno));
exit(1);
}
while(1){
/* Someone connected! Let's try to read BUFFER_SIZE-1 bytes */
memset( buffer, 0, BUFFER_SIZE );
n = read( new_socket_fd, buffer, ( BUFFER_SIZE-1 ) );
if (n==0){
fprintf( stderr, "Connection to client lost\n\n" );
break;
}
else if( n < 0 ){
fprintf(stderr,"Error reading from socket %s\n",
strerror(errno));
}
/* Print the message we received */
printf("Message received: %s\n" ,buffer);
const char *PATTERN1 = "GET /"; //first cut to make on buffer
const char *PATTERN2 = " HTTP"; //second cut to make on buffer
char *target = NULL; //variable to hold the slice we're taking
char *start, *end; //defining variables to hold start and end positions
if ( start = strstr( buffer, PATTERN1 ) ){ //code to grab a slice of buffer
start += strlen( PATTERN1 );
if ( end = strstr( start, PATTERN2 ) ){
target = ( char * )malloc( end - start + 1 );
memcpy( target, start, end - start );
target[end - start] = '\0';
}
}
if ( target ) printf( "Client requested: %s\n", target ); //code is working to this point. I can tell what file to get.
time_t rawtime;
struct tm info;
time( &rawtime );
struct tm * timeinfo;
char timestamp[100];
time_t now = time(0);
struct tm tm = *gmtime(&now);
strftime(timestamp, sizeof( timestamp ) , "%a, %d %b %Y %H:%M:%S %Z", &tm);
//printf("Time is: [%s]\n", timestamp);
struct stat file_info; //define statistics structure for file info
stat( target, &file_info ); //initiate file_info as the stat structure for target
char send_client[9999];
sprintf( send_client, "HTTP/1.0 %s%s\r\nServer: ECE435\r\nLast-Modified: Fri, 08 Sep 2017 04:31:47 GMT\r\nContent-Length: 85\r\nContent-Type: text/html\r\n\r\n", HTTP__OK, timestamp );
char file_path[256]; //limited to 256 char for now
sprintf( file_path, "./%s", target); //this is how you can combine char arrays: puts "./" + 'target' into 'file_path'
//int fd;
//printf( "%ld\r\n" , file_info.st_size ); //this should print the File Size
char source[BUFFER_SIZE + 1];
FILE *fp = fopen( file_path, "r");
if (fp != NULL) {
size_t newLen = fread(source, sizeof(char), BUFFER_SIZE, fp);
if (newLen == 0) {
fputs("Error reading file", stderr);
} else {
source[newLen] = '\0'; /* Just to be safe. */
}
fclose(fp);
}
strcat( send_client, source);
/* Send a response */
printf( "\r\n%s\r\n" , send_client ); //print response before sending
n = write( new_socket_fd, send_client , strlen(send_client) ) ;
if( n < 0 ){
fprintf( stderr, "Error writing. %s\n", strerror(errno));
}
}
close(new_socket_fd);
printf("Done connection, go back and wait for another\n\n");
goto wait_for_connection;
/* Try to avoid TIME_WAIT */
// sleep(1);
/* Close the sockets */
close(socket_fd);
return 0;
}
(因为没有对)。
否则:将数组分为两半,计算左,右两半的结果,则整个数组的结果将为0
(因为这里唯一丢失的对是位于
在Java中,将是这样的:
<result of left half> + <result of right half> + <last element of left half> + <first element of right half>
应称为
int consPairSum(int[] array, int left, int right) {
if(right <= left + 1) return 0;
int mid = (left + right) / 2;
int leftPairSum = consPairSum(array, left, mid);
int rightPairSum = consPairSum(array, mid, right);
return leftPairSum + rightPairSum + array[mid - 1] + array[mid];
}
答案 1 :(得分:1)
谁说分而治之需要划分为相等的块,您只需要划分为自身相似的问题。差不多有1班轮
static private int doTheThing(int[] list){
if (list.length==2)
return list[0]+list[1];
return list[0]+list[1]+doTheThing(Arrays.copyOfRange(list,1,list.length));
}