Qingdao Regional Competition 2017 (Part of the Questions)

Keywords: Programming Java PHP iOS

Topic link on vjudge: VJ

H Chinese Zodiac

//garbage
#include<bits/stdc++.h>
using namespace std;
int judge(char s[])
{
	if(strcmp(s,"rat")==0)
	   return 1;
	else if(strcmp(s,"ox")==0)
	   return 2;
	else if(strcmp(s,"tiger")==0)
	   return 3;
	else if(strcmp(s,"rabbit")==0)
	   return 4;
	else if(strcmp(s,"dragon")==0)
	   return 5;
	else if(strcmp(s,"snake")==0)
	   return 6;
	else if(strcmp(s,"horse")==0)
	   return 7;
	else if(strcmp(s,"sheep")==0)
	   return 8;
	else if(strcmp(s,"monkey")==0)
	   return 9;
	else if(strcmp(s,"rooster")==0)
	   return 10;
	else if(strcmp(s,"dog")==0)
	   return 11;
	if(strcmp(s,"pig")==0)
	   return 12;
}
int main()
{
	int t;
	char s1[10];
	char s2[10];
	scanf("%d",&t);
	while(t--)
	{
		cin>>s1>>s2;
		if(strcmp(s1,s2)==0)
		   {
		   	  printf("12\n");
		   	  continue;
		   }
		else
		{
			int ans1=judge(s1);
			int ans2=judge(s2);
			if(ans2>ans1)
			   cout<<ans2-ans1<<endl;
			else
			   cout<<ans2-ans1+12<<endl;
		}
	}
	return 0;
}

It's better to use map. It's all covered up.  

#include<bits/stdc++.h>
using namespace std;
string a[15]={"rat","ox","tiger","rabbit","dragon","snake","horse","sheep","monkey","rooster","dog","pig"};
map<string,int> mmp;
void init()
{
	
	for(int i=1;i<=12;i++)
	{
		mmp[a[i]]=i;
	}
	
}
int main()
{
	int T;
	init();
	scanf("%d",&T);
	while(T--)
	{
		string s1,s2;
		cin >> s1 >> s2;
		int x1=mmp[s1],x2=mmp[s2];
		if(x1 == x2)
		{
			printf("12\n");
		}
		else if(x2 > x1)
			printf("%d\n",x2-x1);
		else
			printf("%d\n",(x2+12-x1)%12);
	}
	
	
	return 0;
}

K A Cubic number and A Cubic Number

The main thing is thinking. First of all, we know the formula: a^3-b^3= (a-b)* (a ^ 2 + a B + B ^ 2), p is a prime number, so (a-b) = 1 is an important point. Knowing that this problem is solved.

#include<bits/stdc++.h>
using namespace std;
#define ll long long
int main()
{
    int T;
    scanf("%d",&T);
    while(T--)
	{
        ll n;
        scanf("%lld",&n);
        ll  i,flag=0,t=1;
        while(3*t*t+3*t+1<=n)
		{
            if(3*t*t+3*t+1==n)
			{
                printf("YES\n");
                flag=1;
            }
            t++;
        }
        if(!flag)
            printf("NO\n");
 
    }
    return 0;
}

A Apple

High-speed use of java, see the expression, began to doubt life.

import java.math.*;
import java.util.*;
//a=((y2-y1)*(y3*y3-y1*y1+x3*x3-x1*x1)-(y3-y1)*(y2*y2-y1*y1+x2*x2-x1*x1))/(2.0*((x3-x1)*(y2-y1)-(x2-x1)*(y3-y1)));
//b=((x2-x1)*(x3*x3-x1*x1+y3*y3-y1*y1)-(x3-x1)*(x2*x2-x1*x1+y2*y2-y1*y1))/(2.0*((y3-y1)*(x2-x1)-(y2-y1)*(x3-x1)));
public class Main {
    public static void main(String[] args) {
    	 BigDecimal x1,x2,x3,y1,y2,y3,x,y,x0,y0,r;
         BigDecimal a,b,c,d,e,f,g,num,ans;
         Scanner cin = new Scanner(System.in);
         int n;
         n=cin.nextInt();
         while(n>0)
         {
             x1  = cin.nextBigDecimal();
             y1  = cin.nextBigDecimal();
             x2  = cin.nextBigDecimal();
             y2  = cin.nextBigDecimal();
             x3  = cin.nextBigDecimal();
             y3  = cin.nextBigDecimal();
             x  = cin.nextBigDecimal();
             y  = cin.nextBigDecimal();
             a = x3.subtract(x2).multiply(BigDecimal.valueOf(2));// 2*(x3-x2)
             b = y3.subtract(y2).multiply(BigDecimal.valueOf(2));// 2*(y3-y2)
             c = x3.pow(2).subtract(x2.pow(2)).add(y3.pow(2).subtract(y2.pow(2)));
               //x3^2-x2^2+(y3^2-y2^2)
             e = x2.subtract(x1).multiply(BigDecimal.valueOf(2));//2*(x2-x1)
             f = y2.subtract(y1).multiply(BigDecimal.valueOf(2));//2*(y2-y1)
             g = x2.pow(2).subtract(x1.pow(2)).add(y2.pow(2).subtract(y1.pow(2)));//x2^2-(x1^2)+(y2^2-y1^2)
             x0=g.multiply(b).subtract(c.multiply(f)).divide(e.multiply(b).subtract(a.multiply(f)));//(g*b-(c*f))/(e*b-a*f)
             y0=a.multiply(g).subtract(c.multiply(e)).divide(a.multiply(f).subtract(b.multiply(e)));//(a*g-c*e)/(a*f-b*e)
             num = (x1.subtract(x0)).pow(2).add((y1.subtract(y0)).pow(2));//(x1-x0)^2+(y1-y0)^2
             ans = (x.subtract(x0)).pow(2).add((y.subtract(y0)).pow(2));
             if (ans.compareTo(num)>0) {
                 System.out.println("Accepted");
             }
             else
                 System.out.println("Rejected");
            n--;
         }
    }

}

C The Dominator of Strings

The idea is fairly clear, that is, to see if the longest string contains other strings. However, the common method will certainly overtime, not easy to achieve, once again witnessed the strength of JAVA.

Here is Buffered Reader's introduction: BufferedReader

draw lessons from

import java.io.*;
public class Main {
    public static void main(String[] args) throws IOException {
        BufferedReader bf=new BufferedReader(new InputStreamReader(System.in));
        String str[]=new String[100010];
        String t=bf.readLine();
        int T=Integer.parseInt(t);
        while(T>0)
        {
        	String n=bf.readLine();
        	int N=Integer.parseInt(n);
    		String news="";
        	for(int i=0;i<N;i++)
        	{
        		str[i]=bf.readLine();
        		if(str[i].length()>news.length())
        			news=str[i];
        	}
        	int flag=1;
        	for(int i=0;i<N;i++)
        	{
        		if(!news.contains(str[i]))
        		{
        			flag=0;
        			break;
        		}
        	}
        	if(flag==1) System.out.println(news);
        	else System.out.println("No");
        	T--;
        }
        
    }
}

The find() function is absolute. Learn to find() Click to transfer.

#include<bits/stdc++.h>
using namespace std;
string str[100010];
int t,n;
int main()
{
	ios::sync_with_stdio(false);
	int n,t;
    scanf("%d",&t);
    while(t--)
    {
        int flag=1;
        scanf("%d",&n);
        getchar();
        for(int i=1;i<=n;i++)
        {
            cin>>str[i];
        }
        for(int i=1;i<=n;i++)
        {
            int ans=0;
            for(int j=1;j<=n;j++)
            {
                if(str[i].find(str[j])!=-1) 
				  ans++;
                else break;
            }
            if(ans==n)
            {
                cout<<str[i]<<endl;
                flag=0;
                break;
            }
        }
        if(flag) printf("No\n");
    }
    return 0;
}

 

Posted by michaelpalmer7 on Sat, 02 Feb 2019 16:57:16 -0800