输入数值序列i行(0<i),每一行分别是进制和数值,以“#”分割。如:n#m, n是整数,代表n进制(1<n<17),m是n进制下的数值.
输入序列以结束符”END”结束。
m的字符集为0-9和A-F,保证数值在十进制下不超过1e9,行数不超过100001行。
输出j行(0<j<=i),每一行都是输入序列的“异数”。要求:
1.按照输入序列的原序输出;
2.如果没有”异数”,输出字符串”None”
3.结束符“END”不用输出
10#15 4#32 4#33 8#17 END
4#32
import java.util.*; public class Main{ public static void main(String[] args){ Scanner sc=new Scanner(System.in); Map<Integer,Integer> map=new LinkedHashMap<>(); Map<Integer,String> map1=new HashMap<>(); while(sc.hasNext()){ String s=sc.next(); int flag=0; if(s.equals("END")){ for(int key:map.keySet()){ if(map.get(key)==1){ flag=1; System.out.println(map1.get(key)); } } if(flag==0) System.out.println("None"); break; } String[] strs=s.split("#"); int n=Integer.parseInt(strs[0]); int num=Integer.parseInt(strs[1],n); if(map.containsKey(num)){ map.put(num,2); }else{ map.put(num,1); map1.put(num,s); } } sc.close(); } }
//package com.mingzhouL.exer; /** * @author LMZ * @create 2020-05-14-11:21 */ /* 思路:写一个方法,用于将任意进制转为十进制 主函数中,读入一个进制数就放到方法里面求结果 给出三个list分别存储输入序列、当前序列对应的十进制数、所有异数 */ import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.ArrayList; public class Main{ public static void main(String[] args)throws IOException{ BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); String str; int temp = Integer.MAX_VALUE;//用于记录相同的数值 ArrayList<String> as = new ArrayList<String>();//存储输入的序列 ArrayList<Integer> ai = new ArrayList<Integer>();//用于记录当前序列的十进制数 ArrayList<Integer> ad = new ArrayList<Integer>();//用于记录异数 while(!(str = br.readLine()).equals("END")){ as.add(str); String[] nowstr = str.split("#"); int n = Integer.parseInt(nowstr[0]); int num = xToten(n,nowstr[1]); ai.add(num); if(ad.contains(num)){ temp = num; ad.remove(new Integer(num)); } else ad.add(num); } ad.remove(new Integer(temp));//防止还有一个相同数被添加 for(int i = 0;i<ad.size();i++){ int index = ai.indexOf(ad.get(i)); System.out.println(as.get(index)); } } public static int xToten(int n,String str){ if(n==10)return Integer.parseInt(str); char[] ch = str.toCharArray(); int sum = 0; for(int i = ch.length - 1,j=0;i>=0;i--){ if(ch[i]>='0'&&ch[i]<='9') sum+=(ch[i]-'0')*(int)Math.pow(n,j++); else sum+=(ch[i]-'A'+10)*(int)Math.pow(n,j++); } //System.out.println(sum); return sum; } }有没有大神指点一下,为啥我这个只能通过13.3%呢?
import java.util.*;public class Main{
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
Map<Integer,String> map = new HashMap<>();
Listset = new ArrayList<>();
Listlist = new ArrayList<>();
//int[] nums={};
int count = 0;
while(sc.hasNext()){
String s =sc.next();
if("END".equals(s)){
break;
}
String[] split = s.split("#"); Integer num1 = Integer.valueOf(split[0]); int temp = Integer.parseInt(split[1], num1); if(list.isEmpty()||!list.contains(temp)){ list.add(temp); map.put(temp,s); set.add(temp); }else{ if(set.contains(temp)){ set.remove((Object)temp); } } } if(set.size()==0){ System.out.println("None"); }else{ for (Integer temp1 : set) { System.out.println(map.get(temp1)); } } }
}