Home  >  Q&A  >  body text

java - Solving an algorithm problem in C language? Can anyone help me?

仅有的幸福仅有的幸福2735 days ago606

reply all(3)I'll reply

  • 大家讲道理

    大家讲道理2017-05-16 13:26:29

    I would like to say that regardless of the algorithm selection requirements, this is a primary school math problem of finding .
    When inputting, check whether a and b are both odd or even numbers and whether the number is within the available range of the super variable type, and then you must also consider this issue in the case you generate

    reply
    0
  • 为情所困

    为情所困2017-05-16 13:26:29

    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);
     
            }
        }
    }

    reply
    0
  • 淡淡烟草味

    淡淡烟草味2017-05-16 13:26:29

    Using the materials you provided, I coded it in C++ as follows. I hope you can correct me!

    /*
      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;   //input
            try{
              //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;
    }
    

    Run results

    reply
    0
  • Cancelreply