阿里笔试养猪代码分享
import java.io.*;
import java.util.*;
import java.text.*;
import java.math.*;
import java.util.regex.*;
import org.junit.Test;
public class test1 {
/** 请完成下面这个函数,实现题目要求的功能 **/
/** 当然,你也可以不按照这个模板来作答,完全按照自己的想法来 ^-^ **/
static String calculate(int m, int k) {
StringBuffer sb = new StringBuffer();
sb.append(getNum(m));
sb.append(",");
sb.append(getYear(m));
sb.append(",");
sb.append(maxRev(m, k));
return sb.toString();
}
@Test
public void test() {
int m=20;
int a[]=new int[m];
for(int i=0;i<m;i++) { // 反转数组
StringBuffer sb=new StringBuffer();
sb.append(getNum(i+1));
sb.reverse();
String s=sb.toString();
int num=Integer.parseInt(s);
a[i]=num;
}
Arrays.sort(a);
System.out.println(Arrays.toString(a));
}
static int maxRev(int m,int k) {
int a[]=new int[m];
for(int i=0;i<m;i++) { // 反转数组
StringBuffer sb=new StringBuffer();
sb.append(getNum(i+1));
sb.reverse();
String s=sb.toString();
int num=Integer.parseInt(s);
a[i]=num;
}
int b[]=new int[m];
for(int i=0;i<m;i++) { //原数组
b[i]=getNum(i+1);
}
Arrays.sort(a);//升序排序
int result=a[m-k];
String temp=""+result;
StringBuffer sbb=new StringBuffer(temp);
sbb.reverse();
result=Integer.parseInt(sbb.toString());
int index=0;
for(index=0;index<m;index++) {
if(result==b[index]) break;
}
return index+1;
}
static int getYear(int m) {
int i = 0;
int sum = 0;
while (true) {
sum=0;
for (int k = 0; k <= i; k++) {
sum += newBorn(k);
}
if (sum >= m)
break;
i++;
}
return i + 2019;
}
static int newBorn(int i) {
if (i == 0)
return 2;
if (i == 1)
return 1;
if (i == 2)
return 2;
if (i == 3)
return 3;
int sum = 0;
for (int k = 0; k <= i - 2; k++) {
sum += newBorn(k);
}
return sum;
}
public static int getNum(int m) {
if (m == 1)
return 2;
if (m == 2)
return 3;
if (m == 3)
return 4;
return getNum(m - 2) + getNum(m - 3);
}
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
String[] line = in.nextLine().split(",");
int m = Integer.valueOf(line[0]);
int k = Integer.valueOf(line[1]);
System.out.println(calculate(m, k));
}
}
#阿里巴巴##笔试题目#
import java.util.*;
import java.text.*;
import java.math.*;
import java.util.regex.*;
import org.junit.Test;
public class test1 {
/** 请完成下面这个函数,实现题目要求的功能 **/
/** 当然,你也可以不按照这个模板来作答,完全按照自己的想法来 ^-^ **/
static String calculate(int m, int k) {
StringBuffer sb = new StringBuffer();
sb.append(getNum(m));
sb.append(",");
sb.append(getYear(m));
sb.append(",");
sb.append(maxRev(m, k));
return sb.toString();
}
@Test
public void test() {
int m=20;
int a[]=new int[m];
for(int i=0;i<m;i++) { // 反转数组
StringBuffer sb=new StringBuffer();
sb.append(getNum(i+1));
sb.reverse();
String s=sb.toString();
int num=Integer.parseInt(s);
a[i]=num;
}
Arrays.sort(a);
System.out.println(Arrays.toString(a));
}
static int maxRev(int m,int k) {
int a[]=new int[m];
for(int i=0;i<m;i++) { // 反转数组
StringBuffer sb=new StringBuffer();
sb.append(getNum(i+1));
sb.reverse();
String s=sb.toString();
int num=Integer.parseInt(s);
a[i]=num;
}
int b[]=new int[m];
for(int i=0;i<m;i++) { //原数组
b[i]=getNum(i+1);
}
Arrays.sort(a);//升序排序
int result=a[m-k];
String temp=""+result;
StringBuffer sbb=new StringBuffer(temp);
sbb.reverse();
result=Integer.parseInt(sbb.toString());
int index=0;
for(index=0;index<m;index++) {
if(result==b[index]) break;
}
return index+1;
}
static int getYear(int m) {
int i = 0;
int sum = 0;
while (true) {
sum=0;
for (int k = 0; k <= i; k++) {
sum += newBorn(k);
}
if (sum >= m)
break;
i++;
}
return i + 2019;
}
static int newBorn(int i) {
if (i == 0)
return 2;
if (i == 1)
return 1;
if (i == 2)
return 2;
if (i == 3)
return 3;
int sum = 0;
for (int k = 0; k <= i - 2; k++) {
sum += newBorn(k);
}
return sum;
}
public static int getNum(int m) {
if (m == 1)
return 2;
if (m == 2)
return 3;
if (m == 3)
return 4;
return getNum(m - 2) + getNum(m - 3);
}
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
String[] line = in.nextLine().split(",");
int m = Integer.valueOf(line[0]);
int k = Integer.valueOf(line[1]);
System.out.println(calculate(m, k));
}
}
#阿里巴巴##笔试题目#