统计用户购买该商品的季节

package test;

import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

import redis.clients.jedis.Jedis;

public class BuyedTime {
       public static void appendMethod(String fileName, String content) {
           try {
               //打开一个写文件器,构造函数中的第二个参数true表示以追加形式写文件
               FileWriter writer = new FileWriter(fileName, true);
               writer.write(content);
               writer.close();
           } catch (IOException e) {
               e.printStackTrace();
           }
       }
    public static String[] getStrings() {
        FileInputStream fis1;
        InputStreamReader isr1;
        BufferedReader br1 = null;
        try {
            fis1 = new FileInputStream("/public/home/dsj/Public/sundujing/test_items.txt");
            isr1 = new InputStreamReader(fis1, "UTF-8");
            br1 = new BufferedReader(isr1);
        } catch (FileNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (UnsupportedEncodingException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        String[] strings = new String[5462];
        int i=0;
        String str;
        try {
            while ((str = br1.readLine()) != null) {

                strings[i] = str;
                i++;
            }
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return strings;
    }
    public static String[] getStrings2() {
        FileInputStream fis2;
        InputStreamReader isr2;
        BufferedReader br2 = null;
        try {
            fis2 = new FileInputStream("/public/home/dsj/Public/sundujing/user_bought_history.txt");
            isr2 = new InputStreamReader(fis2, "UTF-8");
            br2 = new BufferedReader(isr2);
        } catch (FileNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (UnsupportedEncodingException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        String[] strings = new String[13611038];
        int i=0;
        String str;
        try {
            while ((str = br2.readLine()) != null) {

                strings[i] = str;
                i++;
            }
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return strings;
    }
     /** 
     * * 二分查找算法 * * 
     *  
     * @param srcArray 
     *            有序数组 * 
     * @param des 
     *            查找元素 * 
     * @return des的数组下标,没找到返回-1 
     */     
   public static int binarySearch1(int[] srcArray, int des){   

       int low = 0;   
       int high = srcArray.length-1;   
       while(low <= high) {   
           int middle = (low + high)/2;   
           if(des == srcArray[middle]) {   
               return middle;   
           }else if(des <srcArray[middle]) {   
               high = middle - 1;   
           }else {   
               low = middle + 1;   
           }  
       }  
       return -1;  
  }
    public static void main(String[] args)
    {
        String[] strings = getStrings();//strings[i] test_item_id
        String[] strings1=getStrings2();//usr bought history
        int[] a=new int[9151109];
        String[] a1=new String[9151109];
        int[] b=new int[4459929];
        String[] b1=new String[4459929];
        for(int j=0;j<9151109;j++)
        {
            String[] str2 = strings1[j].split(" ");
                a[j]=Integer.parseInt(str2[1]);//used for search
            a1[j]=str2[2];//used for print
        }
        for(int k=0;k<4459929;k++)
        {
            String[] str3 = strings1[k+9151109].split(" ");
            b[k]=Integer.parseInt(str3[1]);
            b1[k]=str3[2];
        }

        String fileName = "/public/home/dsj/Public/sundujing/buyedTime.txt";
        String content;
        for(int i=0;i<5462;i++)
        {
            boolean flag=false;
            int count1=0,count2=0,count3=0,count4=0;
            int des=Integer.parseInt(strings[i]);
            int index=binarySearch1(a, des);
            int index1=binarySearch1(b, des);
                if(index==-1&&index1==-1)
                {
                    appendMethod(fileName, "\n");
                }else if(index!=-1)
                {
                    //strings1[index]up+down
                    for(int t=index;flag==false;t--)
                    {
                        if(a[t]==des)
                        {
                            if(a1[t].charAt(4)=='0'&&(a1[t].charAt(5)=='2'||a1[t].charAt(5)=='3'||a1[t].charAt(5)=='3'))//2-4
                            {count1++;}else if(a1[t].charAt(4)=='0'&&(a1[t].charAt(5)=='5'||a1[t].charAt(5)=='6'||a1[t].charAt(5)=='7'))//5-7
                            {count2++;}else if((a1[t].charAt(4)=='0'&&(a1[t].charAt(5)=='8'||a1[t].charAt(5)=='9'))||(a1[t].charAt(4)=='1'&&a1[t].charAt(5)=='0'))//8-10
                            {count3++;}else if((a1[t].charAt(4)=='1'&&(a1[t].charAt(5)=='1'||a1[t].charAt(5)=='2'))||(a1[t].charAt(4)=='0'&&a1[t].charAt(5)=='1'))//11-1
                            {count4++;}

                        }else
                        {
                            flag=true;
                        }
                    }
                    flag=false;
                    for(int t=index+1;flag==false;t++)
                    {
                        if(a[t]==des)
                        {
                            if(a1[t].charAt(4)=='0'&&(a1[t].charAt(5)=='2'||a1[t].charAt(5)=='3'||a1[t].charAt(5)=='3'))//2-4
                            {count1++;}else if(a1[t].charAt(4)=='0'&&(a1[t].charAt(5)=='5'||a1[t].charAt(5)=='6'||a1[t].charAt(5)=='7'))//5-7
                            {count2++;}else if((a1[t].charAt(4)=='0'&&(a1[t].charAt(5)=='8'||a1[t].charAt(5)=='9'))||(a1[t].charAt(4)=='1'&&a1[t].charAt(5)=='0'))//8-10
                            {count3++;}else if((a1[t].charAt(4)=='1'&&(a1[t].charAt(5)=='1'||a1[t].charAt(5)=='2'))||(a1[t].charAt(4)=='0'&&a1[t].charAt(5)=='1'))//11-1
                            {count4++;}
                        }else
                        {
                            flag=true;
                        }
                    }
                    flag=false;
                    int[] a2=new int[4];
                    int[] b2=new int[4];
                    a2[0]=count1;a2[1]=count2;a2[2]=count3;a2[3]=count4;b2[0]=1;b2[1]=2;b2[2]=3;b2[3]=4;
                     int temp,temp2;
                     temp2=5;
                    for (int j1 =1; j1 <4; j1++)
                        {  
                            if(a2[0] < a2[j1])
                            {  

                                temp = a2[j1];  
                                a2[j1] = a2[0];  
                                a2[0] = temp;  
                                temp2 = b2[j1];  
                                b2[j1] = b2[0];  
                                b2[0] = temp2;  

                            }  
                        }
                    if(a2[0]!=0)
                    {
                        content=b2[0]+"";
                        appendMethod(fileName,content);
                    }
                    appendMethod(fileName, "\n");
                }else if(index1!=-1)
                {
                    //strings1[index1+9151109]up+down
                    for(int t=index1;flag==false;t--)
                    {
                        if(b[t]==des)
                        {
                            if(b1[t].charAt(4)=='0'&&(b1[t].charAt(5)=='2'||b1[t].charAt(5)=='3'||b1[t].charAt(5)=='3'))//2-4
                            {count1++;}else if(b1[t].charAt(4)=='0'&&(b1[t].charAt(5)=='5'||b1[t].charAt(5)=='6'||b1[t].charAt(5)=='7'))//5-7
                            {count2++;}else if((b1[t].charAt(4)=='0'&&(b1[t].charAt(5)=='8'||b1[t].charAt(5)=='9'))||(b1[t].charAt(4)=='1'&&b1[t].charAt(5)=='0'))//8-10
                            {count3++;}else if((b1[t].charAt(4)=='1'&&(b1[t].charAt(5)=='1'||b1[t].charAt(5)=='2'))||(b1[t].charAt(4)=='0'&&b1[t].charAt(5)=='1'))//11-1
                            {count4++;}
                        }else
                        {
                            flag=true;
                        }
                    }
                    flag=false;
                    for(int t=index1+1;flag==false;t++)
                    {
                        if(b[t]==des)
                        {
                            if(b1[t].charAt(4)=='0'&&(b1[t].charAt(5)=='2'||b1[t].charAt(5)=='3'||b1[t].charAt(5)=='3'))//2-4
                            {count1++;}else if(b1[t].charAt(4)=='0'&&(b1[t].charAt(5)=='5'||b1[t].charAt(5)=='6'||b1[t].charAt(5)=='7'))//5-7
                            {count2++;}else if((b1[t].charAt(4)=='0'&&(b1[t].charAt(5)=='8'||b1[t].charAt(5)=='9'))||(b1[t].charAt(4)=='1'&&b1[t].charAt(5)=='0'))//8-10
                            {count3++;}else if((b1[t].charAt(4)=='1'&&(b1[t].charAt(5)=='1'||b1[t].charAt(5)=='2'))||(b1[t].charAt(4)=='0'&&b1[t].charAt(5)=='1'))//11-1
                            {count4++;}
                        }else
                        {
                            flag=true;
                        }
                    }
                    flag=false;
                    int[] a2=new int[4];
                    int[] b2=new int[4];
                    a2[0]=count1;a2[1]=count2;a2[2]=count3;a2[3]=count4;b2[0]=1;b2[1]=2;b2[2]=3;b2[3]=4;
                     int temp,temp2;
                     temp2=5;
                    for (int j1 =1; j1 <4; j1++)
                        {  
                            if(a2[0] < a2[j1])
                            {  

                                temp = a2[j1];  
                                a2[j1] = a2[0];  
                                a2[0] = temp;  
                                temp2 = b2[j1];  
                                b2[j1] = b2[0];  
                                b2[0] = temp2;  

                            }  
                        }
                    if(a2[0]!=0)
                    {
                        content=b2[0]+"";
                        appendMethod(fileName,content);
                    }
                    appendMethod(fileName, "\n");
                }









        }

    //  
    }

}

这里写图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值