java - 求解一道C語(yǔ)言的算法題?求各位大神幫忙
問(wèn)題描述
問(wèn)題解答
回答1:我想說(shuō)不考慮算法選擇要求 這就是個(gè) 2x+b = a 求x的小學(xué)數(shù)學(xué)題,如果對(duì)算法選擇沒(méi)要求就是輸入a,b,輸出(a-b)/2 和 (a+b)/ 2.輸入的時(shí)候校驗(yàn)下a,b是不是同為奇數(shù)或偶數(shù)以及數(shù)字是不是超變量類型的可用范圍,然后自己生成的case也要考慮這個(gè)問(wèn)題
回答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:
通過(guò)你提供的材料,用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;}
運(yùn)行結(jié)果
相關(guān)文章:
1. 怎么php怎么通過(guò)數(shù)組顯示sql查詢結(jié)果呢,查詢結(jié)果有多條,如圖。2. 求大神支招,php怎么操作在一個(gè)html文件的<head>標(biāo)記內(nèi)添加內(nèi)容?3. php - 數(shù)據(jù)庫(kù)表如果是null怎么替換為其他字段的值4. mysql - 數(shù)據(jù)庫(kù)建字段,默認(rèn)值空和empty string有什么區(qū)別 1105. 致命錯(cuò)誤: Class ’appfacadeTest’ not found6. mysql - JAVA怎么實(shí)現(xiàn)一個(gè)DAO同時(shí)實(shí)現(xiàn)查詢兩個(gè)實(shí)體類的結(jié)果集7. javascript - mysql插入數(shù)據(jù)時(shí)怎樣避免與庫(kù)中的數(shù)據(jù)重復(fù)?8. mysql建表報(bào)錯(cuò),查手冊(cè)看不懂,求解?9. shell - Update query wrong in MySQL10. sql語(yǔ)句 - 如何在mysql中批量添加用戶?
