VIPkid笔试,编程第一题脑袋大,求助。。

编程第一题:
输入一组数,例如-1,2,-2,3,0,-0,1,5    求这个数组里两两组合,如a,b,求a+b=0;这样的数对有多少个,(a,b)和(b,a)算一个。
import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;

public class Main {

	public static void main(String[] args) {
		Scanner scanner=new Scanner(System.in);
		String[] array=scanner.nextLine().split(",");
		Map<String, Integer> map=new HashMap<>();
		int count=0;
		for (int i = 0; i < array.length-1; i++) {
			if (!map.containsKey(array[i])) {
				map.put(array[i], 1);
			}
			for (int j = 1; j < array.length; j++) {
				
				if ((Integer.valueOf(array[i])+Integer.valueOf(array[j])==0)||(Integer.valueOf(array[i])+Integer.valueOf(array[j])==-0)) {
					if (Integer.valueOf(array[i])==0&&Integer.valueOf(array[j])==0) {
						if (!map.containsKey("a")) {
							map.put("a", 1);
							count++;
						}
					}
					if (Integer.valueOf(array[i])==-0&&Integer.valueOf(array[j])==-0) {
						if (!map.containsKey("b")) {
							map.put("b", 1);
							count++;
						}
					}
					if (Integer.valueOf(array[i])==0&&Integer.valueOf(array[j])==-0) {
						if (!map.containsKey("c")) {
							map.put("c", 1);
							count++;
						}
					}
					if (Integer.valueOf(array[i])==-0&&Integer.valueOf(array[j])==0) {
						if (!map.containsKey("c")) {
							map.put("c", 1);
							count++;
						}
					}
					if (!map.containsKey(array[j])) {
						map.put(array[j], 1);
						count++;
					}
					
				}
			}
		}
		System.out.println(count);
		
	}
}
我实在搞不懂了,改了好几次都是33%,求助大佬找找问题。 真是60分钟几乎都在处理这个问题,第二题直接秒了。
#VIPKID##笔试题目##Java#
全部评论
 public static void main(String[] args) {         Scanner sc = new Scanner(System.in);         String str = sc.nextLine();         str = str.replace(" ","");         String[] split =   str.split(",");         int[] a = new int[split.length];         for(int i = 0; i< a.length; i++) {             a[i] = Integer.valueOf(split[i]);         }         long res = findRes(a);         System.out.println(res);     }     private static long findRes(int[] a) {         Arrays.sort(a);         int i = 0;         int j = a.length -1;         int res = 0;         while (i < j) {             if(a[i] + a[j] == 0) {                 i++;                 j--;                 res ++;             } else {                 j--;             }         }         return res;     } 贴一个 AC 的代码,用双指针做。
点赞 回复 分享
发布于 2019-09-03 18:37
#include <iostream> #include <algorithm> #include <vector> #include <string> #include <unordered_map> #include <iomanip> #include <set> using namespace std; int main() {     vector<int> nums;     do{         int num;         cin >> num;         nums.push_back(num);     }while(cin.get() != '\n');     set<int> st(nums.begin(), nums.end());     nums.assign(st.begin(), st.end());     sort(nums.begin(), nums.end());     int count = 0;     for(int i = 0; i < nums.size(); ++i)     {         for(int j = nums.size() - 1; j >= i; --j)         {             if(nums[j] + nums[i] == 0)             {                 count ++;                 break;             }         }     }     cout << count << endl;     return 0; } 贴一个ac代码
点赞 回复 分享
发布于 2019-09-03 18:59
一样33%,第二题秒过
点赞 回复 分享
发布于 2019-09-03 18:48
没看到空格,也是33%😓
点赞 回复 分享
发布于 2019-09-03 18:27
import java.util.*; public class Main{ public static void main(String[] args) { Scanner scanner = new Scanner(System.in); String[] str = scanner.nextLine().split(","); int[] nums = new int[str.length]; for (int i = 0; i < str.length; i++) { nums[i] = Integer.parseInt((str[i]).trim()); } HashSet<Integer> set=new HashSet<>(); for(int i:nums) set.add(i); Iterator<Integer> it=set.iterator(); ArrayList<Integer> list=new ArrayList<>(); while (it.hasNext()){ int n=it.next(); list.add(n); } int count=0; for(int i=0;i<list.size();i++){ for(int j=i+1;j<list.size();j++){ if(list.get(i)+ list.get(j)==0) count++; } } System.out.println(count); } }
点赞 回复 分享
发布于 2019-09-03 18:26
直接用linkedhashset去重,再放到list里边,代码要简单很多
点赞 回复 分享
发布于 2019-09-03 18:25
trim去空格,然后去掉数组内重复的,然后就可以AC了
点赞 回复 分享
发布于 2019-09-03 18:24
String数组中的每个字符串是由数字和空格组成的,将空格去掉,再转为数字。
点赞 回复 分享
发布于 2019-09-03 18:21
空格吧,trim一下就行了
点赞 回复 分享
发布于 2019-09-03 18:21
没想到空格
点赞 回复 分享
发布于 2019-09-03 18:18
我觉得算一个,我用arraylist,有就去一个,不过我想到空格,弄来弄去都只有百分之三十三
点赞 回复 分享
发布于 2019-09-03 18:18
我也是,简直崩溃
点赞 回复 分享
发布于 2019-09-03 18:17
他的输入数字两侧有空格
点赞 回复 分享
发布于 2019-09-03 18:11

相关推荐

认真搞学习:这么良心的老板真少见
点赞 评论 收藏
分享
评论
1
6
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务