Q2:
Given a sorted character array and a character, return the smallest character that is strictly larger than the search character. If no such character exists, return the smallest character in the array. For example:
Input: ['c', 'f', 'j', 'p', 'v'], 'a'
Output: 'c'
Input: ['c', 'f', 'j', 'p', 'v'], 'c'
Output: 'f'
Input: ['c', 'f', 'j', 'p', 'v'], 'z'
Output: 'c'
Input: ['c', 'c', 'k'], 'f'
Output: 'k'
有創建一個character_series.txt的文件來方便輸入sorted的character series。
`​​--------Iterative Version Start--------`
`​​show c`
`​​show f`
`​​show j`
`​​show p`
`​​show v`
`​​<1>input a character <2>Exit : 1`
`​​input a character: c`
`​​result = [ f ]`
`​​execution time of Flatten_Iterative() 0.0000005420 sec`
`​​<1>input a character <2>Exit : 1`
`​​input a character: a`
`​​result = [ c ]`
`​​execution time of Flatten_Iterative() 0.0000003700 sec`
`​​<1>input a character <2>Exit : 1`
`​​input a character: z`
`​​result = [ c ]`
`​​execution time of Flatten_Iterative() 0.0000004360 sec`
`​​<1>input a character <2>Exit : `
`​​2`
`​​--------Iterative Version End----------`
Iterative version 的想法:
• 目標: return比target還要大的值，如果target本身是最大的那就return最小的值
• 分析:
`​​int mystrlen(char str[])`
`​​{`
`​​    int count = 0;`
`​​    while(str[count]!='\0') {`
`​​        count++;`
`​​    }`
`​​    return(count);`
`​​}`
`​​ `
`​​char Smallest_Character_Iterative(char str[], char c)`
`​​{`
`​​    int i=0;`
`​​    if(c >= str[mystrlen(str)-1] || c < str[0]) {  //c 與 str中最大以及最小的元素相比`
`​​            return(str[0]);  //大於等於代表沒有值可以比c 還大，return 最小值，小於也同樣是如此`