网上的,收集代码

1.输入一个有符号整数,输出该整数的反转值。

import java.util.Scanner;
 
public class Main {
    public static int reverse(int x) {
        int rev = 0;
        while (x != 0) {
            rev = rev * 10 + x % 10;
            x /= 10;
        }
        return rev;
    }
 
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        int input = in.nextInt();
        System.out.println(reverse(input));
    }
}

2.给定整数n,取若干个1到n的整数可求和等于整数m,编程求出所有组合的个数。比如当n=6,m=8时,有四种组合:[2,6], [3,5], [1,2,5], [1,3,4]。限定n和m小于120

public class Main {
    public static void main(String[] args){
        Scanner scan=new Scanner(System.in);
        int n=scan.nextInt();
        int m=scan.nextInt();
        if(n>=120||m>=120)
            System.exit(1);
        System.out.println(getCount(n,m));
    }
    //0-1背包问题  f(n,m) 转化为两个子问题 f(n-1,m) 和 f(n-1,m-n)
    public static int getCount(int n, int m){
        if(m<1 || n<1) 
            return 0;
        int sum=0;
        if (n>=m) {
            sum+=getCount(m-1, m)+1;
        } else {
            sum+=getCount(n-1, m);
            sum+=getCount(n-1, m-n);
        }
        /*if(m<n) n=m;
        int sum=0;
        if(m==n)sum++;
        //不选中n
        sum+=getCount(n-1,m);
        //选中n
        sum+=getCount(n-1,m-n);*/
        
        return sum;
    }
}

C++编译过程包括预编译->汇编->编译->链接(.exe)
注:代码为网上收集,忘记地址了,就不上了

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值