1:相似推荐
public class UserCF {
public static void init(String userName) {
Set<String> items = new HashSet();
items.add("A");
items.add("B");
items.add("C");
items.add("D");
items.add("E");
items.add("F");
items.add("G");
items.add("H");
items.add("I");
Map<String, Set<String>> allSet = new HashMap();
Set<String> set1 = new HashSet();
set1.add("p01");
set1.add("p02");
allSet.put("A", set1);
Set<String> set2 = new HashSet();
set2.add("p01");
set2.add("p02");
set2.add("p04");
allSet.put("B", set2);
Set<String> set3 = new HashSet();
set3.add("p01");
set3.add("p02");
set3.add("p03");
allSet.put("C", set3);
Set<String> set4 = new HashSet();
set4.add("p01");
set4.add("p02");
set4.add("p03");
set4.add("p04");
allSet.put("D", set4);
Set<String> set5 = new HashSet();
set5.add("p02");
set5.add("p03");
set5.add("p04");
set5.add("p05");
allSet.put("E", set5);
Set<String> set6 = new HashSet();
set6.add("p03");
set6.add("p04");
set6.add("p05");
allSet.put("F", set6);
Set<String> set7 = new HashSet();
set7.add("p02");
set7.add("p04");
set7.add("p05");
allSet.put("G", set7);
Set<String> set8 = new HashSet();
set8.add("p01");
set8.add("p05");
set8.add("p06");
allSet.put("H", set8);
Set<String> set9 = new HashSet();
set9.add("p01");
set9.add("p02");
set9.add("p03");
set9.add("p04");
set9.add("p05");
set9.add("p06");
allSet.put("I", set9);
Map<String, Set<String>> allUserSet = new HashMap();
Set<String> set01 = new HashSet();
set01.add("A");
set01.add("B");
set01.add("C");
set01.add("D");
set01.add("H");
set01.add("I");
allUserSet.put("p01", set01);
Set<String> set02 = new HashSet();
set02.add("B");
set02.add("C");
set02.add("D");
set02.add("E");
set02.add("G");
set02.add("I");
allUserSet.put("p02", set02);
Set<String> set03 = new HashSet();
set03.add("C");
set03.add("D");
set03.add("E");
set03.add("F");
set03.add("I");
allUserSet.put("p03", set03);
Set<String> set04 = new HashSet();
set04.add("B");
set04.add("D");
set04.add("E");
set04.add("F");
set04.add("G");
set04.add("I");
allUserSet.put("p04", set04);
Set<String> set05 = new HashSet();
set05.add("A");
set05.add("E");
set05.add("F");
set05.add("G");
set05.add("H");
set05.add("I");
allUserSet.put("p05", set05);
Set<String> set06 = new HashSet();
set05.add("H");
set05.add("I");
allUserSet.put("p06", set06);
Set<String> s1 = new HashSet();
Set<String> result = new HashSet();
for (String item : items) {// 遍历每一件物品
Set<String> users = allSet.get(item);
if (!users.contains(userName) && !result.contains(item)) {
for (String user : users) {
s1.clear();
s1.addAll(allUserSet.get(userName));
Set<String> s2 = allUserSet.get(user);
s1.retainAll(s2);
if (s1.size() >= 3) {
result.addAll(allUserSet.get(userName));
result.addAll(s2);
} else {
continue;
}
}
}
if (result.size() == items.size()) {
break;
}
}
result.removeAll(allUserSet.get(userName));
for (String item : result) {
System.out.print(item + ",");
}
System.out.println();
}
public static void main(String[] args) {
init("p03");
init("p04");
init("p06");
}
}
2:最短路劲
/**
- 用户推送
- @author teach
*/
public class ZDJU {
private static int max = 0;
private static int max1 = Integer.MAX_VALUE;
public static void main(String[] args) {
int[][] all = new int[][] { { 0, 4, 6, max, max, max, 8, max, max, max },
{ 4, 0, max, 3, 3, max, max, max, max, max }, { 6, max, 0, 2, max, max, max, max, max, max },
{ max, 3, 2, 0, max, 1, max, max, max, 5 }, { max, 3, max, max, 0, 2, 2, max, 3, max },
{ max, max, max, 1, 2, 0, max, max, max, 3 }, { 8, max, max, max, 2, max, 0, 3, max, max },
{ max, max, max, max, max, max, 3, 0, 1, max }, { max, max, max, max, 3, max, max, 1, 0, 4 },
{ max, max, max, 5, max, 3, max, max, 4, 0 } };
List<Node> allNode = new ArrayList();
allNode.add(new Node(0, "p01", max1, 0));
allNode.add(new Node(1, "p02", max1, 0));
allNode.add(new Node(2, "p03", max1, 0));
allNode.add(new Node(3, "p04", max1, 0));
allNode.add(new Node(4, "p05", max1, 0));
allNode.add(new Node(5, "p06", max1, 0));
allNode.add(new Node(6, "p07", max1, 0));
allNode.add(new Node(7, "p08", max1, 0));
allNode.add(new Node(8, "p09", max1, 0));
allNode.add(new Node(9, "p10", max1, 0));
int d = 0;
for (int i = 0; i < 10; i++) {
for (int j = 0; j < 10; j++) {
// 找到了相连节点
if (all[i][j] != 0) {
// 上一个节点的最短路径的值+与下一个节点相连路径上的值
d = allNode.get(i).value + all[i][j];
// 判断是否比原先的值要小,如果小就将0-j节点的长度替换
if (d < allNode.get(j).value) {
allNode.get(j).value = d;
// 记录前一个节点的序号
allNode.get(j).parent = i;
}
}
}
}
List<Integer> list = new ArrayList();
List<String> list1 = new ArrayList();
Node node = allNode.get(9);
while (node.parent != 0) {
list.add(node.number);
list1.add(node.name);
node = allNode.get(node.parent);
}
list.add(node.number);
list1.add(node.name);
list.add(0);2
list1.add("p01");
Collections.sort(list);
Collections.sort(list1);
System.out.println("路劲为:");
for (String str : list1) {
System.out.print("-->" + str);
}
System.out.println("总距离:");
int sum = 0;
for (int i = 0; i < list.size() - 1; i++) {
sum = sum + all[list.get(i)][list.get(i + 1)];
}
System.out.println(sum);
}
static class Node {
public String name;
public int number;
public int value;
public int parent;
public Node(int number, String n, int v, int p) {
this.name = n;
this.number = number;
this.value = v;
this.parent = p;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getNumber() {
return number;
}
public void setNumber(int number) {
this.number = number;
}
public int getValue() {
return value;
}
public void setValue(int value) {
this.value = value;
}
public int getParent() {
return parent;
}
public void setParent(int parent) {
this.parent = parent;
}
}
}
3:最少基站
public class For4 {
public static void main(String[] args) {
String[] S01 = { "成都", "德阳", "绵阳", "广元" };
String[] S02 = { "雅安", "眉山", "乐山" };
String[] S03 = { "成都", "资阳", "内江", "泸州" };
String[] S04 = { "宜宾", "泸州" };
String[] S05 = { "重庆", "泸州", "内江" };
String[] S06 = { "遂宁", "南充", "广安", "重庆" };
String[] S07 = { "资阳", "遂宁", "内江" };
String[] S08 = { "绵阳", "德阳", "遂宁", "南充" };
String[] baseStationNOArray = { "S01", "S02", "S03", "S04", "S05", "S06", "S07", "S08" };
Set<String> allArea = new HashSet();
List<String> a1 = Arrays.asList(S01);
List<String> a2 = Arrays.asList(S02);
List<String> a3 = Arrays.asList(S03);
List<String> a4 = Arrays.asList(S04);
List<String> a5 = Arrays.asList(S05);
List<String> a6 = Arrays.asList(S06);
List<String> a7 = Arrays.asList(S07);
List<String> a8 = Arrays.asList(S08);
Map<String, List<String>> allsetMap = new HashMap<String, List<String>>();
allsetMap.put("S01", a1);
allsetMap.put("S02", a2);
allsetMap.put("S03", a3);
allsetMap.put("S04", a4);
allsetMap.put("S05", a5);
allsetMap.put("S06", a6);
allsetMap.put("S07", a7);
allsetMap.put("S08", a8);
allArea.addAll(a1);
allArea.addAll(a2);
allArea.addAll(a3);
allArea.addAll(a4);
allArea.addAll(a5);
allArea.addAll(a6);
allArea.addAll(a7);
allArea.addAll(a8);
List<String> all = new ArrayList<String>();
List<String> reStrings = new ArrayList<String>();
for (String s : baseStationNOArray) {
List<String> temp = new ArrayList<String>();
all.clear();
all.addAll(allArea);
List<String> aList = allsetMap.get(s);
all.removeAll(aList);
temp.add(s);
while (!all.isEmpty()) {
String tempString="ddd";
int tempInt=0;;
for (String s1 : baseStationNOArray) {
if (!temp.contains(s1)) {
aList = allsetMap.get(s1);
if(aList.size()>tempInt) {
tempInt=aList.size();
tempString = s1;
}
}
}
if (all.isEmpty()) {
break;
}
temp.add(tempString);
all.removeAll(allsetMap.get(tempString));
}
if (all.isEmpty() && (reStrings.isEmpty() || reStrings.size() > temp.size())) {
reStrings.clear();
reStrings.addAll(temp);
}
}
System.out.println(reStrings);
}
}
4:简单加密解密
public class For5 {
public static String sgin=“###”;
public static void main(String[] args) {
jiami(“d://test.txt”,“d://test1.txt”);
jiemi(“d://test1.txt”,“d://test2.txt”);
}
public static void jiemi(String path1,String path2) {
BufferedReader bufferedReader = null;
BufferedWriter bufferedWriter = null;
try {
bufferedReader = new BufferedReader(new FileReader(path1));
bufferedWriter = new BufferedWriter(new FileWriter(path2));
String lineString = "";
while ((lineString = bufferedReader.readLine()) != null) {
if(lineString.endsWith(sgin)) {
lineString = lineString.substring(0,lineString.length()-sgin.length());
}
int begin = 0;
int end = 2;
while (begin <= lineString.length()) {
if (begin + end > lineString.length()) {
end = lineString.length() - begin;
}
bufferedWriter.write(lineString, begin, end);
begin=begin+2+sgin.length();
bufferedWriter.flush();
}
bufferedWriter.newLine();
}
} catch (Exception e) {
try {
bufferedReader.close();
bufferedWriter.close();
} catch (IOException e1) {
e1.printStackTrace();
}
e.printStackTrace();
}
}
public static void jiami(String path1,String path2) {
BufferedReader bufferedReader = null;
BufferedWriter bufferedWriter = null;
try {
bufferedReader = new BufferedReader(new FileReader(path1));
bufferedWriter = new BufferedWriter(new FileWriter(path2));
String lineString = "";
while ((lineString = bufferedReader.readLine()) != null) {
int begin = 0;
int end = 2;
while (begin <= lineString.length()) {
if (begin + end > lineString.length()) {
end = lineString.length() - begin;
}
bufferedWriter.write(lineString, begin, end);
bufferedWriter.write(sgin);
begin=begin+2;
bufferedWriter.flush();
}
bufferedWriter.newLine();
}
} catch (Exception e) {
try {
bufferedReader.close();
bufferedWriter.close();
} catch (IOException e1) {
e1.printStackTrace();
}
e.printStackTrace();
}
}
}
5 des加密
public class For7 {
public static String sgin=“###”;
public static void main(String[] args) {
jiami(“d://test.txt”,“d://test1.txt”);
jiemi(“d://test1.txt”,“d://test2.txt”);
}
public static void jiemi(String path1,String path2) {
}
static String string = "这是一段需要加密的数据";
public static void jiami(String path1,String path2) {
try {
DESKeySpec desKeySpec = new DESKeySpec("cdszgh2020".getBytes("UTF-8"));
SecretKeyFactory secretKeyFactory = SecretKeyFactory.getInstance("DES");
Key convertSecretKey = secretKeyFactory.generateSecret(desKeySpec);
// 加密
Cipher cipher = Cipher.getInstance("DES/ECB/PKCS5Padding");
cipher.init(Cipher.ENCRYPT_MODE, convertSecretKey);
byte[] result = cipher.doFinal(string.getBytes());
System.out.println("jdk des encrypt:" + new String(result));
// 解密
cipher.init(Cipher.DECRYPT_MODE, convertSecretKey);
result = cipher.doFinal(result);
System.out.println("jdk des decrypt:" + new String(result));
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}