java新手求解题:网上看到这题

weixin_41805681 2018-03-08 09:30:55

在一次答辩的比赛中,一共有10个题目,假设最开始的时候,每个选手都有10分,每个选手必须答完,每道题,要是答对了当前的分数是原来的两倍。要是答错了,当前的分数减去当前的题号。假设答对了该题目显示的是1,错的是0,若一个选手得到的分数是100.那么可以求出这个选手答对了几题吗
...全文
353 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
weixin_41805681 2018-03-13
  • 打赏
  • 举报
回复
在f(a)这行
weixin_41805681 2018-03-13
  • 打赏
  • 举报
回复
看了你的答案有启发,想自己用递归写一个,先把能不能得到这个分算出来,我想法是这个最后的分数是又前一题乘以2或者+题号得到的,所以倒着算,但是报错了,能再帮我看看嘛?报错:Exception in thread "main" java.lang.StackOverflowError public class TextScore { static int n = 10; public static void main(String args[]) { int s = 100; f(s); } public static void f( int s ) { if(s == 10 && n == 0) { System.out.print("it can be got"); } else if( n > 0) { if(s % 2 ==1) { s = s - n; f(s); } else { int a = s; int b = s; a = a/2; b = b-n; f(a); f(b); n--; } } else System.out.println("invaild score"); } }
weixin_41805681 2018-03-13
  • 打赏
  • 举报
回复
谢谢~~~~~
greadworld 2018-03-09
  • 打赏
  • 举报
回复
package com.qm;

public class Test6 {
public static void main(String[] args) {
int[] a = new int[10];
int[] b = new int[10];
answer(1, 10, a, b);
}

public static void answer(int i, int sum, int[] ans, int[] anse) {
if (sum == 100&&i==11) {
for (int j : ans) {
System.out.print(j + ",");
}
System.out.println();
for (int k : anse) {
System.out.print(k + ",");
}
System.out.println();
System.out.println("结束轮次为:"+(i-1));
}
if (i > 10) {
return;
}
add(i, sum, ans, anse);
sub(i, sum, ans, anse);
}

public static void add(int i, int sum, int[] ans, int[] anse) {
sum = sum * 2;
ans[i-1] = 1;
anse[i-1] = sum;
i++;
answer(i, sum, ans, anse);
}

public static void sub(int i, int sum, int[] ans, int[] anse) {
sum = sum - i;
ans[i-1] = 0;
anse[i-1] = sum;
i++;
answer(i, sum, ans, anse);
}
}
greadworld 2018-03-09
  • 打赏
  • 举报
回复
答案:1110010000

23,404

社区成员

发帖
与我相关
我的任务
社区描述
Java 非技术区
社区管理员
  • 非技术区社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧