「寻找数字java」寻找数字的规律的方法
本篇文章给大家谈谈寻找数字java,以及寻找数字的规律的方法对应的知识点,希望对各位有所帮助,不要忘了收藏本站喔。
本文目录一览:
- 1、Java语言:寻找数字,求解
- 2、java 控制台键盘输入一个数,在1~100找出这数
- 3、java 找出字符串的数字
- 4、Java数组,查找数字:输入5个整数,并查找出指定的整数位置
- 5、用java来写:查找数字
Java语言:寻找数字,求解
这个太简单了,有空写给你参考下
时间复杂度O(n)
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int[] index = new int[101];
int[] sum = new int[101];
int head,a;
head = a = sc.nextInt();
sum[a]++;
int t = 1;
for(int i = 1; i n;i++){
int x = sc.nextInt();
if(sum[x]==0){
a = index[a] = x;
t++;
}
sum[x]++;
}
String x = "", y = "";
while(t0)
{
if(sum[head]1)
x+=" "+head;
y += " "+head;
head = index[head];
t--;
}
System.out.print(x.substring(1)+"\n"+y.substring(1));
}
}
真搞不懂有的网友,时间复杂度都已经是O(n^2)了,代码还写得那么啰嗦。
java 控制台键盘输入一个数,在1~100找出这数
Scanner scanner = new Scanner(System.in);
int num = 0;
System.out.println("输入s开始游戏,输入p结束游戏");
String start = scanner.next();
boolean flag = true;
while (start.equals("s")) {
int count = 7;
System.out.println("输入1-100之间的数字");
String str = scanner.next();
if (str.equals("p")) {
System.out.println("游戏结束");
break;
} else if (Integer.parseInt(str) = 1
Integer.parseInt(str) = 100) {
int answer = Integer.parseInt(str);
Random random = new Random();
int result = random.nextInt(100) + 1;
while (count = 1) {
boolean r = false;
if (result Integer.parseInt(str)) {
count--;
System.out.println("你输入的数字过小,你还有" + count + "次机会");
} else if (result Integer.parseInt(str)) {
count--;
System.out.println("你输入的数字过大,你还有" + count + "次机会");
} else {
r = true;
count--;
System.out.println("恭喜你回答正确");
num++;
break;
}
if (!r) {
System.out.println("再挑一个数字吧");
str = scanner.next();
}
}
} else {
System.out.println("请输入正确的数字");
}
System.out.println("你已经成功答对" + num + "次");
}
我之前写的猜数字的游戏
java 找出字符串的数字
像这种当然用正则表达式了,专门干这个的。看我写的代码,亲测可以运行
public class FindNum {
public static void main(String[] args) {
ListString reslist = findNum("15中文中文 5中文中文 中文40元中文20中文");
for (String res : reslist) {
System.out.println(res);
}
}
public static ListString findNum(String str){
ListString list=new ArrayListString();
String regex="\\d+";
Pattern pattern = Pattern.compile(regex);
Matcher matcher = pattern.matcher(str);
while (matcher.find()) {
list.add(matcher.group(0));
}
return list;
}
}
Java数组,查找数字:输入5个整数,并查找出指定的整数位置
你好,提问者:
如果解决了你的问题,请采纳,若有疑问请追问,谢谢!
package com.zyx.cn.baidu_exam;
import java.util.Scanner;
/**
* 输入5个整数,并查找出指定的整数位置
* @author yltd
*/
public class ChaZhao {
public static void main(String[] args) {
int[] arr =new int[5];
Scanner sc =new Scanner(System.in);
for (int i = 1; i = 5; i++) {
System.out.print("请输入第"+i+"个整数:");
int number = sc.nextInt();
arr[i-1] = number;
}
System.out.println("查找数组是否存在这个整数:");
int num = sc.nextInt();
for (int i = 0; i 5; i++) {
if(num == arr[i]){
System.out.println(num+":在数组中的下标:"+i);
}
}
}
}
结果:
请输入第1个整数:10
请输入第2个整数:60
请输入第3个整数:70
请输入第4个整数:80
请输入第5个整数:90
查找数组是否存在这个整数:
80
80:在数组中的下标:3
用java来写:查找数字
package image;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
public class LookingFile
{
public static final String FILE = "查找前.txt";
public static final String RESULT = "查找后.txt";
public static final String VALUES = "0,3,5,6,9";
public static ListString looking ( String file )
{
int index = -1;
String head = "", body = "";
String reg = "\\s+";
ArrayListString bucket = new ArrayListString ();
Scanner scanner = null;
try
{
scanner = new Scanner (new File (file), "UTF-8").skip ("[\u4e00-\u9fff\\s]+");
while (scanner.hasNextLine ())
{
index++;
String[] line = scanner.nextLine ().split (reg);
head = line[0].trim ();
body = line[1].trim ();
if (VALUES.indexOf (body) != -1)
{
if (bucket.contains ("#"))
{
for ( int i = 0; i bucket.size (); i++ )
{
if ("#".equals (bucket.get (i)))
{
bucket.set (i, String.valueOf (index - i));
}
}
}
}
if (!"0".equals (head))
{
bucket.add ("a");
}
else
{
bucket.add ("#");
}
}
for ( int i = 0; i bucket.size (); i++ )
{
if ("#".equals (bucket.get (i)))
{
bucket.set (i, "空");
}
}
bucket.set (bucket.size () - 1, "No");
}
catch (Exception e)
{
e.printStackTrace ();
}
finally
{
if (null != scanner)
{
scanner.close ();
}
}
return bucket;
}
public static void writing ( ListString bucket, String file, String result )
{
int index = -1;
String line = null;
FileReader fr = null;
BufferedReader br = null;
FileWriter fw = null;
BufferedWriter bw = null;
try
{
fr = new FileReader (file);
br = new BufferedReader (fr);
fw = new FileWriter (result);
bw = new BufferedWriter (fw);
while (null != ( line = br.readLine () ))
{
line = line.trim ();
if (line.matches ("\\d+\\s+\\d+"))
{
index++;
bw.write (line + "\t\t" + bucket.get (index) + System.getProperty ("line.separator"));
}
}
bw.flush ();
bw.close ();
fw.close ();
br.close ();
fr.close ();
}
catch (Exception e)
{
e.printStackTrace ();
}
}
public static void main ( String[] args )
{
writing (looking (FILE), FILE, RESULT);
}
}
寻找数字java的介绍就聊到这里吧,感谢你花时间阅读本站内容,更多关于寻找数字的规律的方法、寻找数字java的信息别忘了在本站进行查找喔。