牛客网Java编程题目

1、设计实现能至多存储100个int型数据的顺序表,具备以下功能
(注意:参数和返回类型已经确定,不可更改)
a. void append() :向顺序表尾部追加若干元素(数量不确定),以-1结束;
b. void show():打印输出顺序表中的所有元素
c. boolean insert(int pos, int x):在下标pos处插入元素x,若可以插入(即下标位置合法且表未满),则实施插入,并返回true;否则直接返回false;
d. boolean delete(int pos):删除下标pos处插入元素,若可以删除(即下标位置存在元素),则实施插入,并返回true;否则直接返回false;
e. void insertSorted(int x):假设顺序表是升序表,向表中插入数据x,使其依旧有序

package link;

import java.util.Scanner;

class sequlist{
	final int max=100;
	int []a=new int [max];
	int len;
	private Scanner sc;
	void append() {
		int x;
		int i=0;
		System.out.print("请输入一组数,以-1结束\n");
		sc = new Scanner(System.in);
		x=sc.nextInt();
		while(x!=-1) {
			a[i]=x;x=sc.nextInt();i++;
		}
		len=i;
	}
	void show() {
		for(int i=0;i<len;i++)
			System.out.print(a[i]+" ");
		System.out.print("\n");
	}
	boolean insert(int pos, int x) {
		int i;
		if(pos>len)return false;
		else 
			for(i=len-1;i>=pos;i--)
				a[i+1]=a[i];
		a[i+1]=x;len++;
		return true;
	}
	boolean delete(int pos) {
		int i;
		if(pos>=len&&pos<0)return false;
		else 
			for(i=pos;i<len;i++)
				a[i]=a[i+1];
		        len--;
		return true;
	}
	void insertSorted(int x) {
		int i;
		for(i=len-1;a[i]>x&&i>0;i--) {
			a[i+1]=a[i];
		}
		a[i+1]=x;
		len++;
	}
	public static void main(String [] agrs) {
		sequlist L=new sequlist();
		L.append();/*
		L.show();
		L.insert(1, 3);
		L.show();
		L.delete(4);*/
		L.show();
		L.insertSorted(4);
		L.show();
	}
}

2、在1到100中不能被2和5整除的整数和
the sum of number that can’t to be divisable by 2 and 5 between 1 to 100

package cn.lp;
public class Break {
	public static void main(String[] args) {
		System.out.println("the sum of  number that can't to be divisable by 2 and 5 between 1 to 100");
		int i=1;
		int sum=0;
			do {
				if((i%2!=0&&i%5!=0)||i==2||i==5) {
					sum=sum+i;
					if(i==99) {
						System.out.print(i+"=");
					}else {
						System.out.print(i+"+");
					}
				}
				i++;
			}while(i<=100);
		System.out.print(sum);
	}
}

3、在1到100中能够整除2,3,5的整数和
the sum of number that can to be divisable by 2 , 3 and 5 between 1 to 100

package cn.lp;

public class Function {
	public static void main(String[] args) {
		System.out.println("the sum of  number that can to be divisable by 2 , 3 and 5 between 1 to 100");
		int i=1;
		int sum=0;
			do {
				if(i%2==0&&i%3==0&&i%5==0) {
					sum=sum+i;
					if(i==480) {
						System.out.print(i+"=");
					}else {
						System.out.print(i+"+");
					}
				}
				i++;
			}while(i<=500);
		System.out.print(sum);
	}
}

4、从1到100的素数和
the sum of prime number between 1 to 100

package cn.lp;

public class prime {
	public static void main(String[] args) {
		System.out.println("the sum of prime number between 1 to 100");
		int i,j;
		int sum=0;
		int flag;
		for(i=2;i<=100;i++) {
			flag=1;
			for(j=2;j<=i/2;j++) {
				if(i%j==0) {
					flag=0;
					break;
				}
			}
			if(flag==1) {
				sum=sum+i;
				if(i==97) {
					System.out.print(i+"=");
				}else {
					System.out.print(i+"+");
				}
			}
			
		}
		System.out.print(sum);
	}
}

5、输出如下图所示的菱形在这里插入图片描述

package cn.lp;

public class Shape1 {
	public static void main(String[] args) {
		int P=10;
		int Q=1;
		for(int i=1;i<=3;i++) {
			for(int m=0;m<=P;m++) {
				System.out.print(" ");
			}
			for(int j=1;j<=Q;j++) {
				System.out.print(" *");
			}
			System.out.println();
			P--;
			Q++;
		}

		for(int i=1;i<=4;i++) {
			for(int m=0;m<=P;m++) {
				System.out.print(" ");
			}
			for(int j=1;j<=Q;j++) {
				System.out.print(" *");
			}
			System.out.println();
			P++;
			Q--;
		}
		
	}
}

6、输出如下图所示图形
在这里插入图片描述

package cn.lp;

public class Shape2 {
	public static void main(String[] args) {
		int P=4;
		for(int i=1;i<=6;i++) {
			for(int m=0;m<=P;m++) {
				System.out.print(" ");
			}
			for(int j=1;j<=8;j++) {
				System.out.print("*");
			}
			System.out.println();
			P++;
		}

		for(int i=1;i<=7;i++) {
			for(int m=0;m<=P;m++) {
				System.out.print(" ");
			}
			for(int j=1;j<=8;j++) {
				System.out.print("*");
			}
			System.out.println();
			P--;
		}
		
	}
}

7、输出如下图所示图形
在这里插入图片描述

package cn.lp;

public class Shape3 {
	public static void main(String[] args) {
		int P=4;
		int Q=1;
		for(int i=1;i<=7;i++) {
			for(int m=0;m<=P;m++) {
				System.out.print(" ");
			}
			for(int j=1;j<=Q;j++) {
				System.out.print("*");
			}
			System.out.println();
			P++;
			Q++;
		}

		for(int i=1;i<=8;i++) {
			for(int m=0;m<=P;m++) {
				System.out.print(" ");
			}
			for(int j=1;j<=Q;j++) {
				System.out.print("*");
			}
			System.out.println();
			P--;
			Q--;
		}
		
	}
}

8、输出如下图所示图形
在这里插入图片描述

package cn.lp;

public class Shape4 {
	public static void main(String[] args) {
		int P=10;
		int Q=1;
		for(int i=1;i<=5;i++) {
			for(int m=0;m<=P;m++) {
				System.out.print(" ");
			}
			for(int j=1;j<=Q;j++) {
				System.out.print("*");
			}
			System.out.println();
			P--;
			Q=Q+2;
		}
	}
}

9、输出如下图所示图形
在这里插入图片描述

package cn.lp;

public class Shape5 {
	public static void main(String[] args) {
		int P=10;
		int Q=1;
		for(int i=1;i<=7;i++) {
			for(int m=0;m<=P;m++) {
				System.out.print(" ");
			}
			for(int j=1;j<=Q;j++) {
				System.out.print("*");
			}
			System.out.println();
			P--;
			Q=Q+2;
		}
		for(int i=1;i<=7;i++) {
			for(int m=0;m<=10;m++) {
				System.out.print(" ");
			}
				System.out.print("*");
			System.out.println();
		}		
	}
}

10、从1到100的合数求和
the sum of sum number between 1 to 100

package cn.lp;


public class sum {
	public static void main(String[] args) {
		System.out.println("the sum of sum number between 1 to 100");
		int i,j;
		int sum=0;
		int flag;
		for(i=2;i<=100;i++) {
			flag=1;
			for(j=2;j<=i/2;j++) {
				if(i%j==0) {
					flag=0;
					break;
				}
			}
			if(flag==0) {
				sum=sum+i;
				if(i==100) {
					System.out.print(i+"=");
				}else {
					System.out.print(i+"+");
				}
			}
			
		}
		System.out.print(sum);
	}
}

11.从1到100能被2或5整除的整数和
the sum of number to be divisable by 2 and 5 between 1 to 100

package cn.lp;

public class until {
	public static void main(String[] args) {
		System.out.println("the sum of  number to be divisable by 2 and 5 between 1 to 100");
		int i=4;
		int sum=0;
			do {
				if(i%2==0||i%5==0&&i!=5) {
					sum=sum+i;
					if(i==100) {
						System.out.print(i+"=");
					}else {
						System.out.print(i+"+");
					}
				}
				i++;
			}while(i<=100);
		System.out.print(sum);
	}
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值