java - 求解一道C语言的算法题?求各位大神帮忙
问题描述
问题解答
回答1:我想说不考虑算法选择要求 这就是个 2x+b = a 求x的小学数学题,如果对算法选择没要求就是输入a,b,输出(a-b)/2 和 (a+b)/ 2.输入的时候校验下a,b是不是同为奇数或偶数以及数字是不是超变量类型的可用范围,然后自己生成的case也要考虑这个问题
回答2:import java.math.BigInteger;import java.util.*;import java.io.*; public class Main { public static void main(String[] args) {Scanner input = new Scanner(System.in); String total, more;for(int t = 0; t < 10; ++t) { total = input.nextLine(); more = input.nextLine();; BigInteger total_apples = new BigInteger(total); BigInteger excess_apples = new BigInteger(more); BigInteger ans_1 = total_apples.add(excess_apples).pide(BigInteger.valueOf(2)); BigInteger ans_2 = total_apples.subtract(excess_apples).pide(BigInteger.valueOf(2)); System.out.println(ans_1); System.out.println(ans_2); } }}回答3:
通过你提供的材料,用c++编码如下,望指正!
/* if Natalia’s number of apples is x (x>0) apples:the number of apple extra:the number that Klaudia more tha Natalia (apples>extra>0) so x+extra+x=apples x = (apples-extra)/2*/#include <iostream>int main(int argc, char const *argv[]) { int apples,extra; //apples:the number of apple extra:the number that Klaudia more tha Natalia for (size_t i = 0; i < 10; i++) {std::cin>>apples>>extra; //inputtry{ //To determine whether the input is legal if(apples<=0 || extra<=0 || apples<extra || (apples-extra)%2!=0) throw apples; int Klaudia,Natalia; Natalia = (apples-extra)/2; //calculate Klaudia=Natalia+2; std::cout<<Klaudia<<’n’<<Natalia<<’n’; //output }catch(int e){ //ERROR std::cerr << 'ERROR! the number of applse is error.n' << ’n’;} } return 0;}
运行结果
相关文章:
1. 求救一下,用新版的phpstudy,数据库过段时间会消失是什么情况?2. 请问一下各位老鸟 我一直在学习独孤九贱 现在是在tp5 今天发现 这个系列视频没有实战3. docker start -a dockername 老是卡住,什么情况?4. 关于docker下的nginx压力测试5. golang - 用IDE看docker源码时的小问题6. redis与mysql一致性问题7. javascript - 页面引入的所有的js文件,每个js文件都有window.onload偶尔会某个js文件没有执行8. linux默认python版本升级成2.7后怎么安装mysql包9. javascript - 在vuejs中如何根据变量去引用不同的css样式呢10. mysql问题请教

网公网安备