发布C ++与alghorithm山

时间:2017-06-24 07:13:39

标签: c++

我有以下问题: 如果数字的数字按升序排列到数字的中间并且从该位置开始按降序排列,则数字具有山峰外观。示例:12321,345754是山景数字。对于从PC用户读取的自然数n,请检查该数字是否具有山峰外观,并显示相应的消息。 我做了这个,但我不知道问题出在哪里:

import Foundation

enum PostType: Codable {
    case count(number: Int)
    case comment(text: String)

    init(from decoder: Decoder) throws {
        self = try PostTypeCodableForm(from: decoder).enumForm()
    }

    func encode(to encoder: Encoder) throws {
        try PostTypeCodableForm(self).encode(to: encoder)
    }
}

struct PostTypeCodableForm: Codable {
    // All fields must be optional!
    var countNumber: Int?
    var commentText: String?

    init(_ enumForm: PostType) {
        switch enumForm {
        case .count(let number):
            countNumber = number
        case .comment(let text):
            commentText = text
        }
    }

    func enumForm() throws -> PostType {
        if let number = countNumber {
            guard commentText == nil else {
                throw DecodeError.moreThanOneEnumCase
            }
            return .count(number: number)
        }
        if let text = commentText {
            guard countNumber == nil else {
                throw DecodeError.moreThanOneEnumCase
            }
            return .comment(text: text)
        }
        throw DecodeError.noRecognizedContent
    }

    enum DecodeError: Error {
        case noRecognizedContent
        case moreThanOneEnumCase
    }
}

let test = PostType.count(number: 3)
let data = try JSONEncoder().encode(test)
let string = String(data: data, encoding: .utf8)!
print(string) // {"countNumber":3}
let result = try JSONDecoder().decode(PostType.self, from: data)
print(result) // count(3)

1 个答案:

答案 0 :(得分:0)

Hy,你在第17行犯了一些错误: 替换

for (i = 0; i <= up && ok != 1; i++)

 for(i=0;i<=middle && ok==1;i++)

并在下一个中:

for (i = k - up; i < 0 && ok == 1; i--) {
你写下这个:

for(i=k-middle;i>0 && ok==1;i--)
相关问题