算法练习

有股神吗?有,小赛就是!经过严密的计算,小赛买了一支股票,他知道从他买股票的那天开始,股票会有以下变化:第一天不变,以后涨一天,跌一天,涨两天,跌一天,涨三天,跌一天…依此类推。为方便计算,假设每次涨和跌皆为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);
            }
        }
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值