Salut à tous!
Avez-vous une idée comment écrire une fonction qui affiche la matrice spirale d'un chiffre k entré au clavier?
Exemple:
k=2
4 3
1 2
k=5
21 22 23 24 25
20 7 8 9 10
19 6 1 2 11
18 5 4 3 12
17 16 15 14 13
td{
width:15px;
}
function getSpiraleMatrix(n){
var nbCells = n*n;
var x=Math.floor(n/2);
x-=(x*2==n);
var y=x;
var m=[];
for(var i=0;i';
for(var i=0;i';
for(var j=0;j'+m[i][j]+'';
}
str+='';
}
str+='';
return str;
}
window.onload=function(){
document.getElementsByTagName('form')[0].onsubmit=function(e){
try{
var n=parseInt(document.getElementById('dimension').value, 10);
var m=getSpiraleMatrix(n);
var str=matrixToStr(m);
document.getElementById('output').innerHTML=''+str+'';
}catch(e){
console.log(e);
}
e.preventDefault();
return false;
}
};
Utilisateurs parcourant ce forum : Aucun utilisateur enregistré et 7 invités
Tu pars déja ?
Identification
Pas encore inscrit ?
Ou identifiez-vous :