fork download
  1. import java.util.List;
  2. import java.util.ArrayList;
  3. import java.util.Iterator;
  4.  
  5. public class Main {
  6.  
  7. public static void main(String[] args) {
  8. number372();
  9. }
  10.  
  11. public static void number372(){
  12. IFilter<Integer> mod3_is_0 = new IFilter<Integer>(){
  13. public boolean pass(Integer n){ return (n % 3) == 0; }
  14. };
  15. IFilter<Integer> mod7_is_2 = new IFilter<Integer>(){
  16. public boolean pass(Integer n){ return (n % 7) != 2; }
  17. };
  18.  
  19. IListFilter<Integer> flt = new SimpleListFilter<Integer>(
  20. new ChainFilter<Integer>( mod3_is_0, mod7_is_2 ) );
  21.  
  22. IFormatter<List<Integer>> fmt = new ListFormatter<Integer>(
  23. ":", ",", 10 );
  24.  
  25. List<Integer> ls = new SequenceIntegerListGenerator(
  26. 1, 1, 1000
  27. ).generate();
  28.  
  29. List<Integer> modLs = flt.filter(ls);
  30.  
  31. System.out.println(
  32. fmt.format(modLs.subList(0,90)) );
  33. }
  34.  
  35.  
  36. public static interface IFilter<Type> {
  37. boolean pass(Type n);
  38. }
  39.  
  40. public static interface IListFilter<Type> {
  41. List<Type> filter(List<Type> ls);
  42. }
  43.  
  44. public static interface IFormatter<Type> {
  45. String format(Type data);
  46. }
  47.  
  48. public static interface IListGenerator<Type> {
  49. List<Type> generate();
  50. }
  51.  
  52. public static class ChainFilter<Type>
  53. implements IFilter<Type>
  54. {
  55. IFilter[] m_filters;
  56.  
  57. public ChainFilter(IFilter<Type> ... filters ){
  58. m_filters = filters;
  59. }
  60.  
  61. public boolean pass(Type n){
  62. for( int i = 0; i < m_filters.length; i++ ){
  63. if( !m_filters[i].pass(n) ){
  64. return false;
  65. }
  66. }
  67. return true;
  68. }
  69. }
  70.  
  71. public static class SimpleListFilter<Type>
  72. implements IListFilter<Type>
  73. {
  74. IFilter<Type> m_filter;
  75. SimpleListFilter(IFilter<Type> filter){
  76. m_filter = filter;
  77. }
  78. public List<Type> filter(List<Type> ls){
  79. List<Type> res = new ArrayList<Type>();
  80. Iterator<Type> it = ls.iterator();
  81. while(it.hasNext()){
  82. Type n = it.next();
  83. if( this.m_filter.pass(n) ){
  84. res.add(n);
  85. }
  86. }
  87. return res;
  88. }
  89. }
  90. public static class ListUtil<Type> {
  91. public String join(List<Type> ls, String delim){
  92. Iterator<Type> it = ls.iterator();
  93. String s = it.hasNext() ?
  94. it.next().toString():
  95. "";
  96.  
  97. while( it.hasNext() ){
  98. s += delim + it.next();
  99. }
  100. return s;
  101. }
  102. }
  103.  
  104. public static class ListFormatter<Type>
  105. implements IFormatter<List<Type>>
  106. {
  107. int m_nElemsPerLine;
  108. String m_delimLineNum;
  109. String m_delimElem;
  110. ListUtil<Type> m_lutil;
  111. ListFormatter(
  112. String delimLineNum,
  113. String delimElem,
  114. int nElemsPerLine
  115. ){
  116. m_delimLineNum = delimLineNum;
  117. m_delimElem = delimElem;
  118. m_nElemsPerLine = nElemsPerLine;
  119.  
  120. m_lutil = new ListUtil<Type>();
  121. }
  122.  
  123. private static int min( int x, int y ){
  124. return x < y ? x : y;
  125. }
  126.  
  127. public String format(List<Type> ls){
  128. String res = "";
  129. List<Type> subL;
  130. for( int i = 0; (i*m_nElemsPerLine) < ls.size(); i ++ ){
  131. subL = ls.subList(
  132. (i*m_nElemsPerLine),
  133. min( ( (i+1) *m_nElemsPerLine), ls.size() ) );
  134.  
  135. res += (i+1) + m_delimLineNum;
  136.  
  137. res += m_lutil.join(subL,m_delimElem);
  138.  
  139. res += System.lineSeparator();
  140. }
  141. return res;
  142. }
  143. }
  144. public static class SequenceIntegerListGenerator
  145. implements IListGenerator <Integer>
  146. {
  147. ArrayList<Integer> m_list;
  148. SequenceIntegerListGenerator(
  149. int first, int diff, int length
  150. ){
  151. m_list = new ArrayList<Integer>();
  152. for( int i = 0; i < length; i++ ){
  153. m_list.add( first + (i*diff) );
  154. }
  155. }
  156. public List<Integer> generate(){
  157. return (ArrayList)m_list.clone();
  158. }
  159. }
  160. }
Success #stdin #stdout 0.07s 380224KB
stdin
Standard input is empty
stdout
1:3,6,12,15,18,21,24,27,33,36
2:39,42,45,48,54,57,60,63,66,69
3:75,78,81,84,87,90,96,99,102,105
4:108,111,117,120,123,126,129,132,138,141
5:144,147,150,153,159,162,165,168,171,174
6:180,183,186,189,192,195,201,204,207,210
7:213,216,222,225,228,231,234,237,243,246
8:249,252,255,258,264,267,270,273,276,279
9:285,288,291,294,297,300,306,309,312,315