Java4&5

随便贴一下

Posted by wqw on August 27, 2018

JAVA-4 题目

【第一题】 使用正则,解析出html页面代码中的文本。直接调用库中解析html页面的方法不得分。

时间限制10s内,空间限制512M。 输入格式说明: 1行,html页面中的正常(语法正确,标签配对)的片段,1000字符以内。转义字符(比如&rt)不用考虑,<br>不用换行,不会出现像<…<…>…>这样嵌套的情况。文本中不会出现尖括号,也不需要额外去处理文本中的空白字符。 输出格式说明 1行,该片段中所包含的文本。

输入示例:

清华主办第七届世界和平论坛

探讨以“平等、公平、正义”构建安全共同体


清华新闻网7月14日电(记者 李含 樊向宇)7月14日,由清华大学主办、中国人民外交学会协办的第七届世界和平论坛在清华大学主楼拉开帷幕。中共中央政治局委员、中央外事工作委员会办公室主任杨洁篪出席开幕式并致辞。

本届论坛的主题为“构建安全共同体:平等、公平、正义”。来自多个国家的前政要及智库领袖在为期两天的论坛上针对当今世界持续面临的安全挑战进行讨论,提出建设性应对方案,推动国际社会安全合作。

输出示例: 清华主办第七届世界和平论坛 探讨以平等、公平、正义构建安全共同体 清华新闻网7月14日电(记者 李含 樊向宇)7月14日,由清华大学主办、中国人民外交学会协办的第七届世界和平论坛在清华大学主楼拉开帷幕。中共中央政治局委员、中央外事工作委员会办公室主任杨洁篪出席开幕式并致辞。 本届论坛的主题为“构建安全共同体:平等、公平、正义”。来自多个国家的前政要及智库领袖在为期两天的论坛上针对当今世界持续面临的安全挑战进行讨论,提出建设性应对方案,推动国际社会安全合作。

【第二题】 小涵是一名城市规划师,她所在的城市开辟了一个新区,建有n个商场,小涵通过数据结构的知识了解到,最少只需要n-1条路就能让这n个商场全连通。可惜粗心的小涵随便的选定了n-1条道路,请问他能达成全连通商场的目的吗?
全连通:任意两个商场是连通的,即任意两个商场之间都有路径相连。 时间限制2min,空间限制512M。 【第三题】 使用正则表达式子模式,匹配出所给字符串中在1990年1月1日(含)至2018年1月1日(不含)之间的日期。 如果字符串中包含范围内的实际存在的日期,则依次输出;如果字符串内不包含范围内的实际存在的日期,则输出NotMatch
为方便使用子模式,不考虑2月29日这天(即默认所有的年份都是非闰年,2月只有28天)。

解析HTML

import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.io.*;
public class Regular 
{
	static String reg = ">([\\s\\S]*?)<" ;
	public static void main(String[] args)
	{
		//static String reg = " <[a-zA-Z]+.*?>([\\s\\S]*?)</[a-zA-Z]*> " ;
		Scanner scan=new Scanner(System.in);
		String example =scan.nextLine();
		scan.close();
		Pattern p =Pattern.compile(reg);
		Matcher m =p.matcher(example);
		while ((m.find()))
		{
			System.out.print(example.substring(m.start()+1, m.end()-1));
		}
		

	}
}

城市规划

import java.util.*;
import java.io.*;
public class disjoint
{
	static ArrayList<Integer> list =new ArrayList<Integer>();
	public static void main(String args[])
	{
		Scanner scan=new Scanner(System.in);
		int n=scan.nextInt();
		int ans[]=new int[n+2];
		for(int i=0;i<=n;i++)
		{
			list.add(i);
		}
		for(int i=1;i<=n-1;i++)
		{
			join(scan.nextInt(),scan.nextInt());
			//System.out.println(list);
		}
		for(int i=1;i<=n;i++)
			ans[i]=find(i);
		scan.close();
		for(int i=1;i<=n;i++)
		{
			if(ans[i]!=ans[1])
			{
				System.out.println("NO");
				break;
			}
			if(i==n)
				System.out.println("YES");
		}
		

	}
	static private int find(int num)
	{
		int temp=num;
		while(list.get(temp)!=temp)
		{
			temp=list.get(temp);
		}
		int temp1=num;
		int temp2=num;
		while(list.get(temp1)!=temp)
		{
			temp2=list.get(temp1);
			list.set(temp1, temp);
			temp1=temp2;
		}
		return temp;
	}
	static private void join(int num1,int num2)
	{
		int fnum1=find(num1);
		int fnum2=find(num2);
		//System.out.println("join"+num1+" "+num2+" "+fnum1+" "+fnum2);
		if(fnum1!=fnum2)
			list.set(num1, list.get(num2));		
	}
}

日期判断

import java.io.*;
import java.util.*;
import java.util.regex.*;
public class dateformat 
{
	static String reg="\\d{4}-\\d{2}-\\d{2}";
	public static void main(String []args)
	{
		Scanner scan =new Scanner(System.in);
		String data=scan.nextLine();
		scan.close();
		Pattern p=Pattern.compile(reg);
		Matcher m=p.matcher(data);
		int temp=0;
		int sign=0;
		while(m.find(temp))
		{
			temp=m.start()+1;
			String tempdate=data.substring(m.start(),m.end());
			int year=Integer.parseInt(tempdate.substring(0,4));
			int month=Integer.parseInt(tempdate.substring(5,7));
			int day=Integer.parseInt(tempdate.substring(8,10));
			
			if(year>=1990&&year<=2017)
			{
				if(month==1||month==3||month==5||month==7||month==8||month==12)
				{
					if(day>=1&&day<=31)
					{
						System.out.println(data.substring(m.start(), m.end()));
						sign=1;
					}
				}
				if(month==4||month==6||month==9||month==11)
				{
					if(day>=1&&day<=30)
					{
						System.out.println(data.substring(m.start(), m.end()));
						sign=1;
					}
				}
				if(month==2)
				{
					if(day>=1&&day<=28)
					{
						System.out.println(data.substring(m.start(), m.end()));
						sign=1;
					}
				}
			}
			
		}
		if(sign==0)
		{
		System.out.print("NotMatch");
		}
		
	}
}

JAVA-5 题目

【第一题】 在控制台上,每隔 1 秒输出一次时间。 时间格式: [年/月/日][时:分:秒] 如: [2017/8/20][19:28:56] [2017/8/20][19:28:57] [2017/8/20][19:28:58] [2017/8/20][19:28:59] …… 提示: Calendar SimpleDateFormat… 【第二题】 获取网页 http://www.114la.com/的文本内容,将所有以 com 为根域名的主机地址提取出来列 表(要求程序运行时才爬取网页,提前存储下来不得分)。 并根据输入的关键词,按照 ASCII 码排序输出(详见 TUOJ 平台上的题目说明)。 主机地址(部分) 如下: 1.qtmojo.com 106cai.xmfinger.com 114la.com 114ys.ylmf.com 115.com 126.com 139.com 163.com 188.com 21cn.com 2Fmail.aliyun.com 2Fmy.114la.com …… weibo.com wt.taobao.com www.114la.com www.120ask.com ……

第一题的实验报告 因为作业要求 所以写的多

如下

输出时间

通过SimpleDateFormat

SimpleDateFormat dateformat=new SimpleDateFormat("[yyyy/MM/dd][HH:mm:ss]");//[2018/08/26][23:15:36]
			String d1=dateformat.format(new Date());
			System.out.println(d1);

每隔一秒输出(代码采用

通过线程的sleep()实现

while(true)
		{
			/*
			输出当前时间
			*/
			try
			{
				sleep(1000);
			}
			catch(InterruptedException e)
			{
				e.printStackTrace();
			}
		}

这种方法输出的时间可能会出现相邻的两次输出相差2秒的情况,但每次输出时都能保证是当前时间。

通过对某次获取时间+1S

Calendar c =Calendar.getInstance();
			c.add(Calendar.SECOND, 1);
			Date d2=new Date(c.getTimeInMillis());
			System.out.println("   "+dateformat.format(d2));

只获取一次时间,以后每sleep(1000)之后对获得的时间+1s,这样可以保证相邻输出间隔为1s但有可能导致时间失真。

爬虫

import java.io.*;
import java.util.*;
import java.util.regex.*;
import java.net.*;
public class crawler
{

	public static void main(String args[])
	{
		URL url=null;
		ArrayList<String>  files= new ArrayList<String>();
		URLConnection urlconn = null;
		String regex="(([A-Za-z0-9]+)\\.)+?([A-Za-z0-9])+?\\.com";
		Pattern p=Pattern.compile(regex);
		Scanner scan=new Scanner(System.in);
		String input=scan.nextLine().toLowerCase();
		scan.close();
		try
		{
			url=new URL("http://47.104.89.193/");
			urlconn =url.openConnection();
			InputStream in=urlconn.getInputStream();
			//System.out.println(urlconn.getContentEncoding());
			BufferedReader br =new BufferedReader(new InputStreamReader(in,"UTF-8"));
			//String line=null;
			String data=br.readLine();
			//System.out.println(data);
			Matcher m=p.matcher(data);
			while(m.find())
			{
				//System.out.println(data.substring(m.start(), m.end()));
				if(!files.contains(data.substring(m.start(), m.end()).toLowerCase()))
				{
					files.add(data.substring(m.start(), m.end()).toLowerCase());
				}
				//System.out.print("     "+i++);
			}
			Collections.sort(files);
			//System.out.println(files.size());
			for(int i=0;i<files.size();i++)
			{
				if(files.get(i).contains(input))
				{
					System.out.println(files.get(i));
				}
			}
			//System.out.println(files.size());
		}
		catch(Exception e)
		{
			e.printStackTrace();
		}
	}
}