A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

©   /  2013-6-21 12:34  /  13093 人查看  /  171 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

  1. class ZhuanFen
  2. {
  3. public static void main(String[] args)
  4. {
  5. print();
  6. }

  7. /*
  8. * 1.将该数组由小到大进行排列,数组中的素数全部赋值为零,并 按阶梯状输出。
  9. 数组: 2 5 9 10 48 95 154 31 59 69

  10. * */

  11. public static boolean suShu(int i)//定义一个判断是否为素数的方法。
  12. {
  13. int count = 0;
  14. if (i == 1)
  15. return false;
  16. for(int x=1;x<=i;x++)
  17. {
  18. if((i%x)==0)
  19. count++;
  20. }
  21. if(count>2)
  22. return false;
  23. else
  24. return true;

  25. }

  26. public static void print()
  27. {
  28. int []i = {2,5,9,10,48,95,154,31,59,69};//来个int数组存需要处理的数
  29. System.out.print("数组改变前");
  30. for(int x=0;x<i.length-1;x++)
  31. {
  32. System.out.print(i[x]+" ");
  33. }
  34. System.out.println(i[i.length-1]);

  35. int []arr = new int[i.length];//来个新数组存素数变0后的数组。
  36. for(int x=0;x<i.length;x++)//遍历数组并打印。
  37. {

  38. if(suShu(i[x]))//调用suShu方法判断原数组的数是否为素数
  39. arr[x] = 0;
  40. else
  41. arr[x]=i[x];
  42. }
  43. System.out.print("数组改变后");
  44. for(int x=0;x<arr.length-1;x++)
  45. {
  46. System.out.print(arr[x]+" ");
  47. }
  48. System.out.println(arr[arr.length-1]);

  49. for(int x=0;x<arr.length;x++)
  50. {
  51. for(int y=0;y<=x;y++)
  52. {

  53. if(x==y)//为了打印阶梯型。
  54. System.out.println(arr[x]);
  55. else
  56. System.out.print(" ");
  57. }
  58. }
  59. }
  60. }
复制代码


_______________________________________________________________________________________________________________________________________________________________

  1. <P>class ZhuanFen1
  2. {
  3. public static void main(String args[])
  4. {
  5. int N =4;

  6. yanghui(N);
  7. }
  8. /*
  9. * 2.手动输入一个自然数N,反向打印N行杨辉三角。

  10. * */
  11. public static void yanghui( int N)
  12. {

  13. int a[][] = new int[N+1][];//定义一个二维数组。
  14. for(int i=0;i<=N;i++)//初始化二维数组
  15. {
  16. a[i] = new int[i+1];
  17. }

  18. for (int i = 0; i <= N; i++)//循环行数
  19. {

  20. for (int j = 0; j <= a[i].length - 1; j++) //循环列数
  21. {

  22. if (i == 0 || j == 0 || j == a[i].length - 1)

  23. a[i][j] = 1;//将两侧元素设为1

  24. else//元素值为其正上方元素与左上角元素之和

  25. a[i][j] = a[i - 1][j - 1] + a[i - 1][j];
  26. }
  27. }

  28. for (int i=a.length-1-1;i>=0;i--)
  29. {

  30. for(int x =i+1;x<N;x++)
  31. System.out.print(" ");
  32. for (int j = 0; j <=a[i].length - 1; j++)
  33. System.out.print(a[i][j] + " ");

  34. System.out.println();//换行
  35. }

  36. }
  37. }</P>
  38. <P> </P>
复制代码



点评

第一个题还有点小问题,自己重新看下...  发表于 2013-6-21 15:35

评分

参与人数 1技术分 +3 收起 理由
夜默 + 3 第一个题还有点小问题..自己改下...

查看全部评分

回复 使用道具 举报
王靖远 发表于 2013-6-21 15:18
__________________________________________________________________________________________________ ...
  1. class ZhuanFen
  2. {
  3. public static void main(String[] args)
  4. {
  5. print();
  6. }

  7. /*
  8. * 1.将该数组由小到大进行排列,数组中的素数全部赋值为零,并 按阶梯状输出。
  9. 数组: 2 5 9 10 48 95 154 31 59 69

  10. * */

  11. public static boolean suShu(int i)//定义一个判断是否为素数的方法。
  12. {
  13. int count = 0;
  14. if (i == 1)
  15. return false;
  16. for(int x=1;x<=i;x++)
  17. {
  18. if((i%x)==0)
  19. count++;
  20. }
  21. if(count>2)
  22. return false;
  23. else
  24. return true;

  25. }

  26. public static void print()
  27. {
  28. int []i = {2,5,9,10,48,95,154,31,59,69};//来个int数组存需要处理的数
  29. System.out.print("数组改变前");
  30. for(int x=0;x<i.length-1;x++)
  31. {
  32. System.out.print(i[x]+" "+"\t");
  33. }
  34. System.out.println(i[i.length-1]);

  35. int []arr = new int[i.length];//来个新数组存素数变0后的数组。
  36. for(int x=0;x<i.length;x++)//遍历数组并打印。
  37. {

  38. if(suShu(i[x]))//调用suShu方法判断原数组的数是否为素数
  39. arr[x] = 0;
  40. else
  41. arr[x]=i[x];
  42. }
  43. System.out.print("数组改变后");
  44. for(int x=0;x<arr.length-1;x++)
  45. {
  46. System.out.print(arr[x]+" "+"\t");
  47. }
  48. System.out.println(arr[arr.length-1]);


  49. System.out.println("输出数组");
  50. for(int x=0;x<arr.length;x++)
  51. {
  52. for(int y=0;y<=x;y++)
  53. {

  54. if(x==y)//为了打印阶梯型。
  55. System.out.println(arr[x]+"\t");
  56. else
  57. System.out.print(" ");
  58. }
  59. }
  60. }
  61. }
复制代码
加了制表符给改好看点了。

点评

你没觉得69在最下面不合适么  发表于 2013-6-21 15:50
回复 使用道具 举报
王靖远 发表于 2013-6-21 15:48
加了制表符给改好看点了。

哦哦 前面都是按顺序排的,忘掉看需求要排序了。等我改下
回复 使用道具 举报
王靖远 发表于 2013-6-21 15:48
加了制表符给改好看点了。
  1. import java.util.*;
  2. class ZhuanFen
  3. {
  4. public static void main(String[] args)
  5. {
  6. print();
  7. }

  8. /*
  9. * 1.将该数组由小到大进行排列,数组中的素数全部赋值为零,并 按阶梯状输出。
  10. 数组: 2 5 9 10 48 95 154 31 59 69

  11. * */

  12. public static boolean suShu(int i)//定义一个判断是否为素数的方法。
  13. {
  14. int count = 0;
  15. if (i == 1)
  16. return false;
  17. for(int x=1;x<=i;x++)
  18. {
  19. if((i%x)==0)
  20. count++;
  21. }
  22. if(count>2)
  23. return false;
  24. else
  25. return true;

  26. }

  27. public static void print()
  28. {
  29. int []i = {2,5,9,10,48,95,154,31,59,69};//来个int数组存需要处理的数
  30. System.out.print("数组改变前");
  31. for(int x=0;x<i.length-1;x++)
  32. {
  33. System.out.print(i[x]+" "+"\t");
  34. }
  35. System.out.println(i[i.length-1]);
  36. Arrays.sort(i);

  37. int []arr = new int[i.length];//来个新数组存素数变0后的数组。
  38. for(int x=0;x<i.length;x++)//遍历数组并打印。
  39. {

  40. if(suShu(i[x]))//调用suShu方法判断原数组的数是否为素数
  41. arr[x] = 0;
  42. else
  43. arr[x]=i[x];
  44. }
  45. System.out.print("数组改变后");
  46. for(int x=0;x<arr.length-1;x++)
  47. {
  48. System.out.print(arr[x]+" "+"\t");
  49. }
  50. System.out.println(arr[arr.length-1]);


  51. System.out.println("输出数组");
  52. for(int x=0;x<arr.length;x++)
  53. {
  54. for(int y=0;y<=x;y++)
  55. {

  56. if(x==y)//为了打印阶梯型。
  57. System.out.println(arr[x]+"\t");
  58. else
  59. System.out.print(" ");
  60. }
  61. }
  62. }
  63. }
复制代码

评分

参与人数 1技术分 +1 收起 理由
夜默 + 1 重复修改代码,值得鼓励,话说你已经重复发.

查看全部评分

回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马