Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon. Entire thread

Solve using your language of choice

Name: Anonymous 2011-04-13 9:05

http://i56.tinypic.com/9qlh52.png

my solution in Java

import java.io.*;
import java.util.*;

public class ProblemF {
   
    public static void main(String[] args) throws Exception {
        BufferedReader fileIn = new BufferedReader(new FileReader("f.in"));
       
        String line = null;
        while ((line = fileIn.readLine()) != null) {
            int number = Integer.parseInt(line);
            if (number <= 0 || number >= 100000000) {
                break;
            }
            System.out.println(number +" : "+ getOrderedPairs(number));
        }
    }
   
    public static String getOrderedPairs(int number) {
        StringBuffer buff = new StringBuffer();
       
        int pairCount = 0;
        for (int i = 1; i <= number; i++) {
            for (int j = i; j <= number; j++) {
                int sum = getSummation(i, j);
                if (sum == number) {
                    pairCount++;
                    buff.append(" ("+ i +","+ j +")");
                    break;
                } else if (sum > number) {
                    break;
                }
            }
        }
       
        buff.insert(0, pairCount +" :");
       
        return buff.toString();
    }
   
    public static int getSummation(int tempA, int tempB) {
        int a = tempA - 1;
        int b = tempB;
        int sumB = ((b * (b + 1)) / 2);
        int sumA = ((a * (a + 1)) / 2);
        return sumB - sumA;
    }
   
}

Name: Anonymous 2011-04-13 10:37


(struct output (n p c)
  #:property prop:custom-write
  (λ (o p _)
    (fprintf p "~a : ~a : " (output-n o) (output-p o))
    (for-each (λ (x) (display x p) (display #\space p)) (output-c o))))

(define (solve n)
  (let ((l (for/list ((a (in-range 0 (add1 n)))
                      #:when #t
                      (b (in-range 0 (add1 n)))
                      #:when (= (Σ values a b) n)
                      (k #(#f)))
             (cons a b))))
    (output n (length l) l)))

(solve 9)
: 9 : 3 : (2 . 4) (4 . 5) (9 . 9)

Newer Posts
Don't change these.
Name: Email:
Entire Thread Thread List