This is the 27th day of my participation in the August More Text Challenge

N number sum L1-009 (20 points) Go language | Golang

So this is a very simple thing to do, to sum N numbers. The trouble is, these numbers are given as rational numerators/denominators, and the sum you output must also be rational.

Input format:

The first line of input gives a positive integer N (≤100). The following line is in the format A1 / B1 a2/ B2… Give N rational numbers. They make sure that everything in the numerator and denominator is in the range of long integers. In addition, the sign for negative numbers must appear before the numerator.

Output format:

Print the simplest form of the sum of the numbers above — write the result as the integer part fraction part, where the fraction part is written as the numerator/denominator, requiring that the numerator is less than the denominator and that they have no common factors. If the integer part of the result is 0, only the fractional part is printed.

Input Example 1:

5, 2/5, 4/15, 1/30, minus 2/60, 8/3Copy the code

No blank line at the end

Example 1:

3 a thirdCopy the code

No blank line at the end

Input Example 2:

2 four thirds two-thirdsCopy the code

No blank line at the end

Example 2:

2
Copy the code

No blank line at the end

Input Example 3:

3 a third - 1/6-1/8Copy the code

No blank line at the end

Example 3:

7/24
Copy the code

No blank line at the end

Ideas:

  1. Let’s see if it’s an improper fraction when we add it up.
  2. If yes, print the numerator/denominator, and the numerator % denominator, and the denominator.
  3. If yes, an integer is directly output; if no, the numerator/denominator is directly output. Notice that you discard the 0!

But he only got 15 points. I’m numb. The third and fifth test points failed, we will debug them later. The big boys can take a look at it.

The code is as follows:

package main

import (
	"fmt"
	"strconv"
	"strings"
)

func main(a) {
	var num int
	var str1 string
	_, _ = fmt.Scan(&str1)
	tmp := strings.Split(str1,"/")  // The initial first fraction
	a,_:=strconv.Atoi(tmp[0])
	b,_:=strconv.Atoi(tmp[1])
	t := div(a,b)  // The greatest common divisor
	if a>0 {
		a /= t
		b /= t
	}
	_,_=fmt.Scan(&num)
	for i:=1; i<num; i++{var str string
		_, _ = fmt.Scan(&str)
		tmpList := strings.Split(str,"/") // The first score is computed with the second score
		c,_:=strconv.Atoi(tmpList[0])
		d,_:=strconv.Atoi(tmpList[1])
		lcp := b*d/div(b,d)			// Use the greatest common divisor to find the least common multiple of both denominators
		a = a * lcp/b + c*lcp/d		// The sum of the numerator
		b = lcp
		t1 := div(a,b)
		ift1 ! =0 { 				// Go back to the form after the least common multiple is enlarged
			a = a / t1				// The new input c and d above participate in the calculation, the continuous iteration
			b = b / t1
		}
	}
	// The following is the result of the iteration
	if a>0 && a/b==0 {   			// If a is greater than 0, the numerator is greater than 0.
		fmt.Printf("%d/%d",a%b,b)
	}else if a%b==0{				// If it happens to be an integer, print an integer
		fmt.Printf("%d\n",a/b)
	}else{
		fmt.Printf("%d %d/%d",a/b,a%b,b)
	}
}


func div(a,b int)int{ 		// The greatest common divisor
	if a<b {
		a,b = b,a
	}
	temp := a%b  			// Make sure that the big ones are mod to the small ones
	fortemp! =0 {  			// Ensure that the temp found is greater than zero and minimum
		a=b
		b=temp   // Keep doing residuals until they are minimal
		temp=a%b
	}
	return b
}
Copy the code

L1-010 compare size (10) the Go | Golang

In this case, you need to output any three integers from the smallest to the largest.

Input format:

The input gives three integers in a line separated by Spaces.

Output format:

Output three integers in a row from smallest to largest, connected by a hyphen (->).

Example Input:

4 2 8
Copy the code

No blank line at the end

Example output:

4 - > 2 - > 8Copy the code

No blank line at the end

Ideas:

Basic decision statements

If a is greater than B, then switch the values of a and B so that the smaller number comes first

The code is as follows:

package main

import "fmt"

func main(a) {
	var a,b,c int
	_,_=fmt.Scan(&a,&b,&c)
	if a > b { // If a is greater than B
		a,b=b,a // Change the values of a and b
	}			// Make sure the small number is in front, and the following is similar
	if a > c {
		a,c=c,a
	}
	if b > c {
		b,c=c,b
	}
	fmt.Printf("%d->%d->%d",a,b,c)
}
Copy the code