fork download
  1. #include <iostream>
  2.  
  3. int main(){
  4. int x=10;
  5. int y=10;
  6. int matrix[x][y];
  7. for (int i = 0; i<x; i++) {
  8. for (int j =0; j<y; j++){ // wollte nur sichergehen dass kein Ergebnis durch unbestimmte Speicherplätze gefälscht wird
  9. matrix[i][j]=0;
  10. }
  11. }
  12.  
  13. int cntx,cnty; // 2 Hilfsvariablen zum hoch bzw. runterzählen
  14. int i=1;
  15. cntx=0;
  16. cnty=0;
  17. for(int max=0;max<x;max++){ // da die erste Zahl [0][0] die 2=[0][1] 3.=[01][0]... [0->max][max->0]
  18. cnty=max; // sozusagen y=[max->0] da muss ich ja am anfang auf max sezten
  19. cntx=0;
  20. while(cnty>=0){
  21. matrix[cntx][cnty]=i;
  22. i++;
  23. cntx++;
  24. cnty--;
  25. }
  26. }
  27. //unterer Teil
  28. i=x*x;
  29. cntx=x-1;
  30. cnty=x-1;
  31. for(int min=x-1;min>0;min--){ // da die erste Zahl [0][0] die 2=[0][1] 3.=[01][0]... [0->max][max->0]
  32. cnty=min; // sozusagen y=[max->0] da muss ich ja am anfang auf max sezten
  33. cntx=x-1;
  34. while(cnty<=x-1){
  35. matrix[cntx][cnty]=i;
  36. i--;
  37. cntx--;
  38. cnty++;
  39. }
  40. }
  41.  
  42. //Ausgabe
  43. for (int i = 0; i <x; i++) {
  44. for (int j =0; j<y; j++){
  45. std::cout<<matrix[i][j]<<"\t";
  46. }
  47. std::cout<<"\n";
  48. }
  49. }
  50.  
Success #stdin #stdout 0s 3340KB
stdin
Standard input is empty
stdout
1	2	4	7	11	16	22	29	37	46	
3	5	8	12	17	23	30	38	47	56	
6	9	13	18	24	31	39	48	57	65	
10	14	19	25	32	40	49	58	66	73	
15	20	26	33	41	50	59	67	74	80	
21	27	34	42	51	60	68	75	81	86	
28	35	43	52	61	69	76	82	87	91	
36	44	53	62	70	77	83	88	92	95	
45	54	63	71	78	84	89	93	96	98	
55	64	72	79	85	90	94	97	99	100