помогите понять
не получается цифровые данные из отдельного текстового файла считать в массив в программу и работать с ними
|
Цитата:
Как вариант, сделать ajax-запрос этого файла. А потом var str='1 2 3 2 3 4 5 1 4 6'; alert(str.split(' ')); |
Цитата:
Если в файле "m.txt" находится javascript-код, то стоит изменить ему расширение на "js". Как вы посчитали ответ для вашего примера входящих данных? |
Цитата:
Цитата:
|
Цитата:
Цитата:
1 2 3 2 3 4... Но вода останется только в одной луже 1 2 3 2 3 4 И там будет 1 единица воды. |
ksa, спасибо :)
|
Цитата:
<!DOCTYPE html> <html> <head> <meta http-equiv='Content-Type' content='text/html; charset=windows-1251' /> <script src='https://code.jquery.com/jquery-latest.js'></script> <!-- <script src="https://code.angularjs.org/1.3.9/angular.min.js"></script> <script src="https://code.angularjs.org/1.3.9/angular-route.js"></script> --> <style type='text/css'> </style> <script type='text/javascript'> $(function(){ $.ajax({ url: "tmp1.csp", success: function(Data){ var h=Data.match(/\d+/g); var a=h.slice(1); h=h[0]; alert('Высота волны - '+h); alert('Рельеф - '+a); } }); }); </script> </head> <body> </body> </html> Файл tmp1.csp 4 1 2 3 2 3 4 5 1 4 6 |
Цитата:
function getCount(array, h) { var max = 0, sum = 0; for (var i = 0; i < array.length; ++i) { var item = array[i]; if (item >= h) { break; } sum += Math.max(0, max - item); max = Math.max(max, item); } return sum; } alert(getCount([1, 2, 3, 2, 3, 4, 5, 1, 4, 6], 4)); alert(getCount([1, 2, 3, 2, 3, 4, 5, 1, 4, 6], 6)); |
Мое решение (можно значительно упростить):
const wave=5; const relief='1 2 4 2 3 4 5 1 4 6'.split(' '); const puddles=[]; let puddle=[]; for(let i=0;i<relief.length;i++){ const prevHeight=!puddle.length?(relief[i-1]||0):puddle[0]; const height=relief[i]; if(wave<=height) break; if(!puddle && prevHeight<=height) continue; if(prevHeight>height) (!puddle.length?(puddle=[prevHeight]):puddle).push(height); if(puddle[0]<=height){ puddles.push(puddle); puddle=[]; } } const result=puddles.map(puddle=>{ const max=+puddle.shift(); return puddle.reduce((r,h)=>r+max-h,0); }).reduce((r,v)=>r+v,0); alert(result); |
Alexandroppolus, После вашего решения свое публиковать стыдно :D
|
Часовой пояс GMT +3, время: 15:32. |