1,不调用任何库函数,将一个字符串类型数据转换成一个整形数据,如:String ="123456"->int i=123456(语言不限,本人用的JAVA)?
2,怎样优化一个多线程?
在线求高手解决。

解决方案 »

  1.   

    不调用任何库函数 - -
    java.lang算么。。
      

  2.   

    public class Test777 { /**
     * @param args
     */
    public int toInt(String s) {
    int result = 0;// 返回值
    for (int i = 1; i <= s.length(); i++) {
    int a = Integer.parseInt("" + s.charAt(i - 1));// 将元素转化成int型
    // 计算a乘以10的次数
    for (int j = 0; j < s.length() - i; j++) {
    a = a * 10;
    }
    result = result + a;
    } return result;
    } public static void main(String[] args) {
    Test777 t = new Test777();
    System.out.println(t.toInt("1234"));
    }}我写的这个 应该算是吧。
    楼上的是类库里面的
      

  3.   

    感觉这种题不是应该用java 做吧。Integer.parseInt()肯定不可以用,否则直接就出来结果了。
      

  4.   

    String 本身就在库中,所以你的要求不可能满足
      

  5.   

    直接查看Integer.parseInt源代码不就over了么
      

  6.   

    public int parseInt(String s){
    int result=0;
    int max=s.length();
    int i=0;
    int digit = 0;
        while (i < max) {
    digit = Character.digit(s.charAt(i++),10);
    // System.out.println("digit="+digit);
    result *= 10;
    result += digit;
    }
        return result;
    }
      

  7.   

    怎样优化线程很简单:加cpu呗。呵呵
      

  8.   


    public class StrToInt {
    public int Change(String str) {
    if (str.length() <= 0) {
    return -1;
    }
    char ch = str.charAt(0);
    int tmp = 0;
    switch (ch) {
    case 48:
    tmp = 0;
    break;
    case 49:
    tmp = 1;
    break;
    case 50:
    tmp = 2;
    break;
    case 51:
    tmp = 3;
    break;
    case 52:
    tmp = 4;
    break;
    case 53:
    tmp = 5;
    break;
    case 54:
    tmp = 6;
    break;
    case 55:
    tmp = 7;
    break;
    case 56:
    tmp = 8;
    break;
    case 57:
    tmp = 9;
    break;
    default:
    return 0;
    }
    if (str.length() == 1) {
    return tmp;
    }
    String tmpStr = str.substring(1);
    if (str.charAt(0) == 45)
    return -(tmp * (int) Math.pow(10, tmpStr.length()) + Change(tmpStr));
    else
    return tmp * (int) Math.pow(10, tmpStr.length()) + Change(tmpStr);
    }
    public static void main(String[] args) {
    // TODO Auto-generated method stub
    StrToInt s = new StrToInt();
    System.out.println(s.Change("120345"));
    }
    }
      

  9.   


    public class Test777 
    {
       public int toInt(String s) 
       {
          int result = 0;
          for (int i = 0; i < s.length(); i++) 
          {
            int a = s.charAt(i)-'0';// 将元素转化成int型
            result*=10;
            result = result + a;
          }      return result;
       }   public static void main(String[] args) 
       {
          Test777 t = new Test777();
          System.out.println(t.toInt("1234"));
       }}不确定charAt  和length()  在这能不能用  优化一个多线程, 用偏向锁是不错的
      

  10.   

    不能使用任何库函数,所以 java.lang.String 里的方法都不能使用。
    这个还是用C写吧。
      

  11.   


    直接  Integer.parseInt(s)  得了   拖裤子放屁 舒服呗?
      

  12.   

    这么写也就行了
    package exp1;public class Test { public int toInt(String s) {
    int a = Integer.parseInt(s);
    return a;
    } public static void main(String[] args) {
    Test t = new Test();
    System.out.println(t.toInt("1234"));
    }
    }
      

  13.   

    参考c atoi的代码,我用了char,与int两个数组,转换我不太明白,望高手解答
    #include <ctype.h>
    #include <stdio.h>int main()
    {
    int i;
    int a[100]={0};//char *s;
    //gets(s);
    char *s="12312";
    atoi1(s,a);
    for(i=0;i<strlen(s);i++)
        {
            printf("%d",a[i]);
        }
    system("pause");
    return 0;
    }
    void atoi1 (char *s,int a[])
    {
    int i=0;
    while(s[i]!='\0')
        {
            a[i]=s[i]-'0';
            i++;
        }
    }
      

  14.   

    成功解决了这个问题
    #include <ctype.h>
    #include <stdio.h>int main()
    {
    int i;
    int a[100]={0};
    char *s="12312";
    int sum=0;
    int ren=1;
    atoi1(s,a);
    for(i=0;i<strlen(s);i++)
        {
            printf("%d\n",a[i]);
        }
    for(i=strlen(s)-1;i>=0;i--)
        {
            sum=sum+(a[i])*ren;
            ren=ren*10;
        }
    printf("%d\n",sum);
    system("pause");
    return 0;
    }
    void atoi1 (char *s,int a[])
    {
    int i=0;
    while(s[i]!='\0')
        {
            a[i]=s[i]-'0';
            i++;
        }
    }
      

  15.   


    package com.test;public class TestStr { /**
     * @param args
     */
    public static void main(String[] args) {
    String str = "123456";
    System.out.println(convertStr2int(str));
    } private static int convertStr2int(String str) {
    int value = 0;
    for (int i = 0; i < str.length(); i++) {
    value += mapStr(str.charAt(i)) * Math.pow(10, str.length() - 1 - i);
    }
    return value;
    } private static int mapStr(char ch) {
    switch (ch) {
    case '0':
    return '0';
    case '1':
    return 1;
    case '2':
    return 2;
    case '3':
    return 3;
    case '4':
    return 4;
    case '5':
    return 5;
    case '6':
    return 6;
    case '7':
    return 7;
    case '8':
    return 8;
    case '9':
    return 9;
    }
    return -1;
    }
    }
      

  16.   


    public static int parseInt(String s, int radix)
    throws NumberFormatException
        {
            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, max = s.length();
    int limit;
    int multmin;
    int digit; if (max > 0) {
        if (s.charAt(0) == '-') {
    negative = true;
    limit = Integer.MIN_VALUE;
    i++;
        } else {
    limit = -Integer.MAX_VALUE;
        }
        multmin = limit / radix;
        if (i < max) {
    digit = Character.digit(s.charAt(i++),radix);
    if (digit < 0) {
        throw NumberFormatException.forInputString(s);
    } else {
        result = -digit;
    }
        }
        while (i < max) {
    // 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);
    }
    if (negative) {
        if (i > 1) {
    return result;
        } else { /* Only got "-" */
    throw NumberFormatException.forInputString(s);
        }
    } else {
        return -result;
    }
        }
     public static int parseInt(String s) throws NumberFormatException {
    return parseInt(s,10);
        }
      

  17.   

    这个在我的turbo c种void atoi1 (char *s,int a[])方法返回值我改成atoi1 (char *s,int a[])可以编译成功,输出结果是对的。万分感谢
      

  18.   

    public class Demo { /**
     * @param args
     */
    public static void main(String[] args) {
    // TODO Auto-generated method stub
    int value=2783273;
           new Demo().reverse(value);
    }
    void  reverse(int value){
    int a=0;
    for(int i=0;i<7;i++){

    a=a*10+value%10;
        value=value/10;
    }
      System.out.print(a);
    }
    }