1000位数的字符串,找到没有数组的最大5位数字

时间:2015-11-19 05:36:53

标签: java string numbers digits charat

我知道我的代码可以更简单,更有效...我的代码应该抓住最大的5位数字。它工作,除了它只抓取3位数,我需要修改什么来改变它?

public class thousandDigits {
    public static void main(String[] args) {
        int greatest = 0;
        String num = ("73167176531330624919225119674426574742355349194934"
                + "96983520312774506326239578318016984801869478851843"
                + "85861560789112949495459501737958331952853208805511"
                + "12540698747158523863050715693290963295227443043557"
                + "66896648950445244523161731856403098711121722383113"
                + "62229893423380308135336276614282806444486645238749"
                + "30358907296290491560440772390713810515859307960866"
                + "70172427121883998797908792274921901699720888093776"
                + "65727333001053367881220235421809751254540594752243"
                + "52584907711670556013604839586446706324415722155397"
                + "53697817977846174064955149290862569321978468622482"
                + "83972241375657056057490261407972968652414535100474"
                + "82166370484403199890008895243450658541227588666881"
                + "16427171479924442928230863465674813919123162824586"
                + "17866458359124566529476545682848912883142607690042"
                + "24219022671055626321111109370544217506941658960408"
                + "07198403850962455444362981230987879927244284909188"
                + "84580156166097919133875499200524063689912560717606"
                + "05886116467109405077541002256983155200055935729725"
                + "71636269561882670428252483600823257530420752963450");

        for (int n = 0; n < num.length() - 5; n++) {
            greatest = ((num.charAt(n)) + (num.charAt(n+1)) + (num.charAt(n+2)) + (num.charAt(n+3))
                    + (num.charAt(n+4)));
            if (greatest > n) {
                n = greatest;
            }
        }
        System.out.print(greatest);
    }
}

输出:

357

4 个答案:

答案 0 :(得分:3)

我认为您希望使用String.substring(int, int)来迭代所有可能的5个字符子字符串,然后您可以使用Math.max(int, int)更新greatest。像

这样的东西
int greatest = Integer.MIN_VALUE;
for (int i = 0; i < num.length() - 4; i++) {
    // int value = Integer.parseInt(num.substring(i, i + 5));
    int value = Integer.parseInt(String.valueOf(num.charAt(i))
                + num.charAt(1 + i) + num.charAt(2 + i) + num.charAt(3 + i)
                + num.charAt(4 + i));
    greatest = Math.max(greatest, value);
}
System.out.println(greatest);

我得到99890

答案 1 :(得分:2)

我认为你发现它并没有抓住三位数,而是你抽出的六个字符的总和是一个3位数字。

如果你在最大的五位数之后,你需要提取五位数(而不是六位数),而为它们分配一个权重。因此,第一个数字必须乘以10,000,第二个数字必须乘以1,000,依此类推。

但还有更多:您在字符串中的索引处获得了字符。这不是您想要的,因为它与该字符的数值不同。为此你需要:

num.charAt(n) - '0'

这些更改应该允许您按原样更正算法。

更有效的方法是提取5位子字符串并将其转换为整数。第一个是:

Integer.parseInt(num.subString(0, 5));

你可以迭代让每个人找到最好的。

答案 2 :(得分:2)

虽然@ElliottFrisch和@dave提供了更优雅的答案,但我尝试从原始版本进行修改,这是我的代码(我已对其进行了测试):

class ProfileView: UIViewController,  UIImagePickerControllerDelegate {
 func userchange(sender: AnyObject){
    if UIImagePickerController.isSourceTypeAvailable(UIImagePickerControllerSourceType.SavedPhotosAlbum){
       // imagePicker.delegate = self
        imagePicker.allowsEditing = false
        imagePicker.sourceType = .PhotoLibrary
        presentViewController(imagePicker, animated: true, completion: nil)

    }
}

func imagePickerController(picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : AnyObject]) {
    if let pickedImage = info[UIImagePickerControllerOriginalImage] as? UIImage {

        let user = PFUser.currentUser()
        let image = info[UIImagePickerControllerOriginalImage] as! UIImage
        let imageData = UIImageJPEGRepresentation(image, 0.05)
        let imageFile = PFFile(name:"image.jpg", data:imageData!)
        user!["ProPic"] = imageFile;
        imageFile.save()
        //NOTHING IS BEING PRINTED
        print("called")
        user!.saveInBackgroundWithBlock(nil)


        self.dismissViewControllerAnimated(true, completion: nil)

    }
   }
 }

答案 3 :(得分:2)

我认为您正在尝试添加5个连续字符以获得总和,并存储最高总和的起始索引。

但您应该使用Character.getNumricValue(char)将(num.charAt(n))转换为数值然后添加。

greatest  = Character.getNumericValue((num.charAt(n)) + Character.getNumericValue((num.charAt(n+1)) + Character.getNumericValue((num.charAt(n+2)) + 
Character.getNumericValue((num.charAt(n+3)) + 
Character.getNumericValue((num.charAt(n+4));

您需要一个可存储的旧值来比较和索引

if(greatest > oldGreatest) {
    index = n;
} 

然后最后使用索引输出侧循环打印:

System.out.print((num.charAt(index)) + (num.charAt(index+1) + (num.charAt(index +2)) + (num.charAt(index +3)) + (num.charAt(index +)));