P1003 铺地毯(java)
没想到居然就这么通过了。。。(以为会超时。。。)
从最后一块地毯开始遍历,如果有就输出,全部遍历完都没有则输出-1
import java.util.Scanner;
public class Main {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int arr[][]= new int[n][4];
for(int i=0;i<n;i++){
arr[i][0] = sc.nextInt();
arr[i][1] = sc.nextInt();
arr[i][2] = sc.nextInt();
arr[i][3] = sc.nextInt();
}
int x = sc.nextInt();
int y = sc.nextInt();
int flag = 0;
for(int i=n-1;i>=0;i--){
if(x>=arr[i][0]&&y>=arr[i][1]&&x<=arr[i][0]+arr[i][2]&&y<=arr[i][1]+arr[i][3]){
flag=1;
System.out.println(i+1);
break;
}
}
if(flag==0){
System.out.println(-1);
}
}
}
P1067 多项式输出
这个题需要考虑得情况很多种,一不留神就会忽略某一种情况
1、要判断第一个进入s串得不是一个为0 得,而且 如果是正数没有+号,负数得话需要‘-’号
2、+1,-1得时候,系数都不显示1
3、当指数为1的时候,输出为‘x’,当指数为0 时,只输出系数。
import java.util.Scanner;
public class Main {
/**
* @param args
*/
static String s ="";
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int a[] = new int[n+1];
for(int i=0;i<n+1;i++){
a[i] = sc.nextInt();
}
for(int i=0;i<n+1;i++){
String t = pan(n-i);
if(n-i==0) {
t = String.valueOf(a[i]);
}
if(s.length()==0) {
if(n==0) {
s = s+String.valueOf(a[i]);
}else {
if(a[i]>0) {
if(a[i]==1) {
s = s+t;
}else {
s = s+String.valueOf(a[i])+t;
}
}else if(a[i]<0){
if(a[i]==-1) {
s = s+"-"+t;
}else {
s = s+String.valueOf(a[i])+t;
}
}else {
s = s;
}
}
}
else {
if(a[i]>0) {
if(a[i]==1) {
s = s+"+"+t;
}else {
if(n-i==0) {
s = s+"+"+t;
}else {
s = s+"+"+String.valueOf(a[i])+t;
}
}
}else if(a[i]<0) {
if(a[i]==-1) {
if(n-i==0) {
s = s+t;
}else {
s = s+"-"+t;
}
}else {
if(n-i==0) {
s = s+t;
}else {
s = s+String.valueOf(a[i])+t;
}
}
}else{
s =s;
}
}
}
System.out.println(s);
}
private static String pan(int i) {
// TODO Auto-generated method stub
if(i==1) {
return "x";
}else if(i==0) {
return "";
}else {
return "x"+"^"+String.valueOf(i);
}
}
}
P1540 机器翻译
import java.util.ArrayList;
import java.util.Scanner;
public class Main {
static ArrayList<Integer> e = new ArrayList<>();
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int M = sc.nextInt();
int N = sc.nextInt();
int arr[] = new int[N];
for(int i=0;i<N;i++) {
arr[i] = sc.nextInt();
}
int count=0;
for(int i=0;i<N;i++) {
if(!e.contains(arr[i])) {
if(e.size()>=M) {
e.remove(0);
e.add(arr[i]);
count++;
}else {
e.add(arr[i]);
count++;
}
}
}
System.out.println(count);
}
}