1.用代码实现将一个字符串转成整型。例如 String a = "25869" 2.在一个字符串中寻找子字符串,如果该字符串存在这个子字符串,则输出这个子字符串的起始位置,否则输出-1以上两题均不可以使用库函数。

解决方案 »

  1.   

    第一题:不用现有函数库public static int parseInt(String s, int radix)
                    throws NumberFormatException
        {
            /*
             * WARNING: This method may be invoked early during VM initialization
             * before IntegerCache is initialized. Care must be taken to not use
             * the valueOf method.
             */        if (s == null) {
                throw new NumberFormatException("null");
            }        if (radix < Character.MIN_RADIX) {
                throw new NumberFormatException("radix " + radix +
                                                " less than Character.MIN_RADIX");
            }        if (radix > Character.MAX_RADIX) {
                throw new NumberFormatException("radix " + radix +
                                                " greater than Character.MAX_RADIX");
            }        int result = 0;
            boolean negative = false;
            int i = 0, len = s.length();
            int limit = -Integer.MAX_VALUE;
            int multmin;
            int digit;        if (len > 0) {
                char firstChar = s.charAt(0);
                if (firstChar < '0') { // Possible leading "+" or "-"
                    if (firstChar == '-') {
                        negative = true;
                        limit = Integer.MIN_VALUE;
                    } else if (firstChar != '+')
                        throw NumberFormatException.forInputString(s);                if (len == 1) // Cannot have lone "+" or "-"
                        throw NumberFormatException.forInputString(s);
                    i++;
                }
                multmin = limit / radix;
                while (i < len) {
                    // Accumulating negatively avoids surprises near MAX_VALUE
                    digit = Character.digit(s.charAt(i++),radix);
                    if (digit < 0) {
                        throw NumberFormatException.forInputString(s);
                    }
                    if (result < multmin) {
                        throw NumberFormatException.forInputString(s);
                    }
                    result *= radix;
                    if (result < limit + digit) {
                        throw NumberFormatException.forInputString(s);
                    }
                    result -= digit;
                }
            } else {
                throw NumberFormatException.forInputString(s);
            }
            return negative ? result : -result;
        }
      

  2.   

    第二题static int indexOf(char[] source, int sourceOffset, int sourceCount,
                           char[] target, int targetOffset, int targetCount,
                           int fromIndex) {
            if (fromIndex >= sourceCount) {
                return (targetCount == 0 ? sourceCount : -1);
            }
            if (fromIndex < 0) {
                fromIndex = 0;
            }
            if (targetCount == 0) {
                return fromIndex;
            }        char first  = target[targetOffset];
            int max = sourceOffset + (sourceCount - targetCount);        for (int i = sourceOffset + fromIndex; i <= max; i++) {
                /* Look for first character. */
                if (source[i] != first) {
                    while (++i <= max && source[i] != first);
                }            /* Found first character, now look at the rest of v2 */
                if (i <= max) {
                    int j = i + 1;
                    int end = j + targetCount - 1;
                    for (int k = targetOffset + 1; j < end && source[j] ==
                             target[k]; j++, k++);                if (j == end) {
                        /* Found whole string. */
                        return i - sourceOffset;
                    }
                }
            }
            return -1;
        }
      

  3.   

    转为chr 数组,很简单的第一题:
    C++;
    CString str="123456";
    int iLength=str.GetLength() ;
    char *chr=new char[iLength];
    chr=str.GetBuffer();
    int RatValue=0;
    int iTmp;
    int i,j;
    for (i=0;i<iLength;i++)
    {
    iTmp=1;//计算10的次方,因为不准用函数库
    for (j=0;j<iLength-i-1;j++)
    {
    iTmp=iTmp*10;
    }
    RatValue=RatValue + ((chr[i]-48)* iTmp);
    }
    delete []chr;
            return RatValue;
    第二题也差不多,五分钟内也可以搞定.
      

  4.   

    不好意思,Android 是用JAVA的,不太懂.爱莫能助...
    第一题,如果JAVA的代码多于十行代码,说明,C还是挺优的.
    C关键代码以上楼,也就是五句代码搞定啦.