// N个数求和 Sum of fractions
from std import console.*
from std import convert.*
from std import collection.*
from std import math.*
from std import format.*
//import std.format.*
//import std.math.*
//import std.collection.*
//import std.console.*
//import std.convert.*
//判断输入的字符串是否为空。
func getOrThrow(p: ?String): String{
match (p) {
case Some(x) => x
case None => "none"
}
}
//判断输入的字符串是否为数字。返回真假。
func isNumber(number_string :String):Bool{
var number_rune :Array<Rune>=number_string.toRuneArray()
for(number in number_rune){
if(!number.isAsciiNumber()&&number.toString() !=" "){
if(number.toString() !="-"){
if(number.toString() != "/"){
return false
}
}
}
}
return true
}
//获取输入的数字字符串。返回字符串。
func getInputNumber():String{
var input=Console.stdIn.readln()
let input_to_strig: Option<String> = input
var input_to_strig_to :String=getOrThrow(input_to_strig)
if(isNumber(input_to_strig_to)){
return input_to_strig_to
}
return ""
}
//分割字符串。保存成数组
func getSplit(number :String):Array<String>{
if(!number.isEmpty()&&number.size>0){
var tempArrayNumber:Array<String>=number.split(" ",118,)
return tempArrayNumber
}
return []
}
//分割字符串,检测数字是否合规。 得到数字数组
func getStartAndEndNumber(number_group :Array<String>):ArrayList<Array<String>>{
if(!number_group.isEmpty()&&number_group.size>0){
var array_numbers=ArrayList<Array<String>>(number_group.size)
for (i in number_group){
if(i != " "&& !i.isEmpty()&& i.size>0){
var tempArrayI=Array<String>(3,item: "")
var iSplit :Array<String>=i.split("/",118,)
var count =0
for(j in iSplit){
if(j.size>0&&count == 0&&j.split("-",118).size==2){
var tempArray :Array<String> = j.split("-",118,)
tempArrayI[0]="-"
tempArrayI[1]=tempArray[1]
count++
}else if(j.size>0&&count==0){
tempArrayI[1]=j
count++
}else if(j.size>0&&count ==1){
tempArrayI[2]=j
count++
}
}
array_numbers.append(tempArrayI)
}
}
return array_numbers
}
return ArrayList<Array<String>>()
}
//最大公约数。
func getL(One : Int64,Two : Int64 ):Array<Int64>{
var g = gcd(One, Two)
// println("g = ${g}")
return [One/g,Two/g]
}
//求最小公约数。
func getLeastCommonDivisor(One : Int64,Two : Int64 ):Array<Int64>{
var numOne=One
var numTwo=Two
var min:Int64=0
var arrayDivisorj:Array<Int64>=[0,0]
if(numOne>0&&numTwo>0){
if(numOne==numTwo){
arrayDivisorj[0]=1
arrayDivisorj[1]=1
return arrayDivisorj
}else if(numOne>numTwo){
min=numTwo
}else{
min = numOne
}
if(numOne%min==0&&numTwo%min==0){
arrayDivisorj[0]=numOne/min
arrayDivisorj[1]=numTwo/min
return arrayDivisorj
}
var tempN:Int64=Int64.parse(sqrt(Float32.parse(
min.toString())).format(".0").toString())
for(i in 2..tempN){
while(numOne%i==0&&numTwo%i==0){
arrayDivisorj[0]=numOne/i
arrayDivisorj[1]=numTwo/i
numOne=arrayDivisorj[0]
numTwo=arrayDivisorj[1]
}
}
if(arrayDivisorj[0]==0){
arrayDivisorj[0]=One
arrayDivisorj[1]=Two
return arrayDivisorj
}
}
return arrayDivisorj
}
//两个数相加。用函数。最小公倍数。
func getF(oneSignal :String,
oneA :Int64,oneB :Int64,twoSignal :String,twoA :Int64,twoB :Int64):Array<String>{
var l=lcm(oneB,twoB)
println("l = ${l}")
var a=Int64.parse(oneSignal+"1")*oneA*(l/oneB)+Int64.parse(twoSignal+"1")*twoA*(l/twoB)
return [a.toString(),l.toString()]
}
// 两个分数相加。
func getFractionAddition(oneSignal :String,
oneA :Int64,oneB :Int64,twoSignal :String,twoA :Int64,twoB :Int64):Array<String>{
var a:Int64 = 0
var b:Int64 = 0
if(oneB!=twoB){
if(oneB>twoB&&oneB%twoB == 0){
a=Int64.parse(oneSignal+oneA.toString())+
Int64.parse(twoSignal+(twoA*(oneB/twoB)).toString())
b=oneB
}else if(twoB%oneB == 0){
a=Int64.parse(oneSignal+(oneA*(twoB/oneB)).toString())+
Int64.parse(twoSignal+(twoA.toString()))
b=twoB
}else{
a=Int64.parse(oneSignal+(oneA*twoB).toString())+
Int64.parse(twoSignal+(twoA*oneB).toString())
b=oneB*twoB
}
}
if(oneB==twoB){
if(oneSignal=="-"&&twoSignal ==""){
a=twoA -oneA
b=oneB
}else if(oneSignal=="-"&&twoSignal == "-"){
a=-oneA-twoA
b=oneB
}else if(oneSignal==""&&twoSignal=="-"){
a=oneA-twoA
b=oneB
}else if(oneSignal ==""&&twoSignal == ""){
a=oneA+twoA
b=oneB
}
}
return [a.toString(),b.toString()]
}
//求多个分数相加的和。
func getMultipleScores(multiple :ArrayList<Array<String>>):Array<String>{
var temp :Array<String>=[]
// =["","",""]
for(i in multiple){
if(temp.isEmpty()){
temp=i
}else{
var a=getL(Int64.parse(temp[1]),Int64.parse(temp[2]))
var b=getL(Int64.parse(i[1]),Int64.parse(i[2]))
var t=getF(temp[0],a[0],a[1],i[0],b[0],b[1])
// var t=getFractionAddition(temp[0],a[0],a[1],i[0],b[0],b[1])
var arrayTemp :Array<String>=t[0].split("-",118)
if(arrayTemp.size==2){
temp=["-",arrayTemp[1],t[1]]
}else{
temp=["",t[0],t[1]]
}
}
}
return temp
}
func getPrintlnTo(array: Array<String>){
if(!array.isEmpty()&&array[1].size!=0){
var a=getL(Int64.parse(array[1]),Int64.parse(array[2]))
if(a[0]>a[1]){
var yushu=a[0]%a[1]
var big:Int64=a[0]/a[1]
var b=getL(yushu,a[1])
if(b[0]!=0){
println("${array[0]}${big} ${b[0]}/${b[1]}")
}else{
println("${array[0]}${big}")
}
}else{
if(a[0]!=0){
println("${array[0]}${a[0]}/${a[1]}")
}else{
println("0")
}
}
}
}
main(){
// getL(8,32)
//getL(654,328764)
var temp :Int64=Int64.parse(getInputNumber())
if(temp<=100){
// getPrintlnTo(getMultipleScores(getStartAndEndNumber(getSplit(getInputNumber()))))
getPrintlnTo(getMultipleScores(getStartAndEndNumber(getSplit(getInputNumber()))))
}
return 0
}
仓颉PAT程序设计 L1-009 N个数求和(cangjie)
于 2024-08-03 22:49:30 首次发布