N个数求和(Java)
2022/1/26 14:04:40
本文主要是介绍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); } } } }
这篇关于N个数求和(Java)的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!
- 2025-01-11有哪些好用的家政团队管理工具?
- 2025-01-11营销人必看的GTM五个指标
- 2025-01-11办公软件在直播电商前期筹划中的应用与推荐
- 2025-01-11提升组织效率:上级管理者如何优化跨部门任务分配
- 2025-01-11酒店精细化运营背后的协同工具支持
- 2025-01-11跨境电商选品全攻略:工具使用、市场数据与选品策略
- 2025-01-11数据驱动酒店管理:在线工具的核心价值解析
- 2025-01-11cursor试用出现:Too many free trial accounts used on this machine 的解决方法
- 2025-01-11百万架构师第十四课:源码分析:Spring 源码分析:深入分析IOC那些鲜为人知的细节|JavaGuide
- 2025-01-11不得不了解的高效AI办公工具API