1. 程式人生 > >2017青島區域賽

2017青島區域賽

A. Apple

###Problem Description

Apple is Taotao’s favourite fruit. In his backyard, there are three apple trees with coordinates (x1,y1), (x2,y2), and (x3,y3). Now Taotao is planning to plant a new one, but he is not willing to take these trees too close. He believes that the new apple tree should be outside the circle which the three apple trees that already exist is on. Taotao picked a potential position (x,y) of the new tree. Could you tell him if it is outside the circle or not?

###Input

The first line contains an integer T, indicating that there are T(T≤30) cases.
In the first line of each case, there are eight integers x1,y1,x2,y2,x3,y3,x,y, as described above.
The absolute values of integers in input are less than or equal to 1,000,000,000,000.
It is guaranteed that, any three of the four positions do not lie on a straight line.

###Output

For each case, output “Accepted” if the position is outside the circle, or “Rejected” if the position is on or inside the circle.

###Sample Input

3
-2 0 0 -2 2 0 2 -2
-2 0 0 -2 2 0 0 2
-2 0 0 -2 2 0 1 1

###Sample Output

Accepted
Rejected
Rejected

###Source

輸入輸出測試

程式碼塊

####錯誤程式碼
在做這一個題目時,我首先考慮到的是找三角形的外接圓,然後確定外心,確定外接圓的半徑。
在確定了外接圓的半徑和圓心後,我們就很容易判斷第四個點是否在圓內了。
步驟:
1.計算外接圓圓心
2.計算外接圓半徑
3.判斷點是否在圓內
4.在圓內輸出“Rejected”,不在圓內輸出“Accepted”
但是最終的結果是錯誤的!!

//錯誤程式碼
#include<stdio.h>//runtime error 除零錯誤   
#include<iostream>  
#include<math.h>  
using namespace std;  
int main()  
{  
  
        int x0,y0,x1,y1,x2,y2,x3,y3;  
        int x4,y4;  
        double dis,r;  
        int n;  
        cin>>n;  
        for(int i=0;i<n;i++)  
        {  
            cin>>x1>>y1>>x2>>y2>>x3>>y3>>x4>>y4;              
            x0 = ((y3-y1)*(y2*y2-y1*y1+x2*x2-x1*x1)+(y2-y1)*(y1*y1-y3*y3+x1*x1-x3*x3))/(2*(x2-x1)*(y3-y1)-2*(x3-x1)*(y2-y1));  
            y0 = ((x3-x1)*(x2*x2-x1*x1+y2*y2-y1*y1)+(x2-x1)*(x1*x1-x3*x3+y1*y1-y3*y3))/(2*(y2-y1)*(x3-x1)-2*(y3-y1)*(x2-x1));  
            r  = sqrt((x1-x0)*(x1-x0)+(y1-y0)*(y1-y0));  
            dis= sqrt((x4-x0)*(x4-x0)+(y4-y0)*(y4-y0));  
            if(dis <= r)  
            {   
            cout<<"Rejected"<<endl;  
            }  
             
            else  
            { 
			 cout<<"Accepted"<<endl;  
            }   
    }  
    return 0;  
} 

下面看這樣一個程式碼

public class ssss {  
  
    public static void main(String[] ages){  
        double d1=2.07;  
        double d2=1.03;  
        System.out.println(d1+d2);  
    }  
} 

其輸出結果為
3.0999999999999996

雖然計算結果離精確值誤差很小,但其不是精確的!這在像如金融計算一樣計算精確度要求很高的領域是無法接受的,但這是二進位制本身的問題,而計算機普遍採用二進位制表示,使用基本資料型別無法解決。

為了解決基本資料型別浮點數不能進行精確計算的問題,Java中專門提供了java.math.BigDecimal類,其提供浮點數的精確計算功能。與BigInteger類相同,其運算操作均使用方法呼叫完成

以下是java.math.BigDecimal.multiply()方法宣告
public BigDecimal multiply(BigDecimal multiplicand)
BigDecimal.valueOf(qty)是把qty這個數轉為BigDecimal型別的,BigDecimal是可以處理任意長度的浮點數運算的。

Ⅰ基本函式:
1.valueOf(parament); 將引數轉換為制定的型別
String s=”12345”; BigInteger c=BigInteger.valueOf(s); 則c=12345;
BigInteger a=new BigInteger(“23”); BigInteger b=new BigInteger(“34”); a. add(b); 3.subtract(); 相減
5.divide(); 相除取整
6.remainder(); 取餘
8.gcd(); 最大公約數
10.negate(); 取反數
11.mod(); a.mod(b)=a%b=a.remainder(b);
13.punlic int comareTo();
14.boolean equals(); 是否相等
15.BigInteger建構函式: 一般用到以下兩種: BigInteger(String val); 將指定字串轉換為十進位制表示形式; BigInteger(String val,int radix); 將指定基數的 BigInteger 的字串表示形式轉換為 BigInteger
Ⅱ.基本常量: A=BigInteger.ONE 1
C=BigInteger.ZERO 0
Ⅲ.基本操作

  1. 讀入: 用Scanner類定義物件進行控制檯讀入,Scanner類在java.util.*包中
    Scanner cin=new Scanner(System.in);// 讀入 while(cin.hasNext()) //等同於!=EOF { int n; BigInteger m; n=cin.nextInt(); //讀入一個int;
    m=cin.BigInteger();//讀入一個BigInteger; System.out.print(m.toString()); }
package 大數問題;

import java.math.BigDecimal;
import java.math.BigInteger;
import java.util.Scanner;

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,tt,dis;
        Scanner cin = new Scanner(System.in);
        int ncase;
        ncase = cin.nextInt();
        while(ncase-->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)));
          x0=g.multiply(b).subtract(c.multiply(f)).divide(e.multiply(b).subtract(a.multiply(f)));
               y0=a.multiply(g).subtract(c.multiply(e)).divide(a.multiply(f).subtract(b.multiply(e)));
            tt = (x1.subtract(x0)).pow(2).add((y1.subtract(y0)).pow(2));//(x1-x0)^2+(y1-y0)^2
            dis = (x.subtract(x0)).pow(2).add((y.subtract(y0)).pow(2));
            if (dis.compareTo(tt)>0) {//dis.compareTo(tt) 相當於 dis與tt相比較 類比strcmp()
                System.out.println("Accepted");
            }
            else
                System.out.println("Rejected");
        }
    }

}

H. Chinese Zodiac

The Chinese Zodiac, known as Sheng Xiao, is based on a twelve-year cycle, each year in the cycle related to an animal sign. These signs are the rat, ox, tiger, rabbit, dragon, snake, horse, sheep, monkey, rooster, dog and pig.
Victoria is married to a younger man, but no one knows the real age difference between the couple. The good news is that she told us their Chinese Zodiac signs. Their years of birth in luner calendar is not the same. Here we can guess a very rough estimate of the minimum age difference between them.
If, for instance, the signs of Victoria and her husband are ox and rabbit respectively, the estimate should be 22 years. But if the signs of the couple is the same, the answer should be 1212 years.
Input
The first line of input contains an integer T (1≤T≤1000)T (1≤T≤1000) indicating the number of test cases.
For each test case a line of two strings describes the signs of Victoria and her husband.
Output
For each test case output an integer in a line.

Sample Input
3
ox rooster
rooster ox
dragon dragon
Sample Output
8
4
12

#include<stdio.h> 
#include<stdlib.h> 
#include<iostream> 
#include<algorithm> 
#include<string.h>
using namespace std; 
int num1,num2,num=12;
//rat, ox, tiger, rabbit, dragon, snake, horse, sheep, monkey, rooster, dog and pig
int s(char *str)
{
	if(strcmp(str,"rat")==0)
	 return 1;
	if(strcmp(str,"ox")==0)
	 return 2;
	if(strcmp(str,"tiger")==0)
	 return 3;
	if(strcmp(str,"rabbit")==0)
	 return 4;
	if(strcmp(str,"dragon")==0)
	 return 5;
	if(strcmp(str,"snake")==0)
	 return 6;
	if(strcmp(str,"horse")==0)
	 return 7;
	if(strcmp(str,"sheep")==0)
	 return 8;
	if(strcmp(str,"monkey")==0)
	 return 9;
	if(strcmp(str,"rooster")==0)
	 return 10;
	if(strcmp(str,"dog")==0)
	 return 11;
	if(strcmp(str,"pig")==0)
	 return 12;	 

}
int main(){
  int T;
  char str1[20],str2[20];
  scanf("%d",&T);
  while(T--)
  {
     scanf("%s%s",str1,str2);
	  num1=s(str1); 
	  num2=s(str2);   
	 int ans=num2-num1;
	 if(ans>0)
	 {
	 	printf("%d\n",ans);
	 }else
	 {
	 	ans=ans+12;
	 	printf("%d\n",ans);
	 }
  }
  return 0;	
}

K.A Cubic number and A Cubic Number

A cubic number is the result of using a whole number in a multiplication three times. For example, 3×3×3=273×3×3=27 so 2727 is a cubic number. The first few cubic numbers are 1,8,27,641,8,27,64 and 125125. Given an prime number pp. Check that if pp is a difference of two cubic numbers.
Input
The first of input contains an integer T (1≤T≤100)T (1≤T≤100) which is the total number of test cases.
For each test case, a line contains a prime number p (2≤p≤1012)p (2≤p≤1012).
Output
For each test case, output ‘YES’ if given pp is a difference of two cubic numbers, or ‘NO’ if not.

Sample Input
10
2
3
5
7
11
13
17
19
23
29
Sample Output
NO
NO
NO
YES
NO
NO
NO
YES
NO
NO

公式推導:

#include<stdio.h> 
#include<stdlib.h> 
#include<iostream> 
#include<algorithm> 
#include<string.h>
using namespace std;
typedef long long int ll;
bool flag=0;
int main()
{
    ll T,p;
    double n;
    scanf("%lld",&T);
    while(T--)
    {
    	flag=0;
    	scanf("%lld",&p);
    	ll ans;
        for(ll i=1;(3*i*i+3*i+1)<=p;i++)
		{
		  if((3*i*i+3*i+1)==p)
		  {
		    	flag=1;
		    	printf("YES\n");
		    	break;
     	  }      	
		}
		if(flag==0)
		{
		   printf("NO\n");
		}	
	}
    return 0;
}



相關推薦

2017青島區域 (部分題解)

vjudge上 題目連結:VJ //垃圾 #include<bits/stdc++.h> using namespace std; int judge(char s[]) { if(strcmp(s,"rat")==0) return 1; el

2017青島區域

A. Apple ###Problem Description Apple is Taotao’s favourite fruit. In his backyard, there are three apple trees with coordinates (

2017 ACM區域現場 青島站 E (polya計數)

amp mod 循環 等價 img cout es2017 範圍 知乎 題目鏈接(暫無) 吐槽:這場比賽感覺對我感覺還算友好,雖然Q群知乎上命題方已經被噴死了,C語言上機題還有字符串題有大腿隊友輕松搞定,網絡流恰是我能想出來的,E本來也應該是在能力範圍內,不過因為之前沒寫過

2017 ACM 區域青島站(現場) K Our Journey of Xian Ends

Life is a journey, and the road we travel has twists and turns, which sometimes lead us to unexpected places and unexpected people. Now our journey of Xia

HDU 6214 Smallest Minimum Cut 2017青島1009(最小割最小割邊)

target app left shu info lis 最小 get qt5 j99r2xn磊乩叛8http://jz.docin.com/crv79826 B烈04lN186u氨事28http://shufang.docin.com/xei31846 GzD93Co

2017 ACM區域(南寧站) 參賽流水賬

字符 暴力 through abc 參數 ble 搜索 推公式 stay day0:  早上四點起床趕飛機,還好沒有吵醒室友導致被打死。本來想在飛機上準備一下下周的小測,結果飛機一點都不平穩,只能全程和隊友吹逼聊天。下午在賓館裏和johann通關了一部合金彈頭,重溫了童年的

2017 ICPC區域(西安站)--- J題 LOL(DP)

nds 我們 要求 tle 思路 strings padding back 全排列 題目鏈接 problem description 5 friends play LOL together . Every one should BAN one characte

2017烏魯木齊區域A(動態規劃,組合數學,期望)

ble set 可能 組合 name main i++ return soft #include<bits/stdc++.h>using namespace std;double c[110][110];double g[110];double dp[110]

UVALive - 7740 Coding Contest 2016 青島區域 (費用流)

endif bool namespace spa pre mat -- stdin main 題意:每個點i有\(s_i\)個人和\(b_i\)份食物,每個人都要找到一份食物.現在有M條有向邊,從點i到點j,容量為c,第一次走過不要緊,從第二次開始就要承擔\(p(0<

2018ICPC青島區域 zoj4062 Plants vs. Zombies

  BaoBao and DreamGrid are playing the game Plants vs. Zombies. In the game, DreamGrid grows plants to defend his garden against BaoBao's zombies.

2018青島區域J(BOOK)

#include<bits/stdc++.h> #define inf 0x3f3f3f3f using namespace std; long long sum; int n,m; int num[100009]; int main() { int t; scanf

Tournament ZOJ - 4063 (青島區域 F 打表)

打表題。。 規律是找出來了 奈何優化不了 。。。。 #include <iostream> #include <cstdio> #include <sstream> #include <cstring> #include <map> #in

2018青島區域M:Function and Function

  按照題目中所給出的公式來計算數最後會在0和1之間變換,所以只要在數變成1的時候計算剩餘次方數對2取餘是多少就可以知道最後的答案是多少。 #include <stdio.h> int mm[] = {1,0,0,0,1,0,1,0,2,1}; int mmm

2018青島區域J:Books

 從左到右一次掃n本書,如果當前錢包裡面的錢大於書的價錢,就買下來(必須買),問最多能帶多少錢,錢數必須是非負整數,如果沒有方案,就輸出Impossible,如果可以帶無限的錢,就輸出Richman. 先找出0的個數,因為0元的是必須要買的,如果0的數量大於需要的數量,

2018青島區域C:Flippy Sequence

   題意:找出l,r在此區域內的所有數0變為1,1變為0,進行兩次操作,問一共有多少種方法。 兩個串相同就是兩個串的異或值為0 把兩個串異或, 找出連續的1的有多少塊。 1.兩塊以上的無法用兩次操作達到效果,所以為0 2.全為1的有(n-1)*

ACM 2017 北京區域 J-Pangu and Stones(區間dp)

HihoCoder - 1636 題目大意:       有n堆石子,每次可以選擇連續的一段合併,最少l個,最多r個,每次合併的花費為這些堆的石子的和,問最小花費是多少 題解:      訓

ACM 2018 青島區域 J-Books (模擬)

ZOJ - 4067 題目大意:       DG去書店買書,有n本書按順序放置。買書的策略是從1到n本,遇到價格<=當前手中的錢的書就買       現在告訴你每本書的價格和DG總共買了多少

ACM 2017 香港區域 F-Nearby Bicycles(模擬)

https://nanti.jisuanke.com/t/19930 (uva有問題,計蒜客可以交) 題目大意:       有m輛共享單車,n個人,分別給出他們的座標,每個人有一個距離上限。    

2018 青島區域 C - Flippy Sequence

                        &nbs

2018 青島區域 J - Books

                        &nbs