博主的语言是java,第一题过了,第二题没看懂,后面师兄解释是选集合...才做出来,第三题不知道为什么一直30%,逻辑检查了好几遍没问题。
第一题是等式计算,直接贴代码:
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner sc=new Scanner(System.in);
String s;
int x=0,i,res=0,t=-1;
char a[];
s=sc.next();
a=s.toCharArray();
for(i=0;i<a.length;i++)
{
if(a[i]=='+')
{
if(t==0)res+=x;
else if(t==1)res-=x;
else res=x;
t=0;
x=0;
}
else if(a[i]=='-')
{
if(t==0)res+=x;
else if(t==1)res-=x;
else res=x;
t=1;
x=0;
}
else if(a[i]=='=')
{
if(t==0)res+=x;
else if(t==1)res-=x;
else res=x;
x=0;
break;
}
else x=x*10+(a[i]-(int)'0');
}
if(a[i-1]!='=')
{
if(t==0)res+=x;
else if(t==1)res-=x;
}
System.out.println(res);
}
}
第二题选集合,可以统计全部字符的数量,然后再重复选择最长的区间输出,没做,下面的代码是后面写的,做为参考,贴代码:
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner sc=new Scanner(System.in);
String s;
s=sc.next();
char c;
int a[],A[],i,max,maxl,maxr,l,r;
a=new int[26];
A=new int[26];
for(i=0;i<s.length();i++)
{
c=s.charAt(i);
if(c>='a' && c<='z')a[c-'a']++;
else if(c>='A' && c<='Z')A[c-'A']++;
}
while(true)
{
l=-1;
r=-1;
max=0;
maxl=-1;
maxr=-1;
for(i=0;i<26;i++)
{
if(a[i]>0 && A[i]>0)
{
if(l==-1)l=i;
}
else
{
if(l!=-1)
{
r=i-1;
if(max<r-l+1)
{
maxl=l;
maxr=r;
max=r-l+1;
}
l=-1;
}
}
}
if(maxl==-1)break;
for(i=maxl;i<=maxr;i++)
{
System.out.print((char)('A'+i)+""+(char)('a'+i));
a[i]--;
A[i]--;
}
System.out.println();
}
}
}
第三题,不知道为什么只过了30%,就是一些if逻辑判断...
import java.util.Hashtable;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner sc=new Scanner(System.in);
String s,a[],b[];
int i;
Hashtable<String, String> ht=new Hashtable<String, String>();
s=sc.nextLine();
b=s.split(" ");
for(i=0;i<b.length;i++)
{
a=b[i].split("\\|");
if(a[0].equalsIgnoreCase("RmApp"))
{
if(a[2].equalsIgnoreCase("start"))
{
if(!ht.containsKey(a[1]))
{
ht.put(a[1],"submitted");
System.out.print(a[1]+"|submitted;");
}
}
else if(a[2].equalsIgnoreCase("kill"))
{
if(ht.containsKey(a[1]) && !ht.get(a[1]).equalsIgnoreCase("finished"))
{
ht.put(a[1],"killed");
System.out.print(a[1]+"|killed;");
}
}
}
else if(a[0].equalsIgnoreCase("ResouceScheduler"))
{
if(ht.containsKey(a[1]) && ht.get(a[1]).equalsIgnoreCase("submitted") && a[2].equalsIgnoreCase("app_accepted"))
{
ht.put(a[1], "scheduled");
System.out.print(a[1]+"|scheduled;");
}
else if(ht.containsKey(a[1]) && ht.get(a[1]).equalsIgnoreCase("running") && a[2].equalsIgnoreCase("finished"))
{
ht.put(a[1], "finished");
System.out.print(a[1]+"|finished;");
}
}
else if(a[0].equalsIgnoreCase("RmContainer"))
{
if(ht.containsKey(a[1]) && ht.get(a[1]).equalsIgnoreCase("scheduled") && a[2].equalsIgnoreCase("container_allocated"))
{
ht.put(a[1], "allocated");
System.out.print(a[1]+"|allocated;");
}
}
else if(a[0].equalsIgnoreCase("ApplicationMasterLauncher"))
{
if(ht.containsKey(a[1]) && ht.get(a[1]).equalsIgnoreCase("scheduled") && a[2].equalsIgnoreCase("launched"))
{
ht.put(a[1], "running");
System.out.print(a[1]+"|running;");
}
}
}
}
}