首页 > 编程语言 >Java面向对象2(G~J)

Java面向对象2(G~J)

时间:2023-02-14 13:03:43浏览次数:37  
标签:Java int ans1 ans2 面向对象 sc new Sum


G    织女的红线(SDUT 2240)

import java.util.Scanner;
import java.text.DecimalFormat;

class Sum {
double x1, y1, x2, y2;

Sum(double n1, double m1, double n2, double m2) {
x1 = n1;
x2 = n2;
y1 = m1;
y2 = m2;
}

double getAns() {
double ans = 0;
ans = (x1 - x2) * (x1 - x2) + (y1 - y2) * (y1 - y2);
ans = Math.sqrt(ans);
return ans;
}
}

public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
DecimalFormat df = new DecimalFormat(".00");
int t, r;
double ans = 0;
double x[] = new double[200];
double y[] = new double[200];
t = sc.nextInt();
r = sc.nextInt();
for (int i = 1; i <= t; i++) {
x[i] = sc.nextDouble();
y[i] = sc.nextDouble();
}
x[t + 1] = x[1];
y[t + 1] = y[1];
Sum p;
for (int i = 1; i <= t; i++) {
p = new Sum(x[i], y[i], x[i + 1], y[i + 1]);
ans += p.getAns();
}
ans += 2 * r * 3.1415926;
System.out.println(df.format(ans));
}
}

H     分数加减法(SDUT 2253)

import java.util.Scanner;
import java.text.DecimalFormat;

class Sum {
int x1, y1, x2, y2;
char str;

Sum(int n1, int m1, int n2, int m2, char op) {
x1 = n1;
x2 = n2;
y1 = m1;
y2 = m2;
str = op;
}

int getGcd(int a, int b) {
int n = a, m = b;
while (m > 0) {
int x = n;
n = m;
m = x % m;
}
return n;
}

void getAns() {
int x = getGcd(y1, y2);
int a, b, c, d, ans1, ans2;
a = x1;
b = y1;
c = x2;
d = y2;
int lcm = b * d / x;
a = a * d / x;
c = c * b / x;
if (str == '+')
ans1 = a + c;
else
ans1 = a - c;
ans2 = lcm;
if (ans1 < 0)
x = -ans1;
else
x = ans1;
x = getGcd(x, ans2);
if (ans1 % x == 0 && ans2 % x == 0) {
ans1 /= x;
ans2 /= x;
}
if (ans1 == 0 && ans1 != ans2 || ans2 == 1)
System.out.println(ans1);
else if (ans1 == ans2)
System.out.println(1);
else
System.out.println(ans1 + "/" + ans2);
}
}

public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
// DecimalFormat df = new DecimalFormat(".00");
Sum p;
String s;
char op;
while (sc.hasNext()) {
s = sc.next();
// System.out.println(s);
int x1 = s.charAt(0) - '0';
int y1 = s.charAt(2) - '0';
op = s.charAt(3);
int x2 = s.charAt(4) - '0';
int y2 = s.charAt(6) - '0';
// System.out.println(x1 + " " + y1 + " " + x2 + " " + y2 + " " + op);
p = new Sum(x1, y1, x2, y2, op);
p.getAns();
}
}
}

高中数学?(SDUT 2400)

import java.util.*;

public class Main {

public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int t = sc.nextInt();
for (int i = 0; i < t; i++) {
int n = sc.nextInt();
Sum p = new Sum(n);
int ans = p.getAns(n);
System.out.println(ans);
}
sc.close();
}
}

class Sum {
int a[] = new int[55];
int n;

public Sum(int n) {
a[1] = 0;
a[2] = 1;
for (int i = 3; i <= 50; i++) {
a[i] = 4 * a[i - 1] - 5 * a[i - 2];
}
this.n = n;
}

public int getAns(int n) {
return a[n];
}
}

最大矩形面积(SDUT 2401)

import java.lang.reflect.Array;
import java.util.*;

public class Main {

public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int t = sc.nextInt();
int n,l,w;
node s[] = new node[2000];
for (int i = 0; i < t; i++) {
l = sc.nextInt();
w = sc.nextInt();
n = sc.nextInt();
for(int j = 0; j < n; j ++)
{
s[j] = new node();
s[j].x = sc.nextInt();
s[j].y = sc.nextInt();
}
//Arrays.sort(s,0,n,new cmp());
Sum p = new Sum(l,w,n,s);
if(n == 0)
System.out.println(l * w);
else
System.out.println(p.getAns());
}
sc.close();
}
}

class node
{
int x;
int y;
}
class cmp implements Comparator<node>
{
public int compare(node a, node b)
{
if(a.x - b.x != 0) return a.x - b.x;
else return a.y - b.y;
}
}
class cmp1 implements Comparator<node>
{
public int compare(node a, node b)
{
if(a.y - b.y != 0) return a.y - b.y;
else return a.x - b.x;
}
}
class Sum {
int l,w,n;
node s[] = new node[2000];
Sum(int l, int w, int n, node s[])
{
this.l = l;
this.w = w;
this.n = n;
this.s = s;
}
int max(int a, int b)
{
if(a >= b) return a;
else return b;
}
int min(int a, int b)
{
if(a >= b) return b;
else return a;
}
int getAns1()
{
Arrays.sort(s,0,n,new cmp1());
int i,j,ans1;
ans1 = 0;
for (i = 0; i < n; ++i)
{
int L = 0, R = l;
for (j = i + 1; j < n; ++j)
{
if (s[i].y != s[j].y)
{
ans1 = max(ans1,(s[j].y - s[i].y)*(R - L));
if (s[j].x > s[i].x) R = min(R,s[j].x);
else L = max(L,s[j].x);
}
}
}
return ans1;
}
int getAns2()
{
Arrays.sort(s,0,n,new cmp());
int i,j,ans2;
ans2 = 0;
for (i = 0; i < n; ++i)
{
int top = w, down = 0;
for (j = i + 1; j < n; ++j)
{
if (s[i].x != s[j].x)
{
ans2 = max(ans2,(s[j].x - s[i].x)*(top - down));
if (s[j].y > s[i].y) top = min(top,s[j].y);
else down = max(down,s[j].y);
}
}
}
return ans2;
}
int getAns()
{
int ans,ans1,ans2;
ans1 = getAns1();
ans2 = getAns2();
ans = max(ans1,ans2);
return ans;
}
}

 

标签:Java,int,ans1,ans2,面向对象,sc,new,Sum
From: https://blog.51cto.com/u_15965659/6056620

相关文章

  • Java面向对象6(AA ~ AE)
    AE 简单的复数运算(类和对象)(SDUT4303)importjava.util.*;classComplex{ inta,b; Complex(){ } Complex(intn,intm){ a=n; b=m; } voi......
  • Java面向对象3(K~O)
     K    正方形(SDUT2444)importjava.lang.reflect.Array;importjava.util.*;publicclassMain{ publicstaticvoidmain(String[]args){ Scanners......
  • JAVA-studyDay02
    java-day02一.注释//书写注释是非常好的习惯//单行注释////多行注释/**///JavaDoc:文档注释/***///平时写代码一定要注意规范注意:注释不会影响代码二.运算符1.......
  • rxjava之复习
    1.rxjava之操作符1).转换类操作符(mapflatMapconcatMapflatMapIterableswitchMapscangroupBy...);map及flatMap以及concatMap区别?map和flatMap都可以对RxJava传入......
  • java根据地址获取百度API经纬度
    java根据地址获取百度API经纬度(详细文档)1publicvoidgetLarLng(Stringaddress)throwsException{23Stringak="vZ5wAkH9uc6mCnrhtYWey2fBHBmU9Rh......
  • Java 通过get post 请求url
    .已获取小程序的access_token为例,通过Get请求url1importcom.alibaba.fastjson.JSONObject;23StringwechatUrl="https://api.weixin.qq.com/cgi-bin/tok......
  • java删除字符串最后一位
    Strings="1,2,3,4,5,6,7,8,";//目标:删除最后一个","s=s.substring(0,s.length()-1);System.out.println(s); ......
  • eclipse 为javaWeb项目更改jdk版本的正确姿势
    1.情景展示在使用eclipse进行web项目开发时,针对公司以前的老项目,从SVN下载下来后,我们往往会需要更换jdk版本,使其与本地jdk版本保持一致。如何切换jdk版本?2.具体实现第一步:切......
  • 从混沌到规范:JavaScript模块化方案的演进史
    前言JavaScript语言诞生至今,模块规范化之路曲曲折折。社区先后出现了各种解决方案,包括AMD、CMD、CommonJS等,而后ECMA组织在JavaScript语言标准层面,增加了模块功能(因......
  • JavaScript-面向对象的理解
    Everythingisobject(万物皆对象)作为开发大家都非常熟悉的一句话。(1)对象是单个事物的抽象。在生活中大的事物圈子,比如一只猫、一本书、一个人,都可以理解为对象(objec......