2010年12月14日火曜日

TopCoder練習 SRM424 DIV2

250問題文
public class MagicSpell {
 public String fixTheSpell(String spell) {
  String magic = new String();
  for(int i = 0; i < spell.length(); i++) {
   if(spell.charAt(i) == 'A' || spell.charAt(i) == 'Z') {
    magic += spell.charAt(i);
   }
  }
  String magicSpell = new String();
  int count = 0;
  for(int i = 0; i < spell.length(); i++) {
   if(spell.charAt(i) == 'A' || spell.charAt(i) == 'Z') {
    magicSpell += magic.charAt(magic.length()-1-count);
    count++;
   } else {
    magicSpell += spell.charAt(i);
   }
  }
  return magicSpell;
 }
}
問題文が読めれば解ける。コードは汚い。

500

import java.util.ArrayList;

public class ProductOfDigits {
 public int smallestNumber(int N) {
  if(N < 10) return 1;
  
  ArrayList<Integer> list = new ArrayList<Integer>();
  for(int i = 9; i > 1; i--) {
   if(N % i == 0) {
    list.add(i);
   }
  }
  if(list.size() < 1) return -1;
  
  int index = 0;
  int a = 0;
  while(N > 1) {
   if(N % list.get(index) == 0) {
    a++;
    N /= list.get(index);
   } else {
    if(index < list.size() -1) {
     index++;
    } else {
     return -1;
    }
   }
  }
  return a;
 }
}
問題文の意味よく分からなくて1文字もかけなかった。

0 件のコメント:

コメントを投稿