1 //我国古代数学家张邱建在《算经》中出了一道“百钱买百鸡”的问题,题意是这样的: 2 //5文钱可以买一只公鸡,3文钱可以买一只母鸡,1文钱可以买3只雏鸡。现在用100文钱买100只鸡, 3 //那么各有公鸡、母鸡、雏鸡多少只?请编写程序实现。 4 for(int gj=1;gj<=20;gj++) 5 { 6 for(int mj=1;mj<=33;mj++) 7 { 8 for(int cj=1;cj<=300;cj++) 9 { 10 if((gj*5+mj*3+cj/3==100)&&(gj+mj+cj==100)) 11 { 12 System.out.println("公鸡"+gj+"母鸡"+mj+"雏鸡"+cj); 13 14 } 15 } 16 } 17 }
百马百担没有运行出来
1 //这是经典的"百马百担"问题, 2 //有一百匹马,驮一百担货,大马驮3担,中马驮2担,两只小马驮1担,问有大,中,小马各几匹? 3 4 for(int dm=1;dm<=33;dm++) 5 { 6 for(int zm=1;zm<=50;zm++) 7 { 8 for(int xm=1;xm<=100;xm++) 9 { 10 if((dm*3+zm*2+xm*2==100)&&(dm+zm+xm==100)) 11 { 12 System.out.println("大马"+dm+"中马"+zm+"小马"+xm); 13 14 } 15 } 16 } 17 }
1 //控制台输出九九乘法表 2 /*for(int d=1;d<=9;d++) 3 { 4 for(int e=1;e<=d;e++) 5 { 6 System.out.print(d+"*"+e+"="+(d*e)+" "); 7 } 8 System.out.println(); 9 } 10 11
1 //控制台输出九九乘法表:方法二 2 int c = 10; 3 for(int a = 9; a >= 1; a--) 4 { 5 for(int b = 9; b >= a; b--) 6 { 7 System.out.print((c-b)+"*"+(c-a)+"="+((c-a)*(c-b))+" "); 8 } 9 System.out.println();*/ 10 }
1 Scanner sc=new Scanner(System.in); 2 System.out.println("请输入三角形的行数"); 3 int n=sc.nextInt(); 4 //左上 5 for (int i = 1; i <=n; i++) 6 { 7 for (int j = 1; j <=i; j++) 8 { 9 System.out.print("★"); 10 } 11 System.out.println(""); 12 } 13 //右上 14 for (int i = 1; i <= n; i++) 15 { 16 for (int k = 1; k <=n-i; k++) 17 { 18 System.out.print(" "); 19 } 20 for (int j = 1; j <= i; j++) 21 { 22 System.out.print("★"); 23 } 24 System.out.println(""); 25 } 26 //左下 27 for (int i = 1; i <= n; i++) 28 { 29 for (int j = 1; j <=n-i+1; j++) 30 { 31 System.out.print("★"); 32 } 33 System.out.println(""); 34 } 35 //右下 36 for (int i = 1; i <= n; i++) 37 { 38 for (int k = 1; k <=i-1; k++) 39 { 40 System.out.print(" "); 41 } 42 for (int j = 1; j <= n - i + 1; j++) 43 { 44 System.out.print("★"); 45 } 46 System.out.println(""); 47 } 48 //上 49 for (int i = 1; i <= n-1; i++) 50 { 51 for (int k = 1; k <= n - i; k++) 52 { 53 System.out.print(" "); 54 } 55 for (int j = 1; j <= 2*i-1; j++) 56 { 57 System.out.print("★"); 58 } 59 System.out.println(""); 60 } 61 //下 62 for (int i = 1; i <= n; i++) 63 { 64 for (int k = 1; k <= i-1; k++) 65 { 66 System.out.print(" "); 67 } 68 for (int j = 1; j <= 2*(n-i+1)-1; j++) 69 { 70 System.out.print("★"); 71 } 72 System.out.println(""); 73 } 74
没有运行出来
1 //实现冒泡排序 2 Scanner sc=new Scanner(System.in); 3 int n = sc.nextInt(); 4 int[] arr = new int[n];{ 5 for (int i = 0; i < arr.length; i++) 6 { 7 System.out.println("请输入第" + (i + 1) + "个人的身高"); 8 9 } 10 for (int j = 0; j < arr.length-1 ; j++) 11 { 12 for (int i = j+1; i <arr.length; i++) 13 { 14 if (arr[j] < arr[i]) 15 { 16 int z = arr[i]; 17 arr[i] = arr[j]; 18 arr[j] = z; 19 } 20 } 21 } 22 for (int i = 0; i <arr.length ; i++) 23 { 24 System.out.println(arr[i]+" "); 25 }