题解 | #DNA序列#
DNA序列
https://www.nowcoder.com/practice/e8480ed7501640709354db1cc4ffd42a
//以字串长度childL为窗口扫描整个基因序列
//将子列在基因序列中的起始位置存在变量maxIndex里,该子列的GC-ratio存在tempRatio里
//如果计算出的GC-ratio大于maxRatio
//则更新maxIndex
package main
import "fmt"
func main() {
var dna string
var childL int
fmt.Scan(&dna)
fmt.Scan(&childL)
var maxRatio float32 = 0.0
var maxIndex int
for i:=0; i<len(dna)-childL; i++ {
//var tempRatio float32
counter := 0
for j:=0; j<childL; j++ {
if dna[i+j:i+j+1]=="G" || dna[i+j:i+j+1]=="C" {
counter++
}
}
tempRatio := float32(counter) / float32(childL)
if tempRatio > maxRatio {
maxRatio = tempRatio
maxIndex = i
}
}
fmt.Println(dna[maxIndex:maxIndex+childL])
}
查看3道真题和解析
