当前位置:   article > 正文

3.26总结

3.26总结

JAVA学习今天学了字符串相关类的底层原理(简单了解了一下)和集合部分,自己试着做了一些集合的简单练习来加强理解

集合和数组部分功能上有些相似,但是总体存在很大区别

1.长度

数组长度固定,集合长度可以改变

2.内容

数组可以存储基本数据类型和引用数据类型

集合中能存储引用数据类型

3.储存元素类型

数组只能存储一种类型的元素,集合可以存储多种不同类型的元素

构建集合时有很多好用的函数

如:list.add(),list.remove(),list.get(),list.set()等

储存用户信息,实现简单查询功能

  1. package Arraylist;
  2. import java.util.ArrayList;
  3. public class user2 {
  4. public static void main(String[] args) {
  5. ArrayList<user2pople> list=new ArrayList<>();
  6. user2pople u1=new user2pople("h1","zs","123");
  7. user2pople u2=new user2pople("h2","ls","456");
  8. user2pople u3=new user2pople("h3","ww","789");
  9. list.add(u1);
  10. list.add(u2);
  11. list.add(u3);
  12. int index=getindex(list,"hhh");
  13. System.out.println(index);
  14. //boolean flag = contains(list,"hhh");
  15. //System.out.println(flag);
  16. }
  17. /*public static boolean contains(ArrayList<user2pople>list,String id)
  18. {
  19. for(int i=0;i<list.size();i++)
  20. {
  21. user2pople u=list.get(i);
  22. String uid=u.getId();
  23. if(uid.equals(id))
  24. {
  25. return true;
  26. }
  27. }
  28. return false;
  29. }*/
  30. public static int getindex(ArrayList<user2pople>list,String id)
  31. {
  32. for(int i=0;i<list.size();i++)
  33. {
  34. user2pople u=list.get(i);
  35. String uid=u.getId();
  36. if(uid.equals(id))
  37. {
  38. return i;
  39. }
  40. }
  41. return -1;
  42. }
  43. }
  1. package Arraylist;
  2. public class user2pople {
  3. private String id;
  4. private String username;
  5. private String password;
  6. public user2pople() {
  7. }
  8. public user2pople(String id, String username, String password) {
  9. this.id = id;
  10. this.username = username;
  11. this.password = password;
  12. }
  13. public String getId() {
  14. return id;
  15. }
  16. public void setId(String id) {
  17. this.id = id;
  18. }
  19. public String getUsername() {
  20. return username;
  21. }
  22. public void setUsername(String username) {
  23. this.username = username;
  24. }
  25. public String getPassword() {
  26. return password;
  27. }
  28. public void setPassword(String password) {
  29. this.password = password;
  30. }
  31. }

Q - 生日蛋糕

7月17日是Mr.W的生日,ACM-THU为此要制作一个体积为Nπ的M层生日蛋糕,每层都是一个圆柱体。
设从下往上数第i(1 <= i <= M)层蛋糕是半径为Ri, 高度为Hi的圆柱。当i < M时,要求Ri > Ri+1且Hi > Hi+1。
由于要在蛋糕上抹奶油,为尽可能节约经费,我们希望蛋糕外表面(最下一层的下底面除外)的面积Q最小。
令Q = Sπ
请编程对给出的N和M,找出蛋糕的制作方案(适当的Ri和Hi的值),使S最小。
(除Q外,以上所有数据皆为正整数)

Input

有两行,第一行为N(N <= 10000),表示待制作的蛋糕的体积为Nπ;第二行为M(M <= 20),表示蛋糕的层数为M。

Output

仅一行,是一个正整数S(若无解则S = 0)。

Sample

InputcopyOutputcopy
100
2
68

Hint

圆柱公式
体积V = πR2H
侧面积A' = 2πRH
底面积A = πR2

思路:利用两个数组记录当前层数的s和v,搜索时带有三个参数(前一步的s总和与前一步的v总和),对于其进行剪枝操作(v之和+当前v是否大于n,s之和+当前s是否大于当前sum之和),再进入循环操作(要从r、h的最大值开始使用嵌套循环来一次次遍历,每次嵌套进行一次递归搜索(因为是递归搜索,dfs会更加方便)),若某次搜索时层数为0且v=n,即将s赋值给sum并输出

代码:

  1. #include <iostream>
  2. #include<algorithm>
  3. #include<math.h>
  4. #include <cmath>
  5. using namespace std;
  6. int n, m, minv[1000], mins[1000];
  7. const int inf = 1e9;
  8. int best = inf;
  9. void DFS(int depth, int sumv, int sums, int r, int h)
  10. {
  11. if (depth == 0)
  12. {
  13. if (sumv == n && sums < best)
  14. {
  15. best = sums;//都满足的情况下
  16. }
  17. return;
  18. }
  19. if (sumv + minv[depth - 1] > n || sums + mins[depth - 1] > best || sums + 2 * (n - sumv) / r >= best)
  20. return;//三种情况下的剪枝操作
  21. for (int i = r - 1; i >= depth; i--) //每种情况下层数一般就是最大值,因为每一次都必为整数
  22. {
  23. if (depth == m)//等于m的情况只会有一种,因此只需要判断一次即可
  24. sums = i * i;
  25. int maxh = min((n - sumv - minv[depth - 1]) / (i * i), h - 1);
  26. for (int j = maxh; j >= depth; j--)
  27. {
  28. DFS(depth - 1, sumv + i * i * j, sums + 2 * i * j, i, j); //递归搜索
  29. }
  30. }
  31. }
  32. int main()
  33. {
  34. cin >> n >> m;
  35. int rmax = (int)sqrt((double)n);
  36. int hmax = n;
  37. minv[0] = mins[0] = 0;
  38. for (int i = 1; i <= m; i++)
  39. {
  40. minv[i] = minv[i - 1] + i * i * i; //用数学公式推导得
  41. mins[i] = mins[i - 1] + 2 * i * i;
  42. }
  43. DFS(m, 0, 0, rmax, hmax);
  44. if (best == inf)
  45. best = 0;
  46. if (best == 0)
  47. cout << "0" << endl;
  48. else
  49. cout << best << endl;
  50. return 0;
  51. }

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/小蓝xlanll/article/detail/323212
推荐阅读