Last active
December 16, 2015 22:23
-
-
Save ckob/97b3594eaa0d923345fb to your computer and use it in GitHub Desktop.
Solucions examen bucles
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| public static String figuraSensePrint(int n1,int n2){ | |
| String s=""; // amb StringBuffer o StringBuilder millor | |
| s="\n\nFigura "+n1+" "+n2+":\n\n"; | |
| for (int i=0;i<n1;i++){ | |
| s+="@\n"; | |
| for (int j=0;j<i;j=j+1){ | |
| s+="+"; | |
| } | |
| for (int k=0;k<n2;k=k+2){ | |
| s+="*"; | |
| } | |
| if (i==n2/2-1){ | |
| int topeSeqNros=Math.max(n1,n2); | |
| for (int l=0;l<topeSeqNros;l=l+1){ | |
| s+="_"+l%10; | |
| } | |
| s+="_m_x_"; | |
| } | |
| } | |
| return s; | |
| } | |
| return false; | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /** | |
| * | |
| * @param num positiu | |
| * @param posicio 0 == unitats, 1==decenes, ... | |
| * @return la xifra corresponent | |
| * -posicio si posició invàlida | |
| * | |
| */ | |
| public static int xifraDeNumeroPosicio(int num,int posicio){ | |
| int i=0; | |
| while(num>0){ | |
| if (i==posicio){ | |
| return num%10; | |
| } | |
| num=num/10; | |
| i++; | |
| } | |
| return -posicio; | |
| } | |
| /** NO s'utilitza | |
| * | |
| * @param n positiu | |
| * @return la llargada del número | |
| */ | |
| public static int llargadaDeNumero(int n){ | |
| int llargada=0; | |
| while(n>0){ | |
| n=n/10; | |
| llargada++; | |
| } | |
| return llargada; | |
| } | |
| /** | |
| retorna true si el nombre passat com a argument contè una sèrie | |
| capicua de llargada al menys 3 a l'interior | |
| exemple | |
| 123456789 no | |
| 121345678 sí 121 | |
| 123456578 sí 565 | |
| 123456547 sí 45654 | |
| 123456657 sí 5665 | |
| n és un número positiu | |
| no cal comprovació | |
| cap print | |
| */ | |
| public static boolean conteUnaSerieCapicuaDeLlargadaAlMenys3(int n){ | |
| int div, numOriginal=n, llargada=llargadaDeNumero(n); | |
| while(n>0){ | |
| div=n%10; | |
| n=n/10; | |
| llargada++; | |
| } | |
| // es podria fer tot en un únic bucle, en realitat no cal obtenir la llargada | |
| n=numOriginal; | |
| // x0 == xifra actual, x1 següent, .. | |
| int x0=xifraDeNumeroPosicio(n, 0); | |
| int x1=xifraDeNumeroPosicio(n, 1); | |
| int x2=xifraDeNumeroPosicio(n, 2); | |
| int x3=xifraDeNumeroPosicio(n, 3); | |
| llargada-=2; | |
| for (int i=0;i<llargada;i++){ | |
| if (x0==x2||x0==x3&&x1==x2){ | |
| return true; | |
| } | |
| x0=x1; | |
| x1=x2; | |
| x2=x3; | |
| x3=xifraDeNumeroPosicio(n, 4+i); | |
| } | |
| return false; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment