坩埚花菜嘎嘎香 2021-07-18 14:32 采纳率: 0%
浏览 78

同样的代码在IDEA能正常完成需求,在PTA平台却无输出

开始学习的Day15,代码可能繁琐,是顺着自己的思路来的
但同样的代码在IDEA能正常完成需求,在PTA平台却无输出
L1-009(刚开始学习缓慢爬行)
IDEA:

img

PTA

img

代码如下

import java.util.Scanner;
class Main {
    //计算公倍数
    public static int getGbs(int a,int b){
        int t;
        int t1=a;
        int t2=b;
        while(b!=0) {
            t=b;
            b=a%b;
            a=t;
        }
        return (t1*t2)/a;
    }
    //计算公约数
    public static int getGys(int a,int b){
        int t;
        while(b!=0) {
            t=b;
            b=a%b;
            a=t;
        }
        return a;
    }
    public static void main(String[] args) {
        Scanner dx = new Scanner(System.in);
        int num =dx.nextInt();
        Scanner gx=new Scanner(System.in);
        String s =gx.nextLine();
        String[] str =s.split("/| ");
        int[] top =new int[num];
        int[] bottom= new int[num];
        int count =0;
        //记录分子分母 索引一一对应
        for(int i =0;i<str.length;i++) {
            if(i%2==0) {
                top[count]=Integer.parseInt(str[i]);
            } else {
                bottom[count]=Integer.parseInt(str[i]);
                count++;
            }
        }
        int mbottom =1;
        //找到分母的公倍数
        for(int i =0;i<bottom.length;i++) {
            mbottom =getGbs(mbottom,bottom[i]);
        }
        int mtop=0;
        for(int i=0;i<top.length;i++) {
            top[i]=top[i]*(mbottom/bottom[i]);
            mtop+=top[i];
            }
        int temp =getGys(mtop,mbottom);
        mtop/=temp;
        mbottom/=temp;
        int ansnum =0;
        //计算整数部分
        if(mtop<0) {
            int ttop=Math.abs(mtop);
            ansnum=-(int)(Math.floor(ttop/mbottom));
        } else {
            ansnum=(int)(Math.floor(mtop/mbottom));
        }
        int anstop = mtop-ansnum*mbottom;
        String ans ="";
        ans=anstop+"/"+mbottom;
        if(ansnum!=0) {
            ans=ansnum+" "+ans;
        }
        System.out.print(ans);
    }
}

展开全部

  • 写回答

2条回答 默认 最新

  • CSDN专家-sinJack 2021-07-18 16:23
    关注

    没有一点提示的吗,这种就不好追踪问题了。

    评论
  • 一杯汗水 2024-06-21 15:14
    关注

    其实这个问题很简单,你要把握问题的关键,如何把握问题的关键,就要说你要找到关键的问题,如何找到关键的问题,就是说你要把握问题的关键

    评论
编辑
预览

报告相同问题?

手机看
程序员都在用的中文IT技术交流社区

程序员都在用的中文IT技术交流社区

专业的中文 IT 技术社区,与千万技术人共成长

专业的中文 IT 技术社区,与千万技术人共成长

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

客服 返回
顶部