赞
踩
JAVA学习今天学了字符串相关类的底层原理(简单了解了一下)和集合部分,自己试着做了一些集合的简单练习来加强理解
集合和数组部分功能上有些相似,但是总体存在很大区别
1.长度
数组长度固定,集合长度可以改变
2.内容
数组可以存储基本数据类型和引用数据类型
集合中能存储引用数据类型
3.储存元素类型
数组只能存储一种类型的元素,集合可以存储多种不同类型的元素
构建集合时有很多好用的函数
如:list.add(),list.remove(),list.get(),list.set()等
储存用户信息,实现简单查询功能
- package Arraylist;
-
- import java.util.ArrayList;
-
- public class user2 {
- public static void main(String[] args) {
- ArrayList<user2pople> list=new ArrayList<>();
-
- user2pople u1=new user2pople("h1","zs","123");
- user2pople u2=new user2pople("h2","ls","456");
- user2pople u3=new user2pople("h3","ww","789");
-
- list.add(u1);
- list.add(u2);
- list.add(u3);
-
-
- int index=getindex(list,"hhh");
- System.out.println(index);
- //boolean flag = contains(list,"hhh");
- //System.out.println(flag);
- }
-
- /*public static boolean contains(ArrayList<user2pople>list,String id)
- {
- for(int i=0;i<list.size();i++)
- {
- user2pople u=list.get(i);
- String uid=u.getId();
- if(uid.equals(id))
- {
- return true;
- }
- }
- return false;
- }*/
- public static int getindex(ArrayList<user2pople>list,String id)
- {
- for(int i=0;i<list.size();i++)
- {
- user2pople u=list.get(i);
- String uid=u.getId();
- if(uid.equals(id))
- {
- return i;
- }
- }
- return -1;
- }
- }

- package Arraylist;
-
- public class user2pople {
- private String id;
- private String username;
- private String password;
-
- public user2pople() {
- }
-
- public user2pople(String id, String username, String password) {
- this.id = id;
- this.username = username;
- this.password = password;
- }
-
- public String getId() {
- return id;
- }
-
- public void setId(String id) {
- this.id = id;
- }
-
- public String getUsername() {
- return username;
- }
-
- public void setUsername(String username) {
- this.username = username;
- }
-
- public String getPassword() {
- return password;
- }
-
- public void setPassword(String password) {
- this.password = password;
- }
- }

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外,以上所有数据皆为正整数)
有两行,第一行为N(N <= 10000),表示待制作的蛋糕的体积为Nπ;第二行为M(M <= 20),表示蛋糕的层数为M。
仅一行,是一个正整数S(若无解则S = 0)。
Inputcopy | Outputcopy |
---|---|
100 2 | 68 |
圆柱公式
体积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并输出
代码:
- #include <iostream>
- #include<algorithm>
- #include<math.h>
- #include <cmath>
- using namespace std;
- int n, m, minv[1000], mins[1000];
- const int inf = 1e9;
- int best = inf;
- void DFS(int depth, int sumv, int sums, int r, int h)
- {
- if (depth == 0)
- {
- if (sumv == n && sums < best)
- {
- best = sums;//都满足的情况下
- }
- return;
- }
- if (sumv + minv[depth - 1] > n || sums + mins[depth - 1] > best || sums + 2 * (n - sumv) / r >= best)
- return;//三种情况下的剪枝操作
- for (int i = r - 1; i >= depth; i--) //每种情况下层数一般就是最大值,因为每一次都必为整数
- {
- if (depth == m)//等于m的情况只会有一种,因此只需要判断一次即可
- sums = i * i;
- int maxh = min((n - sumv - minv[depth - 1]) / (i * i), h - 1);
- for (int j = maxh; j >= depth; j--)
- {
- DFS(depth - 1, sumv + i * i * j, sums + 2 * i * j, i, j); //递归搜索
- }
- }
- }
- int main()
- {
- cin >> n >> m;
- int rmax = (int)sqrt((double)n);
- int hmax = n;
- minv[0] = mins[0] = 0;
- for (int i = 1; i <= m; i++)
- {
- minv[i] = minv[i - 1] + i * i * i; //用数学公式推导得
- mins[i] = mins[i - 1] + 2 * i * i;
- }
- DFS(m, 0, 0, rmax, hmax);
- if (best == inf)
- best = 0;
- if (best == 0)
- cout << "0" << endl;
- else
- cout << best << endl;
- return 0;
- }

Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。