编程语言
首页 > 编程语言> > N个数求和(Java)

N个数求和(Java)

作者:互联网

 用Java写N个数求和

测试三一直过不去,看了很多文章发现可能是因为分子分母各自相加时超过longlong的范围,以下代码是没有修改过不了测试三的代码。我通过查找分母之间的最大公倍数然后进行运算。

代码如下:

import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Main m = new Main();
        Scanner sc = new Scanner(System.in);
        sc.nextInt();
        sc.nextLine();
        String a = sc.nextLine();
        String [] b ;
        int flag = 0;
        //按空格拆分数据,分为String数组b
        b = a.split("\\s+");
        //按除号分开数据
        String [] c ;
        int up = 0 , down = 0;
        int minnumber = 0;
        int total = 0;
        for (int i = 0; i < b.length-1; i++) {
            c = b[i].split("/+");
            up = Integer.parseInt(c[0]);
            down = Integer.parseInt(c[1]);
            c = b[i+1].split("/+");
            minnumber = m.FindMin(down,Integer.parseInt(c[1]),flag);
            total = m.Addup(up,down,c,minnumber);
            b[i+1] = total+"/"+minnumber;
        }
        m.Print(b[b.length-1]);
    }
    //查找最小公倍数
    public int FindMin(int a, int b ,int flag){
        int min = a<b? a:b ;
        int max = a>b? a:b ;
        int minnumber = min ;
        while(max%min!=0){
            minnumber = max%min;
            max = min;
            min = minnumber;
        }
        if(flag == 0){
            minnumber = a * b / minnumber;
            return minnumber;
        }
        else {
            return min;
        }
    }
    //两个分子相加
    public int Addup(int ups1 ,int downs1, String [] s2 ,int minnumber){
        int ups2 = Integer.parseInt(s2[0]);
        int downs2 = Integer.parseInt(s2[1]);
        ups1 = ups1*(minnumber/downs1);
        ups2 = ups2*(minnumber/downs2);
        return ups1+ups2;
    }

    public void Print(String a){
        String [] c = a.split("/+");
        String b = "";
        int flag = 1 ;
        int divisor = 0;
        int upnumber = Integer.parseInt(c[0]);
        int downnumber = Integer.parseInt(c[1]);
        if(upnumber == 0){
            System.out.println("0");
        }
        else if(Math.abs(upnumber) < Math.abs(downnumber) ){
            divisor = FindMin(upnumber,downnumber,flag);
            upnumber /= divisor;
            downnumber /= divisor;
            if(upnumber < 0 && downnumber > 0 || upnumber > 0 && downnumber<0){
                upnumber = Math.abs(upnumber);
                downnumber = Math.abs(downnumber);
                System.out.println("-"+upnumber + "/" + downnumber);
            }
            else{
                System.out.println(upnumber + "/" + downnumber);
            }
        }
        else if(upnumber%downnumber == 0){
            System.out.println(upnumber/downnumber);
        }
        else{
            b += upnumber/downnumber;
            upnumber -= downnumber*(upnumber/downnumber);
            divisor = FindMin(upnumber,downnumber,flag);
            upnumber /= divisor;
            downnumber /= divisor;
            if(upnumber > 0 && downnumber > 0) {
                System.out.println(b + " " + upnumber + "/" + downnumber);
            }
            else if(upnumber > 0 && downnumber <0 || upnumber < 0 && downnumber >0){
                upnumber = Math.abs(upnumber);
                downnumber = Math.abs(downnumber);
                System.out.println(b + " " +"-"+ upnumber + "/" + downnumber);
            }
            else{
                upnumber = Math.abs(upnumber);
                downnumber = Math.abs(downnumber);
                System.out.println(b + " " + upnumber + "/" + downnumber);
            }
        }
    }
}

 

标签:upnumber,Java,String,minnumber,求和,个数,int,downnumber,parseInt
来源: https://blog.csdn.net/weixin_51944182/article/details/122699770