简单有趣#128:双不小【难度:3级】:
答案1:
namespace myjinxin
{
using System;
using System. Linq;
using System. Collections. Generic;
using System. Numerics;
public class Kata
{
public string DoublyNotLess ( string n) {
var s= n. ToCharArray ( ) . Select ( x= > int . Parse ( "" + x) ) ;
var r= s. Select ( x= > x) . ToArray ( ) ;
int i= 0 ;
Array. Reverse ( r) ;
var ss= BigInteger. Parse ( string . Join ( "" , s) ) ;
while ( BigInteger. Parse ( string . Join ( "" , r) ) < ss) {
if ( r[ i] < 9 ) r[ i] ++ ;
else i++ ;
}
Array. Reverse ( r) ;
return string . Join ( "" , r) ;
}
}
}
答案2:
namespace myjinxin
{
using System;
public class Kata
{
public string DoublyNotLess ( string n) {
var arrayOfGreater = new bool [ n. Length / 2 ] ;
for ( var leftIndexFromMiddle = n. Length / 2 - 1 ; leftIndexFromMiddle >= 0 ; leftIndexFromMiddle-- )
{
var rightIndex = n. Length - leftIndexFromMiddle - 1 ;
if ( n[ leftIndexFromMiddle] == n[ rightIndex] )
arrayOfGreater[ leftIndexFromMiddle] = arrayOfGreater. Length - 1 == leftIndexFromMiddle ||
arrayOfGreater[ leftIndexFromMiddle + 1 ] ;
else
arrayOfGreater[ leftIndexFromMiddle] = n[ leftIndexFromMiddle] < n[ rightIndex] ;
}
for ( var leftIndex = 0 ; leftIndex < n. Length / 2 ; leftIndex++ )
{
var leftCharacter = n[ leftIndex] ;
var rightIndex = n. Length - leftIndex - 1 ;
var rightCharacter = n[ rightIndex] ;
if ( leftCharacter < rightCharacter)
return n;
if ( arrayOfGreater. Length - 1 == leftIndex || arrayOfGreater[ leftIndex + 1 ] )
return Replace ( n, rightIndex, leftCharacter) ;
if ( leftCharacter != '9' )
return Replace ( n, rightIndex, ( char ) ( leftCharacter + 1 ) ) ;
n = Replace ( n, rightIndex, '9' ) ;
}
return n;
}
private static string Replace ( string s, int index, char c) = > s. Remove ( index, 1 ) . Insert ( index, c. ToString ( ) ) ;
}
}
答案3:
using System. Linq;
namespace myjinxin {
public class Kata {
public string DoublyNotLess ( string n) {
if ( n. Length < 2 || n. First ( ) < n. Last ( ) ) return n;
var middle = n. Substring ( 1 , n. Length- 2 ) ;
var middleReversed = string . Concat ( middle. Reverse ( ) ) ;
if ( middle. CompareTo ( middleReversed) <= 0 ) return string . Concat ( n. First ( ) , middle, n. First ( ) ) ;
if ( n. First ( ) < '9' ) return string . Concat ( n. First ( ) , middle, ( char ) ( n. First ( ) + 1 ) ) ;
return $"9{DoublyNotLess(middle)}9" ;
}
}
}
答案4:
namespace myjinxin
{
using System;
using System. Globalization;
using System. Linq;
public class Kata
{
public string DoublyNotLess ( string n)
{
var digitList = n. Select ( letter = > CharUnicodeInfo. GetDecimalDigitValue ( letter) ) . ToList ( ) ;
var indexOfFirstNon9 = - 1 ;
for ( var index = 0 ; index < n. Length / 2 ; index++ )
{
if ( digitList[ index] == 9 & amp; & amp; indexOfFirstNon9 == - 1 )
{
digitList[ n. Length - index - 1 ] = 9 ;
continue ;
}
var isFirstNon9 = indexOfFirstNon9 == - 1 ;
if ( isFirstNon9)
{
indexOfFirstNon9 = index;
}
int compareResult = digitList[ n. Length - index - 1 ] . CompareTo ( digitList[ index] ) ;
var extraIncrease = ( compareResult == 1 & amp; & amp; ! isFirstNon9) || ( compareResult == - 1 & amp; & amp; isFirstNon9) ? 0 : 1 ;
if ( compareResult == - 1 || ( compareResult == 1 & amp; & amp; ! isFirstNon9) )
{
digitList[ n. Length - indexOfFirstNon9 - 1 ] = digitList[ indexOfFirstNon9] + extraIncrease;
}
if ( compareResult == 1 || ( compareResult == - 1 & amp; & amp; ! isFirstNon9) )
{
break ;
}
}
return string . Concat ( digitList) ;
}
}
}
答案5:
namespace myjinxin
{
using System;
using System. Numerics;
public class Kata
{
public string DoublyNotLess ( string n)
{
var arr = n. ToCharArray ( ) ;
for ( int i = 0 ; i < arr. Length / 2 ; i++ )
{
if ( arr[ i] < arr[ arr. Length - 1 - i] ) break ;
if ( arr[ i] >= arr[ arr. Length - 1 - i] )
{
if ( arr[ i] == '9' )
{
arr[ arr. Length - 1 - i] = '9' ;
}
else
{
var flag = false ;
arr[ arr. Length - 1 - i] = arr[ i] ;
for ( int j = i; j < arr. Length / 2 ; j++ )
{
if ( arr[ j] < arr[ arr. Length - 1 - j] )
{
flag = true ;
break ;
}
if ( arr[ j] > arr[ arr. Length - 1 - j] ) break ;
}
if ( ! flag)
{
arr[ arr. Length - 1 - i] = ( char ) ( arr[ i] + 1 ) ;
break ;
}
}
}
}
return new string ( arr) ;
}
}
}
答案6:
namespace myjinxin {
using System;
using System. Collections. Generic;
using System. Linq;
public class Kata {
private Dictionary< char , char > replaceDict;
public Kata ( ) {
replaceDict = new Dictionary < char , char > ( ) ;
replaceDict. Add ( '0' , '1' ) ;
replaceDict. Add ( '1' , '2' ) ;
replaceDict. Add ( '2' , '3' ) ;
replaceDict. Add ( '3' , '4' ) ;
replaceDict. Add ( '4' , '5' ) ;
replaceDict. Add ( '5' , '6' ) ;
replaceDict. Add ( '6' , '7' ) ;
replaceDict. Add ( '7' , '8' ) ;
replaceDict. Add ( '8' , '9' ) ;
}
public string DoublyNotLess ( string n) {
var nAr = n. ToCharArray ( ) ;
var revN = n. Reverse ( ) . ToArray ( ) ;
var index = n. Length- 1 ;
while ( ! BiggerOrEqualThan ( revN, nAr) ) {
while ( nAr[ index] == '9' ) index-- ;
nAr[ index] = replaceDict[ nAr[ index] ] ;
revN = nAr. Reverse ( ) . ToArray ( ) ;
}
return new string ( nAr) ;
}
private bool BiggerOrEqualThan ( char [ ] bigger, char [ ] than) {
if ( bigger. Length != than. Length) return bigger. Length >= than. Length;
Console. WriteLine ( $"bigger: {string.Concat(bigger)}, than: {string.Concat(than)}" ) ;
Console. WriteLine ( $"bigger.Length: {bigger.Length}, than.Length: {than.Length}" ) ;
for ( var i = 0 ; i < bigger. Length; i++ ) {
Console. WriteLine ( i) ;
if ( bigger[ i] != than[ i] ) {
return char . GetNumericValue ( bigger[ i] ) >= char . GetNumericValue ( than[ i] ) ;
}
}
return true ;
}
}
}
答案7:
namespace myjinxin
{
using System;
public class Kata
{
public string DoublyNotLess ( string n) {
char [ ] number = n. ToCharArray ( ) ;
for ( int i = 0 ; i < n. Length / 2 ; i++ )
{
char head = n[ i] ;
char tail = n[ n. Length - i - 1 ] ;
if ( head >= tail)
{
if ( head < '9' )
{
number[ n. Length - i - 1 ] = head;
if ( ! CheckDoublyNotLess ( string . Concat ( number) ) )
{
number[ n. Length - i - 1 ] = ( Char) ( ( int ) ( head) + 1 ) ;
}
}
else
{
number[ n. Length - i - 1 ] = n[ i] ;
}
}
if ( CheckDoublyNotLess ( string . Concat ( number) ) )
break ;
}
Console. WriteLine ( string . Concat ( number) ) ;
return string . Concat ( number) ;
}
private bool CheckDoublyNotLess ( string n)
{
for ( int i = 0 ; i < n. Length / 2 ; i++ )
{
char head = n[ i] ;
char tail = n[ n. Length - i - 1 ] ;
if ( head < tail)
{
return true ;
}
if ( head > tail)
{
return false ;
}
}
return true ;
}
}
}
答案8:
namespace myjinxin
{
using System;
using System. Linq;
public class Kata
{
public string DoublyNotLess ( string n)
{
char [ ] number = n. ToCharArray ( ) ;
for ( int i = 0 ; i< n. Length/ 2 ; i++ )
{
char head = n[ i] ;
char tail = n[ n. Length- i- 1 ] ;
if ( head>= tail)
{
if ( head< '9' )
{
number[ n. Length- i- 1 ] = head;
if ( ! CheckDoublyNotLess ( string . Concat ( number) ) )
{
number[ n. Length- i- 1 ] = ( Char) ( ( int ) ( head) + 1 ) ;
}
}
else
{
number[ n. Length- i- 1 ] = n[ i] ;
}
}
if ( CheckDoublyNotLess ( string . Concat ( number) ) )
break ;
}
Console. WriteLine ( string . Concat ( number) ) ;
return string . Concat ( number) ;
}
private bool CheckDoublyNotLess ( string n)
{
for ( int i = 0 ; i< n. Length/ 2 ; i++ )
{
char head = n[ i] ;
char tail = n[ n. Length- i- 1 ] ;
if ( head< tail)
{
return true ;
}
if ( head> tail)
{
return false ;
}
}
return true ;
}
}
}