我无法破解这个算法。
问题:JAVA中的WAP打印序列
1
3*2
4*5*6
10*9*8*7
11*12*13*14*15
请帮忙。!!
试试这个:
final int n = 15;
int i = 1;
int cnt = 1;
boolean ltr = true;
while (i < n)
{
String s = "";
if (ltr)
for (int j=0; j<cnt; j++)
s = s + ( s.length() > 0 ? "*" : "") + i++;
else
for (int j=0; j<cnt; j++)
s = i++ + ( s.length() > 0 ? "*" : "") + s;
cnt++;
ltr=!ltr;
System.out.println(s);
}
public static void main(String[] args) throws IOException {
Scanner sc = new Scanner(System.in);
int n,k=0;
n=sc.nextInt();
for(int i=0;i<n;i++)
{
if(i%2==0)
{
for(int y=0;y<i;y++)
{
System.out.print(++k+"*");
}
System.out.print(++k);
}
else
{
int t=k+i+1;
for(int x=0;x<i;x++)
{
System.out.print((t--)+"*");
}
System.out.print(t--);
k=k+i+1;
}
System.out.println();
}
}
import java.util.Scanner;
public class PatternOddEven {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
int n=sc.nextInt();
sc.nextLine();
int k=1,l=1,m=1,x=1;
for(int i=1;i<=n;i++)
{
System.out.println();
if(i%2!=0)
{
for(int j=1;j<=i;j++)
{
System.out.print(l++);
int a=1;
if(a<=j && j!=i)
{
System.out.print("*");
}
}
k=l+m*2;
m++;
l--;
}
else
{
l=k+1;
x=k;
for(int j=1;j<=i;j++)
{
System.out.print(--x);
int a=1;
if(a<=j && j!=i)
{
System.out.print("*");
}
}
l--;
}
}
}
}
1
3*2
4*5*6
10*9*8*7
11*12*13*14*15
.
.
to the nth row...
n = int(input())
k = 1
res = ''
for i in range(n):
if i % 2 == 0:
for j in range(i+1):
res = res + str(k) +"*"
k += 1
print(res[:-1])
res = ''
else:
k += i
for j in range(i+1):
res = res + str(k) +"*"
k -= 1
print(res[:-1])
res = ''
k += i
导入 java.util.Scanner;公共类样本{
/**To print
* 1
* 3*2
* 4*5*6
* 10*9*8*7
* 11*12*13*14*15
*/
public static void main(String[] args) {
Scanner s=new Scanner(System.in);
int b=0,a;
System.out.println("Enter the limit:");
int n=s.nextInt();
for (int i=1;i<=n;i++) {
if (i%2 == 0) {
for(int j=1;j<=i;j++) {
b=b+1;
}
a=b;
for (int j=1;j<=i;j++) {
System.out.print(a--);
if (j != i) {
System.out.print("*");
}
}
System.out.println("");
} else {
for (int k=1;k<=i;k++) {
b=b+1;
System.out.print(b);
if (k != i) {
System.out.print("*");
}
}
System.out.println("");
}
}
}