如题,要求输出这两个数组中相同的元素个数(counter),
还有就是对应下标上相同元素的个数(location);例如:arr1={1,2,3,4}; arr2={4,5,6,7};
则:counter=1,location=0;
下面是我试着写的,总感觉不够完善(注释部分的功能和下面的代码重复了),
大家帮我改一下,或者有更高效的代码分享一下,不胜感激![code=java]
import java.util.Scanner;
public class Compare
{
private static Scanner scan;
public static void numCompare()
{
int counter = 0; //猜对的数字
int location = 0; //猜对的位置
scan = new Scanner(System.in);
int sysRandom = scan.nextInt();
int userInput = scan.nextInt();
int[] sysArr = { sysRandom/1000,
sysRandom%1000/100,
sysRandom%1000%100/10,
sysRandom%1000%100%10 };
int[] userArr = { userInput/1000,
userInput%1000/100,
userInput%1000%100/10,
userInput%1000%100%10 };
for(int i=0;i<4;i++)
{
if(userArr[i]==sysArr[i])
{
counter++;
}
// for(int j=0;j<4;j++)
// {
// if(userArr[j]==sysArr[i]&&i==j)
// {
// location++;
// }
// }
}
System.out.println("counter:"+counter);
System.out.println("location:"+location);
}
}
class Test
{
public static void main(String[] args)
{
Compare.numCompare();
}
}
[code=java]Java编程语言求助
还有就是对应下标上相同元素的个数(location);例如:arr1={1,2,3,4}; arr2={4,5,6,7};
则:counter=1,location=0;
下面是我试着写的,总感觉不够完善(注释部分的功能和下面的代码重复了),
大家帮我改一下,或者有更高效的代码分享一下,不胜感激![code=java]
import java.util.Scanner;
public class Compare
{
private static Scanner scan;
public static void numCompare()
{
int counter = 0; //猜对的数字
int location = 0; //猜对的位置
scan = new Scanner(System.in);
int sysRandom = scan.nextInt();
int userInput = scan.nextInt();
int[] sysArr = { sysRandom/1000,
sysRandom%1000/100,
sysRandom%1000%100/10,
sysRandom%1000%100%10 };
int[] userArr = { userInput/1000,
userInput%1000/100,
userInput%1000%100/10,
userInput%1000%100%10 };
for(int i=0;i<4;i++)
{
if(userArr[i]==sysArr[i])
{
counter++;
}
// for(int j=0;j<4;j++)
// {
// if(userArr[j]==sysArr[i]&&i==j)
// {
// location++;
// }
// }
}
System.out.println("counter:"+counter);
System.out.println("location:"+location);
}
}
class Test
{
public static void main(String[] args)
{
Compare.numCompare();
}
}
[code=java]Java编程语言求助
解决方案 »
- 运行applet程序,java.exe突然自动关闭
- Exception in thread "AWT-EventQueue-0"
- 问题~
- java调用shell问题
- [Microsoft][ODBC 驱动程序管理器] 未发现数据源名称并且未指定默认驱动程序
- 关于流的一个问题
- 请教:一个java的applet程序问题
- 新手提问:热心人请进(非常感谢)
- 关于ECLIPSE的插件VE.EMF.GEF.................朋友!请进!(解决问题都另开贴加100分,分有的是)!
- 设置路径时,%CLASSPATH% 是什么意思啊?
- 各位Java技术达人,急求助,谢谢?
- 【求助】关于数组元素变换的问题探讨
另外你的代码是不是把location和counter写反了。。 public static void findNumOfMatches(final int[] generated, final int[] input) {
Map<Integer, Integer> generatedMap = new HashMap<Integer, Integer>();
for (int i = 0; i < generated.length; i++) {
generatedMap.put(generated[i], i);
} int total = 0;
int totalLocations = 0;
for (int i = 0; i < input.length; i++) {
Integer location = generatedMap.remove(input[i]);
if (location != null) {
total++;
if (location == i)
totalLocations++;
}
} System.out.println("Total correct numbers: " + total);
System.out.println("Correct locations: " + totalLocations);
}
public class Test01 {
public static void main(String[] args) {
final int[] arr1 = new int[] { 1, 2, 3, 4 };
final int[] arr2 = new int[] { 4, 5, 6, 7 };
find(arr1, arr2);
} static void find(int[] arr1, int[] arr2) {
int N = arr1.length;
int counter = 0, location = 0;
//假设数字范围为[0,9]
int[] result = new int[10];
for(int i = 0;i < N;++i) {
result[arr1[i]]++;
result[arr2[i]]++;
if(arr1[i] == arr2[i]) {
location++;
}
}
for(int i = 0;i < result.length;++i) {
if(result[i] == 2) {//或者result[i] > 1
counter++;
}
}
System.out.printf("counter:%d,location:%d",counter,location);
}
}
static int counter,loc;
public static void count(int[] a,int[] b) {
int[] m = new int[a.length];
for(int i=0;i<a.length;i++) {
for(int j=0;j<b.length;j++) {
if(a[i]==b[j]) {
counter++;
}
}
m[i] = counter;
counter = 0;
}
for(int i=0;i<m.length;i++) {
System.out.println("数组b中与a["+i+"]相同的有"+m[i]+"个数");
}
}
public static void location(int[] a,int[] b) {
if(a.length>b.length) {
int[] c = a;
a = b;
b = c;
}
for(int i=0;i<a.length;i++) {
if(a[i]==b[i]) {
loc++;
}
}
System.out.println("数组a中与b中相同下标且值相同的有"+loc+"个");
}
public static void main(String[] args) {
int[] a1 = {1,2,3,4,5,6,9,10,8,5};
int[] a2 = {1,2,3,4,5,6,9,10,8,5,1,2,3,4,5,6,9,10,8,5,1,2,3,4,5,6,9,10,8,5,1,2,3,4,5,6,9,10,8,5};
count(a1,a2);
location(a2,a1);
}
}
数组b中与a[0]相同的有4个数
数组b中与a[1]相同的有4个数
数组b中与a[2]相同的有4个数
数组b中与a[3]相同的有4个数
数组b中与a[4]相同的有8个数
数组b中与a[5]相同的有4个数
数组b中与a[6]相同的有4个数
数组b中与a[7]相同的有4个数
数组b中与a[8]相同的有4个数
数组b中与a[9]相同的有8个数
数组a中与b中相同下标且值相同的有10个