- 博客(26)
- 收藏
- 关注
原创 为什么重写了equal方法,一定要重写hashCode方法?
但是 equals 方法,更多是用来比较两对象的内容是否相同,所以就要重写,重写之后就是内容相同的两对象就返回 true,但是在没有重写的 hashCode 方法中,就会生成两个不同哈希码。举例说明 set 集合,这个要求元素不能重复,p1 和 p2 内容相等,如果不重写,p1 和 p2 就可能同时存到 set 集合中(set 集合判断重复,查找元素底层是要涉及哈希表和 hashCode 函数的),重写一般可以根据内容来计算哈希值,这样二者得到的哈希值就一样了,也就不会出现上述问题了。
2024-11-30 03:07:09
156
原创 Java的单例模式
懒汉式—只有在获取对象的静态方法被调用的时候才会创建(准确来说,一般是类中静态方法拿到唯一对象的引用时候,先判断该对象是否已经实例化(判空),若已实例化直接返回该类对象。获取对象的方式,一种是new一个对象,另一种是通过调用类的静态方法去获取对象(一般是该类的对象只有一个)JVM加载的时候是会执行类中的静态初始化部分,包括静态变量的初始化,静态方法不执行。就是将构造方法私有化,可以避免外部直接创建新的实例。饿汉式—类加载到内存中的时候就会创建这唯一的对象,单例模式—在内存中只会创建一次某类的对象。
2024-09-13 00:15:21
195
原创 最长上升子序列
#include<bits/stdc++.h>using namespace std;int i,j,n,a[20003],m=0,f[20003]; //f[i]是指以i结尾的最长子序列长度 int main(){ cin>>n; for(i=1;i<=n;i++) cin>>a[i],f[i]=1;//子序列只有自身,则长度就是1 for(i=1;i<=n;i++) {//按照输入的顺序 ,开始计算数值f[i] for(j=1.
2021-11-11 20:46:27
101
原创 HDU 2050 (折线分割平面)
#include<bits/stdc++.h>using namespace std;int face(int m){ int c=0,s=0; if(m==2) c=0; for(int i=1;i<=m-2;i++) c+=i; s=c+m*2; return s;}int main(){ int n,m; scanf("%d",&n); while(n--) { sca
2021-05-20 20:42:01
127
原创 HDU 2046 (骨牌铺方格)
#include<bits/stdc++.h>using namespace std; int main(){ int i,n; double way[51]; way[1]=1,way[2]=2; for(i=3;i<=51;i++) way[i]=way[i-1]+way[i-2]; while(scanf("%d",&n)==1) { printf("%.f\n",way[n]); } return 0; } //若用递归,会出现超过时限的问题;
2021-05-19 20:18:21
93
原创 HDU 2044 (一只小蜜蜂)
#include<bits/stdc++.h>using namespace std;int main(){ int n,i,a,b; double road[51]; road[2]=1,road[3]=2; scanf("%d",&n); for(i=4;i<=50;i++) road[i]=road[i-1]+road[i-2]; while(n--) { scanf("%d %d",&a,&a
2021-05-19 19:50:26
92
原创 HDU2041
题目:http://acm.hdu.edu.cn/showproblem.php?pid=2041#include<bits/stdc++.h>using namespace std;int main(){ int a[40],n,m; a[1]=a[2]=1; for(int i=3;i<=40;i++) a[i]=a[i-1]+a[i-2]; cin>>n; while(n--) { cin>>m; cout<&l
2021-05-16 16:18:26
71
原创 用链表实现简单的数据输入输出
输入一组数据,紧接着输出它们;#include<bits/stdc++.h>using namespace std;typedef struct lnode{ int data; struct lnode *next; }lnode,*linklist; void createlist(linklist &l,int n){ l=new lnode; l->next=NULL; linklist p; for(int i=0;i<n;i++)
2021-05-16 10:10:52
2101
原创 HDU 1233
题目;http://acm.hdu.edu.cn/showproblem.php?pid=1233#include <bits/stdc++.h>using namespace std;int parent[101];struct edge{ int a,b,dis;}nums[5000];int find(int x)//找根节点 { if (x!= parent[x]) parent[x] = find(parent[x]); return par
2021-05-13 19:40:06
90
原创 HDU1232
题目:http://acm.hdu.edu.cn/showproblem.php?pid=1232#include<bits/stdc++.h>using namespace std;int a[1010];int find(int b) //寻找根 root{ int r=b; while(r!=a[r]) { r=a[r]; } return r;}void merge(int x,int y)//将集合merge并在一起; { int s=find(x);
2021-05-10 20:27:55
90
原创 hdu 2037
题目:http://acm.hdu.edu.cn/showproblem.php?pid=2037#include<bits/stdc++.h>using namespace std;struct node{ int s,e;}t[109];bool cmp(node x, node y){return x.e<y.e;}int main(){ int n,i,sum,a; while(scanf("%d",&n)==1) {
2021-04-18 12:27:54
136
原创 hdu 1098
题目;http://acm.hdu.edu.cn/showproblem.php?pid=1098#include <bits/stdc++.h>using namespace std;int main(){ int n; while(scanf("%d\n,&n")!=EOF) { n%=65; for(int i=0;i<=64;i++) { if((18+n*i)%65==0) { printf("%d\n",i); brea
2021-04-14 21:57:16
79
原创 hdu 1052(田忌赛马)
题目;http://acm.hdu.edu.cn/showproblem.php?pid=1052#include <bits/stdc++.h>using namespace std;int n, t[1050], w[1050];int main(){ while(scanf("%d", &n)) { if(n==0) break; for(int i=1;i<=n;i++) scanf("%d",t+i);
2021-04-14 19:34:43
102
原创 基于顺序存储结构的图书信息表的创建和输出
题目;基于顺序存储结构的图书信息表的创建和输出#include<bits/stdc++.h>using namespace std;#define maxsize 10000 typedef int Status;typedef struct{ char no[20]; char name[50]; double price;}book;typedef struct{ book *elem; int length; }SqList;void InitList
2021-04-11 13:52:07
795
原创 HD1060
题目:http://acm.hdu.edu.cn/showproblem.php?pid=1060#include<stdio.h>#include<math.h>int main(){ int a,n,s; double b;//所求结果涉及小数,应用浮点型; scanf("%d",&a); while(a--) { scanf("%d",&n); b=n*log10(n); s=pow(10,b-floor(b));//floor函数是向下
2021-03-31 20:59:20
88
原创 HD2035
题目:http://acm.hdu.edu.cn/showproblem.php?pid=2035#include<stdio.h>int main(){ int a,b,c,i; while(scanf("%d %d",&a,&b)!=EOF) { if(a==0&&b==0) return 0; c=1; for(i=1;i<=b;i++) { c=c*a%1000; }//防止数据超出范围; printf("
2021-03-31 19:52:40
115
原创 HD1097
题目:http://acm.hdu.edu.cn/showproblem.php?pid=1097#include<stdio.h>#include<math.h>int main(){ int a,b,c; while(scanf("%d %d",&a,&b)!=EOF) { while(a>=10) a%=10; b=b%4; if(b==0) b=4; c=pow(a,b); while(c>=10) c
2021-03-31 19:15:23
93
原创 hdu1021
题目:http://acm.hdu.edu.cn/showproblem.php?pid=1021#include<stdio.h>int main(){ int n; while(scanf("%d",&n)!=EOF) { if(n%4==2)//主要是找规律;不然就要求出F(n)的值 else printf("no\n"); } return 0;}
2021-03-30 20:39:52
91
原创 HD1019
题目http://acm.hdu.edu.cn/showproblem.php?pid=1019#include<stdio.h>int gcd(int a,int b);int lcm(int a,int b);int main(){ int m,n,x,y; scanf("%d",&m); while(m--) { y=1; scanf("%d",&n); while(n--) { scanf("%d",&x); y=l
2021-03-30 20:01:41
126
原创 HD1108
题目:http://acm.hdu.edu.cn/showproblem.php?pid=1108#include<stdio.h>//递归方法较快;int gcd(int a,int b);int main(){ int a,b; while(scanf("%d %d",&a,&b)!=EOF) printf("%d\n",(a*b)/gcd(a,b)); } int gcd(int a,int b) { if(b==0) return a;
2021-03-30 18:56:49
106
原创 HD1001
任务是计算和(N)=1+2+3+.+n。输入输入将由一系列整数n组成,每一行一个整数。输出量对于每一种情况,输出和(N)在一行中,后面是空行。您可以假设结果将在32位有符号整数的范围内。样本输入1100样本输出15050#include<stdio.h>int main() { int i, n, s, a; while(scanf("%d", &n), n)//括号内为逗号表达式,最终取逗号后表达式的值 { s=0; for (i=0;i&l
2021-03-26 20:09:37
108
原创 求n的阶层
求n的阶层;/求n的阶层 (递归函数)#include<stdio.h>int fact(int n);int main(){ int n; scanf("%d",&n); printf("%d\n",fact(n)); return 0; } int fact(int n) { if(n==0) return 1; //0的阶层是1; if(n==1) return 1;//递归一般先要设置出口; return fact(n-1)*n;
2021-03-26 19:44:35
1042
原创 HD1000
HD1000**题目问题描述计算A+B.输入每一行将包含两个整数。A和B。进程到文件结束。输出量对于每一种情况,输出A+B一条条线。样本输入1 1样本输出2**代码如下:#include <stdio.h>int main(){ int a,b; while(scanf("%d %d",&a, &b)!=EOF)//括号内为逗号表达式 printf("%d\n",a+b); return 0;}...
2021-03-23 19:15:10
246
2
空空如也
空空如也
TA创建的收藏夹 TA关注的收藏夹
TA关注的人