精華區beta CKEFGISC-1st 關於我們 聯絡資訊
嗯.... $@#$@# 的資結老師又出作業了... 題目: a為正整數和負整數所構成的陣列。 contigsum(i,j)定義為由元素 a[i]到a[j]之間的連續元素的和,期中i<=j。請寫個程式來找出另 contigsum(i,j)為極大值i,j。這遞迴應該同時考慮陣列 a中的兩個半部分 ex: 4 8 -23 30 24 -8 19 從 30+24+(-8)+19 會得到最大的解 目前進度: 因為實在想不出來要怎麼用遞迴寫,就想要用暴力迴圈寫 就是 用排列組合的方式 把每種解都算出來 再排序 再算出是第幾個組合 偶現在碰到的問題是 /* #define MAX 10 #include <stdio.h> #include <conio.h> #include <string.h> main () { int array[MAX]; int total[MAX]; int a=0; int b; int i=1; int j; int k; int l=1; int t; int m=0; clrscr(); /*Input numbers*/ printf("How many numbers?"); scanf("%d",&b); printf("Please input the data:\n"); for(a=1;a<=b ;a++) { printf("number %d (like 1 or 5,-2,0,-3):",a); scanf("%d",&array[a]); } for(t=0;t<MAX;t++) /* set total[]=0 */ { total[t]=0; } for(i=1;i<b;i++) /* add from i */ { for(j=2;j<=b;j++) /* add j numbers */ { for(k=i;k-i<j;k++) /* start add */ { total[l]=total[l]+array[k]; } printf("total[%d]=%d\t",l,total[l]); l++; } } } */ 我現在寫到單獨加總的部分 我現在實驗的 test data 是 1 2 3 total[1]=3 total[2]=6 total[3]=5 total[4]=1321 ... 不知道為什麼這邊會跑出這個數目... 請大家幫我 debug 看看吧... 謝謝囉... -- ※ 發信站: 批踢踢實業坊(ptt.twbbs.org) ◆ From: libra.DormL.nsysu.edu.tw