我是靠谱客的博主 高贵大碗,最近开发中收集的这篇文章主要介绍2017青岛区域赛A. AppleH. Chinese ZodiacK.A Cubic number and A Cubic Number,觉得挺不错的,现在分享给大家,希望可以做个参考。

概述

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;
}

####正确代码
参考大神代码:http://www.cnblogs.com/chenssy/archive/2012/09/09/2677279.html

下面看这样一个代码

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("%dn",ans);
}else
{
ans=ans+12;
printf("%dn",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("YESn");
break;
}
}
if(flag==0)
{
printf("NOn");
}
}
return 0;
}

最后

以上就是高贵大碗为你收集整理的2017青岛区域赛A. AppleH. Chinese ZodiacK.A Cubic number and A Cubic Number的全部内容,希望文章能够帮你解决2017青岛区域赛A. AppleH. Chinese ZodiacK.A Cubic number and A Cubic Number所遇到的程序开发问题。

如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(52)

评论列表共有 0 条评论

立即
投稿
返回
顶部