解题思路:
注意事项:
参考代码:
public static void main(String[] args) { Scanner scanner = new Scanner(System.in); while (scanner.hasNext()) { int k = scanner.nextInt(); int w = scanner.nextInt(); int h = w%k;//多余的位数作为最高位 int n = w/k+1;//此数值有多少 位 int max= (int) Math.pow(2,k)-1; int high= (int) (Math.pow(2,h)-1); int count=0; if (k>9||k<0) break; else { for(int i=2;i<n;i++) //从第二位开始(从右往左),对每一位数(除最高位)计算排列数 count+=c(max,i); // System.out.println(count+"hig"); if (high!=0){//最高位排列数用减法思维 count+=(c(max,n)-c(max-high,n));//减法 //System.out.println(count+"high"); // System.out.println(count); } System.out.println(count); } } } public static int c(int n, int m){//m表示当前位置 int count=1; int i; for(i=1;i<=m;i++){ count*=(n-i+1); //System.out.println(count+"h"); } for(i=2;i<=m;i++){ count=count/i; // System.out.println(count+"k"); } //return sum; return count; }
0.0分
3 人评分
C语言训练-自守数问题 (C语言代码) long long浏览:1067 |
C语言程序设计教程(第三版)课后习题8.8 (C语言代码)浏览:642 |
水仙花 (C语言代码)浏览:1062 |
C语言程序设计教程(第三版)课后习题11.3 (C语言代码)浏览:577 |
DNA (C语言代码)浏览:744 |
链表数据求和操作 (C语言代码)浏览:953 |
C语言程序设计教程(第三版)课后习题10.5 (C语言代码)浏览:958 |
简单的a+b (C语言代码)浏览:647 |
P1001 (Java代码)浏览:693 |
C语言程序设计教程(第三版)课后习题6.9 (C语言代码)简单版浏览:789 |