有股神吗?有,小赛就是!经过严密的计算,小赛买了一支股票,他知道从他买股票的那天开始,股票会有以下变化:第一天不变,以后涨一天,跌一天,涨两天,跌一天,涨三天,跌一天…依此类推。为方便计算,假设每次涨和跌皆为1,股票初始单价也为1,请计算买股票的第n天每股股票值多少钱?
import java.util.Scanner;
public class Main {
/*
* 股神问题
*/
public static void main(String[] args) {
Scanner s = new Scanner(System.in);
while (s.hasNext()) {
int n = s.nextInt();
System.out.println(Cal(n));
}
s.close();
}
public static int Cal(int n) {
int result = 1;
boolean flag = true;
int step = 1;
for (int i = 2,j=0; i <= n; i++) {
if (j <step) {
j++;
} else {
j=0;
step++;
flag=false;
}
if (flag) {
result += 1;
} else {
result -= 1;
flag=true;
}
}
return result;
}
}
用1,2,3……,9组成3个三位数abc,def和ghi,每个数字恰好使用一次,要abc:def:ghi=1:2:3.输出所有解。
import java.util.Arrays;
public class Compare {
public static void main(String[] args) {
int[] a = new int[9];
for(int i = 123;i <= 987;i++) {
int x=0 , y=0 , f = 0;
x = i*2;
y = i*3;
a[0] = i/100;
a[1] = i/10%10;
a[2] = i%10;
a[3] = x/100;
a[4] = x/10%10;
a[5] = x%10;
a[6] = y/100;
a[7] = y/10%10;
a[8] = y%10;
Arrays.sort(a);
for(int j = 0;j <= a.length-2;j++) {
if(a[j] == a[j+1]) {
f = 1;
break;
}
if(a[j] == 0) {
f = 1;
break;
}
}
if(f == 0 && x <1000 && y < 1000) {
System.out.println(i + " " + x + " " + y);
}
}
}
}