C语言中打印各种三角形
public class Triangle {
1.
2./**
3. * @param args
4. */
5.
6.public static void main(String[] args) {
7. a(); //打印正等腰三角
8. b(); //打印倒等腰三角
9. c(); //打印直边靠左正直角三角
10. d(); //打印直边靠右正直角三角
11. e(); //打印直边靠左倒直角三角
12. f(); //打印直边靠右倒直角三角
13. g(); //打印底边靠左钝角角三角
14. h(); //打印底边靠右钝角角三角
15. }
16. /**
17. * 作用:打印正等腰三角
18. */
19. public static void a(){
20. System.out.println("打印正等腰三角");
21. int i ,j;
22. for(i=1;i<=5;i++){
23. for(j=5;j>i;j--){
24. System.out.print(" ");
25. }
26. for(j=0;j
27. System.out.print("*");
28. }
29. System.out.println();
30. }
31. }
32.
33. /**
34. * 打印倒等腰三角
35. */
36. public static void b(){
37. System.out.println("打印倒等腰三角");
38. int i ,j ;
39. for(i=1;i<=5;i++){
40. for(j=1;j
41. System.out.print(" ");
42. }
43. for(j=10;j>i*2-1;j--){
44. System.out.print("*");
45. }
46. System.out.println();
47. }
48. }
49. /**
50. * 打印直边靠左正直角三角
51. */
52. public static void c(){
53. System.out.println("打印直边靠左正直角三角");
54. int i ,j ;
55. for(i=1;i<=5;i++){
56. for(j=0;j
57. System.out.print("*");
58. }
59. System.out.println();
60. }
61. }
62.
63. /**
64. * 打印直边靠右正直角三角
65. */
66. public static void d(){
67. System.out.println("打印直边靠右正直角三角");
68. int i ,j;
69. for(i=1;i<=5;i++){
70. for(j=5;j>i;j--){
71. System.out.print(" ");
72. }
73. for(j=0;j
74. System.out.print("*");
75. }
76. System.out.println();
77. }
78. }
79. /**
80. * 打印直边靠左倒直角三角
81. */
82. public static void e(){
83. System.out.println("打印直边靠左倒直角三角");
84. int i ,j;
85. for(i=1;i<=5;i++){
86. for(j=5;j>=i;j--){
87. System.out.print("*");
88. }
89. System.out.println();
90. }
91. }
92. /**
93. * 打印直边靠右倒直角三角
94. */
95. public static void f(){
96. System.out.println("打印直边靠右倒直角三角");
97. int i ,j;
98. for(i=1;i<=5;i++){
99. for(j=1;j
100. System.out.print(" ");
101. }
102.for(j=5;j>=i;j--){
103. System.out.print("*");
104. }
105. System.out.println();
106. }
107. }
108./**
109. * 打印底边靠左钝角角三角
110. */
111.public static void g(){
112. System.out.println("打印底边靠左钝角角三角"); 113.int i ,j ;
114.for(i=1;i<=5;i++){
115.for(j=0;j
116. System.out.print("*");
117. }
118. System.out.println();
119. }
120.for(i=1;i<5;i++){
121.for(j=5;j>i;j--){
122. System.out.print("*");
123. }
124. System.out.println();
125. }
126. }
127./**
128. * 打印底边靠右钝角角三角
129. */
130.public static void h(){
131. System.out.print("打印底边靠右钝角角三角");
132.int i,j;
133.for(i=0;i<=5;i++){
134.for(j=5;j>i;j--){
135. System.out.print(" ");
136. }
137.for(j=0;j
138. System.out.print("*");
139. }
140. System.out.println();
141. }
142.for(i=1;i<5;i++){
143.for(j=0;j
144. System.out.print(" ");
145. }
146.for(j=5;j>i;j--){
147. System.out.print("*");
148. }
149. System.out.println();
150. }
151. }
152.}
逆序的各种算法:
1.public class Reverse {
2. public static void main(String[] args) {
3. //定义的字符
4. String s = "华中爱我";
5.
6.
7.//通过String的subString方法
8. int len = s.length();
9. String sub_reverse = "";
10. for (int i = len; i > 0; i--)
11. sub_reverse += s.substring(i - 1, i); //返回子字
符串,开始于i-1结束于i
12. System.out.println("通过substring方法逆
序 : "+sub_reverse);
13.
14.
15.//通过Sring的StringBuffer方法逆序
16. String buffer_reverse;
17. buffer_reverse=(new StringBuffer(s).reverse()).toString
();
18. System.out.println("通过StringBuffer方法逆
序: "+buffer_reverse);
19.
20.
21.//通过数组实现字符串逆序
22. char[] c = s.toCharArray();
23. char[] temp = new char[c.length];
24. for (int i = 0, j = c.length - 1; i < c.length; i++, j-
-) {
25. temp[j] = c[i];
26. }
27. System.out.println("通过数组来逆
序 : "+buffer_reverse);
28.
29.
30.
31.
32. }
33.}