アットウィキロゴ

KS > practice > Study04-03

  1. package Practice04;
  2.  
  3. import java.util.Scanner;
  4.  
  5. public class Study03 {
  6.  
  7. public static void main(String[] args) {
  8. Scanner scan = new Scanner(System.in);
  9.  
  10. System.out.println("入力");
  11. int n = scan.nextInt();
  12. int[] a = new int[n];
  13.  
  14. int j;
  15. int k = 0;
  16.  
  17. for (int i = 0; i < n ; i++) {
  18.  
  19.  
  20. j = k + 1;
  21. while (j <= n) {
  22.  
  23. if (j % 3 == 0 && j % 7 == 0) {
  24. a[i] = j;
  25. k = j;
  26. break;
  27. } else if (j % 3 == 0) {
  28. a[i] = j;
  29. k = j;
  30. break;
  31. } else if (j % 7 == 0) {
  32. a[i] = j;
  33. k = j;
  34. break;
  35. }
  36. j++;
  37. }
  38. if ( a[i] == 0) {
  39. break;
  40. }
  41. System.out.print(a[i] + ", ");
  42.  
  43. }
  44.  
  45. System.out.println();
  46. int sum = 0;
  47. int a2 = 0;
  48. int a3 = 0;
  49.  
  50. for (int i = 0; i < n; i++) {
  51. sum = a[i] + sum;
  52. if (a[i] % 3 == 0) {
  53. a3 = a[i] + a3;
  54. }
  55. if (a[i] % 2 == 0) {
  56. a2 = a[i] + a2;
  57. }
  58. }
  59. System.out.println(sum);
  60. System.out.println(a3);
  61. System.out.println(a2);
  62.  
  63. }
  64. }
  65.  
最終更新:2013年05月19日 00:04