Java:素数

java 输入N以内的全部素数

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
package ex3;
import java.util.*;
public class Sushu {
public static void main(String[] args) {
// TODO Auto-generated method stub
@SuppressWarnings("resource")
Scanner reader=new Scanner(System.in);
int n=reader.nextInt();

if(n>=2)
{
System.out.printf(2+" ");
for(int i=3;i<=n;i++)
{
int s=1;
for(int j=2;j<i;j++)
{
if(i%j==0)
{
s=0;
break;
}
}
if(s!=0)
System.out.printf(i+" ");
}
}
else
return;
}

}

数据结构栈

#数据结构
##顺序栈

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
#include<iostream>
using namespace std;
#define MAX 10
typedef struct{
int base;
int top;
int s[MAX];
}SqStack;

//基本操作说明
void InitStack(SqStack &S);
void push(SqStack &S,int e);
void pop(SqStack &S,int &e);

//基本操作的算法描述
void InitStack(SqStack &S)
{
S.base=S.top=0;
}
void push(SqStack &S,int e)
{
if(S.top==MAX)
{
cout<<"栈满"<<endl;
return;
}
S.s[S.top]=e;
S.top+=1;
}
void pop(SqStack &S,int &e)
{
if(S.top==0)
{
cout<<"栈空"<<endl;
return;
}
S.top-=1;
e=S.s[S.top];
}

void print(SqStack S)
{
cout<<"栈的各个元素如下:"<<endl;
for(int i=0;i<S.top;i++)
cout<<S.s[i]<<' ';
cout<<endl;

}
int main()
{
SqStack S;
//初始化
cout<<"初始化栈"<<endl;
InitStack(S);
cout<<"栈底和栈顶:"<<S.base<<','<<S.top<<endl;
//入栈
cout<<"5个元素入栈\n";
for(int i=0;i<5;i++)
{
push(S,2*i+i);
cout<<"栈顶为:"<<S.top<<endl;
}
print(S);
//出栈
int x;
pop(S,x);
cout<<"出栈元素为:"<<x<<"栈顶"<<S.top<<endl;
print(S);
return 0;
}

数据结构顺序表

顺序表的插入、删除、遍历

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
#include<iostream>
using namespace std;
#define MAX 15

typedef struct
{
int elem[MAX];
int length;
}Sqlist;

void Initlist_sq(Sqlist &L);
void ListInsert_sq(Sqlist &L,int i,int e);
void ListDel_sq(Sqlist &L,int i,int &e);
void print_sq(Sqlist L);

//函数的定义
void Initlist_sq(Sqlist &L)//定义空链表
{
L.length=0;
}
void ListInsert_sq(Sqlist &L,int i,int e)//插入元素
{
int *q,*p;
if(i<1||i>L.length) return;
q=&L.elem[i-1];//插入位置
for(p=&L.elem[L.length-1];p>=q;--p)
*(p+1)=*p;
*q=e;
++L.length;
return;

}
void ListDel_sq(Sqlist &L,int i,int &e)//删除元素
{
if(i<1||i>L.length) return;
e=L.elem[i-1];
for(int j=i;j<L.length;j++)
L.elem[j-1]=L.elem[j];
--L.length;
}
void print_sq(Sqlist L)
{
int *p,*q=&L.elem[L.length-1];//指向尾元素的位置
for(p=L.elem;p<=q;p++)
cout<<*p<<' ';
cout<<"\n";
}

int main()
{
int a[11]={10,20,30,40,50,60,70,80,90,100};
Sqlist L;

//初始化顺序表
Initlist_sq(L);
cout<<"现在的表长为:"<<L.length<<endl;
//插入10个元素
for(int i=1;i<=10;i++)
ListInsert_sq(L,i,a[i-1]);
cout<<"现在的表长为:"<<L.length<<endl;
//遍历
print_sq(L);
cout<<"现在的表长为:"length<<endl;

//删除第5个元素
int x;
ListDel_sq(L,5,x);
cout<<"删除的元素是:"<<x<<endl;
cout<<"删除第 5 个元素后的表:"<<endl;
print_sq(L);
cout<<"现在的表长为:"<<endl;
return 0;
}

Java String

编写一个Java应用程序,实现如下功能:

(1) 判断两个字符串是否相同,s1=”you are a student”,s2=”how are you”;

(2) 判断字符串”22030219851022024”的前缀、后缀是否和某个字符串”220302”相同;

(3) 按字典顺序比较两个字符串”你”和”我”的大小关系;

(4) 将数字型字符串”100”和”123.678”转换为数字;

(5) 将字符串”FEDCBA”存放到数组中,调用for循环读出数组数据显示在屏幕上。
————————————————

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
package string;

public class Ex1 {

public static void main(String[] args) {
// TODO Auto-generated method stub
//判断两字符是否相等
String s1=new String("you are a strdent"),
s2=new String("how are you");
if(s1.equals(s2)){
System.out.println("s1与s2相同");
}
else{
System.out.println("s1与s2不相同");
}

//判断两字符的前缀是否相同
String s3=new String("220302199911225720");
if(s3.startsWith("220302")){
System.out.println("吉林省的身份证");
}

//按字典顺序排序
String s4=new String("你"),
s5=new String("我");
if(s4.compareTo(s5)>0){
System.out.println("按字典序,s4大于s5");
}
else{
System.out.println("按字典序,s4小于s5");
}

//检索字符的位置
int position=0;
String path="C:\\java\\jsp\\A.java";//因为一个“\”+“字母”,会出现转义的意思,所以加两个“\”
position=path.lastIndexOf("\\");//获取path中最后出现目录分隔符的位置
System.out.println("C:\\java\\jsp\\A.java 中最后出现的位置"+position);

//将字符型的数字,转换成int或double类的能计算的数据
String fileName=path.substring(12);
System.out.println("C:\\java\\jsp\\A.java 中含有的文件名:"+fileName);
String s6=new String("100"), s7=new String("123.678");
int n1=Integer.parseInt(s6);//将s6转化成int型数据
double n2=Double.parseDouble(s7);//将s7转化成double型数据
double m=n1+n2;
System.out.println(m);

//字符型数字与能计算的数字之间的相互转化
String s8=String.valueOf(m);//将数字改成字符型,string调用valueof(int n)方法将m转化为字符串对象,也可以使用Double to String();
position=s8.indexOf(".");//从“.”后开始检索
String temp=s8.substring(position+1);
System.out.println("数字"+m+"有"+temp.length()+"为小数");

//将字符串放到数组中
String s9=new String("ABCDEF");
char a[]=s9.toCharArray();//将s9存放到数组a中
for(int i=a.length-1;i>=0;i--){
System.out.printf("%3c",a[i]);
}
}
}
1、int compareTo(String anotherString)   //按字典顺序比较两个字符串。返回值为一个数字 
2、int indexOf(int ch)      //返回指定字符在此字符串中第一次出现处的索引。最后结果是一个字符
   int lastIndexOf(int ch)  // 返回指定字符在此字符串中最后一次出现处的索引。返回结果为int型的数字
3、String substring(int beginIndex, int endIndex)//从原字符串截取一个子串,注:不能截到endIndex,只能截到他的前一位    
4、int n1=Integer.parseInt(s6);      //将s6转化成int型数据
   double n2=Double.parseDouble(s7);  //将s7转化成double型数据
   String valueOf(int i)      //返回 int 参数的字符串表示形式,将数字转化为字符
5、String s9=new String("ABCDEF");
   char a[]=s9.toCharArray();//将s9存放到数组a中

Java:接口

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
package hello;

public class Student {

public static void main(String[] args) {
// TODO Auto-generated method stub
ComputeTotalSales[] goods=new ComputeTotalSales[50];
for(int i=0;i<goods.length;i++){
if(i%3==0)
goods[i]=new Television();
else if(i%3==1)
goods[i]=new Computer();
else if(i%3==2)
goods[i]=new Mobile();
}
Shop shop=new Shop(goods);
System.out.println("商店年销售额:"+shop.giveTotalSales());
}

}
interface ComputeTotalSales{
public double totalSalesByYear();
}
class Television implements ComputeTotalSales {
public double totalSalesByYear(){
return 80000.0;
}
}
class Computer implements ComputeTotalSales {
public double totalSalesByYear(){
return 40000.0;
}
}
class Mobile implements ComputeTotalSales {
public double totalSalesByYear(){
return 20000.0;
}
}
class Shop{
ComputeTotalSales[] goods;
double totalSales=0;
Shop(ComputeTotalSales[] goods){
this.goods=goods;
}
public double giveTotalSales(){
totalSales=0;
for(int i=0;i<goods.length;i++)
totalSales=totalSales+goods[i].totalSalesByYear();
return totalSales;
}
}

Java:上转型对象

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
package hello;

public class Ex2 {

public static void main(String[] args) {
// TODO Auto-generated method stub
A a;
B b=new B();
a=b;
System.out.println("两数之和为:"+a.f1(3, 5));
System.out.println("两数之差为:"+a.f2(3,4));
A c;
C d=new C();
c=d;
System.out.println("两数之积为:"+c.f1(3, 5));
System.out.println("两数的平方和为:"+c.f2(3,4));

}
}
abstract class A{
public abstract int f1(int x,int y);
public abstract double f2(double x,double y);
}
class B extends A{
public int f1(int x,int y){
return x+y;
}
public double f2(double x,double y){
return x-y;
}
}
class C extends A{
public int f1(int x,int y){
return x*y;
}
public double f2(double x,double y){
return x*x+y*y;
}
}