2009年6月25日 星期四

ACM Q11462



#include <iostream>

using namespace std;

int main(){
int ints[100];
while(true){
int count =0;
int count2 =0;
for(int i=0; i< 100; i++){
ints[i]=0;
}
int items;
cin >> items;
if(items==0)
break;
for(int i=0; i< items; i++){
int tmp;
cin >> tmp;
if(ints[tmp-1]==0)
count++;
ints[tmp-1]++;
}

for(int i=0; i< 100; i++){
if(ints[i]>0)
count2++;
for(int j=0; j< ints[i]; j++){
if(count2==count && j==ints[i]-1)
cout<<(i+1)<<endl;
else
cout<<(i+1)<<" ";
}
}
}
return 0;
}

ACM Q11428



import java.util.Scanner;
import java.lang.Math.*;
public class Q11428 {
public static void main(String args[]) {
Scanner cin = new Scanner(System.in);
while(true){
double N = cin.nextDouble();
boolean flag = false;
int tmpx=99999, tmpy=99999;
if(N==0)
break;
double base = (double) 1/3;
for(int x = (int)Math.ceil(Math.pow(N,base)); x <= Math.sqrt(N) ; x++){
if (((int)(((Math.pow((Math.pow(x,3)-N),base)+1e-7)*100000)%100000)==0)&&(Math.pow((Math.pow(x,3)-N),base)!=0)){
flag = true;
if((int)(Math.pow((Math.pow(x,3)-N),base)+1e-7)< tmpy){
tmpx = x;
tmpy = (int)(Math.pow((Math.pow(x,3)-N),base)+1e-7);
}
}
}
if(flag==false){
System.out.println("No solution");
}else{
System.out.println(tmpx+" "+tmpy);
}
}
}
}

2009年6月24日 星期三

ACM Q11369



import java.util.Scanner;
import java.util.Arrays;

public class Q11369 {
public static void main(String args[]) {
Scanner cin = new Scanner(System.in);
int loop = Integer.parseInt(cin.nextLine());
for(int i=0; i< loop ; i++){
int count =0;
int items = Integer.parseInt(cin.nextLine());
String str = cin.nextLine();
String[] tokens = str.split(" ");

int[] ints = new int [items] ;
for(int k = 0 ; k < items ; k++){
ints[k] = Integer.parseInt(tokens[k] ) ;
}

Arrays.sort(ints);
for(int k = items-1; k>=0; k--){
if((items-k-1)%3==2){
count += ints[k];
}
}
System.out.println(count);

}
}
}

ACM Q11461



import java.util.Scanner;
import java.lang.Math.*;
public class Q11461 {
public static void main(String args[]) {
Scanner cin = new Scanner(System.in);
while(cin.hasNextInt()){
int M = cin.nextInt();
int N = cin.nextInt();
if((M==0)&&(N==0))
break;
int L = (int) Math.ceil(Math.sqrt(Math.min(M,N)));
int U = (int) Math.floor(Math.sqrt(Math.max(M,N)));
System.out.println(U-L+1);
}
}
}

ACM Q11586



import java.util.Scanner;
public class Q11586 {
public static void main(String args[]) {
Scanner cin = new Scanner(System.in);
int loop = Integer.parseInt(cin.nextLine());
boolean flag;
for(int i=0; i< loop ; i++){
flag =true;
int FF=0, FM=0, RF=0, RM=0;
String str = cin.nextLine();
if(str.isEmpty()){
System.out.println("LOOP");
continue;
}
String[] tokens = str.split(" ");
if(tokens.length>1){
for(int j = 0; j< tokens.length; j++){
if(tokens[j].charAt(0)=='M'){
FM++;
}
else if(tokens[j].charAt(0)=='F'){
FF++;
}
if(tokens[j].charAt(1)=='F'){
RF++;
}
else if(tokens[j].charAt(1)=='M'){
RM++;
}
}
}else{
System.out.println("NO LOOP");
continue;
}

if((FM==RF) && (FF==RM)){
System.out.println("LOOP");
}else{
System.out.println("NO LOOP");
}
}
}
}

2009年6月11日 星期四

ACM Q11530



import java.util.Scanner;
public class Main {
public static void main(String args[]) {
Scanner cin = new Scanner(System.in);
int T = cin.nextInt();
cin.nextLine();
for(int i=0; i< T; i++){
int count =0;
String line = cin.nextLine();
char[] lineAry = line.toCharArray();
for(int j=0; j<lineAry.length; j++){
int tmp = lineAry[j];
if(tmp<112&& tmp>= 97){
count += ((int)lineAry[j]-1)%3+1;
}else if(lineAry[j]=='p'||lineAry[j]=='t'||lineAry[j]=='w'||lineAry[j]==' '){
count +=1;
}else if (lineAry[j]=='q'||lineAry[j]=='u'||lineAry[j]=='x'){
count +=2;
}else if (lineAry[j]=='r'||lineAry[j]=='v'||lineAry[j]=='y'){
count +=3;
}else if (lineAry[j]=='s'||lineAry[j]=='z'){
count +=4;
}
}
System.out.println("Case #"+(i+1)+": "+count);
}
}
}

ACM Q10079



import java.util.Scanner;
public class Q10079 {
public static void main(String args[]) {
Scanner cin = new Scanner(System.in);
while(cin.hasNextLong()){
long n = cin.nextLong();
if(n<0)
break;
System.out.println(n*(n+1)/2+1);
}
}
}

ACM Q686



import java.util.Scanner;
import java.util.HashSet;
import java.util.ArrayList;
import java.lang.Math.*;
public class Main {
public static void main(String args[]) {
Scanner cin = new Scanner(System.in);
ArrayList<Integer> numbers = new ArrayList<Integer>();
numbers.add(2);
numbers.add(3);
for(int i=3; i< 32768; i+=2){
boolean flag = false;
for(int j=0; j< numbers.size(); j++){
if(numbers.get(j)>Math.sqrt(i)){
break;
}else if((i%numbers.get(j))==0){
flag=false;
break;
}else{
flag=true;
}

}
if(flag==true)
numbers.add(i);
}
HashSet<Integer> numberSet = new HashSet<Integer>(numbers);
while(cin.hasNextInt()){
int n = cin.nextInt();
int count=0;
if(n==0)
break;
for(int i=0; numbers.get(i)<=(n/2); i++){
if(numberSet.contains(n-numbers.get(i))){
count ++;
}
}
System.out.println(count);
}
}
}

2009年6月6日 星期六

ACM Q482



import java.util.Scanner;
import java.util.Iterator;
import java.util.Collections;
import java.util.ArrayList;
import java.util.HashMap;

public class Q482 {
public static void main(String args[]) {
Scanner cin = new Scanner(System.in);
int set = Integer.parseInt(cin.nextLine());
for(int i=0; i<set; i++){
HashMap<Integer, String> numbers = new HashMap<Integer, String>();
String empty = cin.nextLine();
String index = cin.nextLine();
String[] indexToken = index.split(" ");
String content = cin.nextLine();
String[] contentToken = content.split(" ");
for(int j=0; j< indexToken.length; j++){
numbers.put(Integer.parseInt(indexToken[j]),contentToken[j]);
}
ArrayList<Integer> as = new ArrayList<Integer>(numbers.keySet());
Collections.sort(as);
Iterator<Integer> itr = as.iterator();
while(itr.hasNext()){
int s = itr.next();
System.out.println(numbers.get(s));
}
if(i!=set-1)
System.out.println();
}
}
}

2009年6月5日 星期五

ACM Q530



import java.util.Scanner;
public class Main {
public static void main(String args[]) {
Scanner cin = new Scanner(System.in);
while(cin.hasNextDouble() ){
double n = cin.nextDouble();
double m = cin.nextDouble();
double fac_n=1, fac_m=1;
double count;
if(n<1&&m==0&&m-n<=0){
break;
}
if(m==0){
System.out.println(1);
continue;
}
if(n-m < m){
count = n-m;
}else{
count =m;
}
for(double i=0; i<count; i++){
fac_n *= (n-i);
}
for(double j=1; j<=count; j++){
fac_m *= j;
}
System.out.println((int) (fac_n / fac_m));
}
}
}

2009年6月3日 星期三

ACM Q591



import java.util.Scanner;
public class Q591 {
public static void main(String args[]) {
int count =0;
Scanner cin = new Scanner(System.in);
while(cin.hasNextLine() ){
String inputLine = cin.nextLine();
int size = Integer.parseInt(inputLine);
if (size==0){
break;
}
count++;
System.out.println("Set #"+count);
String tokenLine = cin.nextLine();
String[] tokens = tokenLine.split(" ");
int total =0;
int move =0;
for(int i=0; i< size;i++ ){
total += Integer.parseInt(tokens[i]);
}
for(int j=0; j< size;j++ ){
if( Integer.parseInt(tokens[j])> total/size){
move += Integer.parseInt(tokens[j]) - total/size;
}
}
System.out.println("The minimum number of moves is "+move+".\n");
}
}
}

2009年6月2日 星期二

ACM Q579



import java.util.Scanner;
public class Q579 {
public static void main(String args[]) {
Scanner cin = new Scanner(System.in);
while(cin.hasNextLine() ){
String inputLine = cin.nextLine();
String[] tokens = inputLine.split(":");
float h = Float.parseFloat(tokens[0]);
float m = Float.parseFloat(tokens[1]);
if(h==0 && m==0){
break;
}
float angle = (float) Math.abs(h * 30 + m /2 - m * 6);
if( angle > 180){
System.out.printf("%.3f\n",360-angle);
}else{
System.out.printf("%.3f\n",angle);
}
}

}
}

ACM Q11296



import java.util.Scanner;
public class Q11296 {
public static void main(String args[]) {
Scanner cin = new Scanner(System.in);
long n;
while(cin.hasNextInt() ){
n = cin.nextInt();
if(n > 1000001){
continue;
}
long count=0;
count = (n/2+1)*(n/2+2)/2;
System.out.println(count);
}

}
}



利用公式解!!!



import java.util.Scanner;
public class Q11296 {
public static void main(String args[]) {
Scanner cin = new Scanner(System.in);
int n;
while(cin.hasNextInt() ){
n = cin.nextInt();
if(n > 1000001){
continue;
}
long count=0;
for(int x=n%2; x<=n ;x=x+2){
count += (n-x)/2+1;
}
System.out.println(count);
}

}
}



不用公式解!!!

How to format source code for blogger

Please visit this site for further information

http://www.manoli.net/csharpformat/

http://formatmysourcecode.blogspot.com/

It may re-decorate your source code into HTML format

Software Company Interview Question




using System;
using System.Collections.Generic;
using System.Text;
using System.Collections;

namespace ApplicantTestin
{
/// The DataObject class stored with a key
class DataObject
{
// Populate
private string keyStr;
private long keyValue;
private bool keyStatus;



public DataObject(string inputKey)
{
keyStr = inputKey;
}

public string Str
{
get { return keyStr; }
}
public long Val
{
get { return keyValue; }
set { keyValue = value; }
}
public bool Status
{
get { return keyStatus; }
set { keyStatus = value; }
}


public void Decrease()
{
keyValue--;
}
public void Increase()
{
keyValue++;
}


}


class Program
{
static Hashtable Data = new Hashtable();

static string[] StaticData = new string[] { "X-ray","Echo","Alpha", "Yankee","Bravo", "Charlie",
"Delta", "Hotel", "India", "Juliet", "Foxtrot","Sierra",
"Mike","Kilo", "Lima", "November", "Oscar", "Papa", "Qubec",
"Romeo", "Tango","Golf", "Uniform", "Victor", "Whisky",
"Zulu"};

static void Main(string[] args)
{
for (int i = 0; i < StaticData.Length; i++)
Data.Add(StaticData[i].ToLower(), new DataObject(StaticData[i]));
while (true)
{
PrintSortedData();
Console.WriteLine();
Console.Write("> ");
string str = Console.ReadLine();
string[] strs = str.Split(' ');

if (strs[0] == "q")
break;
else if (strs[0] == "printv")
PrintSortedDataByValue();
else if (strs[0] == "print")
PrintSortedData();
else if (strs[0] == "inc")
Increase(strs[1]);
else if (strs[0] == "dec")
Decrease(strs[1]);
else if (strs[0] == "swap")
Swap(strs[1], strs[2]);
else if (strs[0] == "ref")
Ref(strs[1], strs[2]);
else if (strs[0] == "unref")
UnRef(strs[1]);
}
}

/// <summary>
/// Create a reference from one data object to another.
/// </summary>
/// <param name="key1">The object to create the reference on</param>
/// <param name="key2">The reference object</param>
static void Ref(string key1, string key2)
{
try
{
if (Data.ContainsKey(key1) && Data.ContainsKey(key2))
{
Data[key1.ToLower()] = Data[key2.ToLower()];
Console.WriteLine(key1 + " -> <" + ((DataObject)Data[key1.ToLower()]).Str + ">");
Console.WriteLine(key2 + " -> <" + ((DataObject)Data[key2.ToLower()]).Str + ">");
}
}
catch (Exception e)
{
Console.WriteLine(e);
}
}

/// <summary>
/// Removes an object reference on the object specified.
/// </summary>
/// <param name="key">The object to remove the reference from</param>
static void UnRef(string key)
{
try
{
if (Data.ContainsKey(key))
{
string tmpstr = key.Substring(0, 1).ToUpper() + key.Substring(1, key.Length - 1);
Data[key.ToLower()] = new DataObject(tmpstr);
}
}
catch (Exception e)
{
Console.WriteLine(e);
}

}

/// <summary>
/// Swap the data objects stored in the keys specified
/// </summary>
static void Swap(string key1, string key2)
{
// Populate
try
{
if (Data.ContainsKey(key1) && Data.ContainsKey(key2))
{
DataObject obj = (DataObject)Data[key1.ToLower()];
Data[key1.ToLower()] = Data[key2.ToLower()];
Data[key2.ToLower()] = obj;
Console.WriteLine(key1+ " <-> " + key2);
}
}
catch (Exception e)
{
Console.WriteLine(e);
}
}

/// <summary>
/// Decrease the Value field by 1 of the
/// data object stored with the key specified
/// </summary>
static void Decrease(string key)
{
try
{
if (Data.ContainsKey(key))
{

((DataObject)Data[key.ToLower()]).Decrease();
Console.WriteLine("Decrement: " + ((DataObject)Data[key.ToLower()]).Val);

}
}
catch (Exception e)
{
Console.WriteLine(e);
}
}

/// <summary>
/// Increase the Value field by 1 of the
/// data object stored with the key specified
/// </summary>
static void Increase(string key)
{
try
{
if (Data.ContainsKey(key))
{
((DataObject)Data[key.ToLower()]).Increase();
Console.WriteLine("Increment: "+ ((DataObject)Data[key.ToLower()]).Val);
}
}
catch (Exception e)
{
Console.WriteLine(e);
}

}


/// <summary>
/// Prints the information in the Data hashtable to the console.
/// Output should be sorted by key
/// References should be printed between '<' and '>'
/// The output should look like the following :
///
///
/// Alpha...... -3
/// Bravo...... 2
/// Charlie.... <Zulu>
/// Delta...... 1
/// Echo....... <Alpha>
/// --etc---
///
/// </summary>
static void PrintSortedData()
{
ArrayList sortList = new ArrayList();
sortList.AddRange(Data.Keys);
sortList.Sort();
foreach (string s in sortList)
{
DataObject obj = (DataObject)Data[s];
string tmpstr = s.Substring(0,1).ToUpper()+s.Substring(1,s.Length-1);

if(obj.Str == tmpstr){
Console.WriteLine(tmpstr + " " + obj.Val);
}else{
Console.WriteLine(tmpstr + " <" + obj.Str+">");
}
}
}


/// <summary>
/// Prints the information in the Data hashtable to the console.
/// Output should be sorted by stored value
/// References should be printed between '<' and '>'
/// Sorting order start from max to min, larger value takes priority.
/// The output should look like the following :
///
///
/// Bravo...... 100
/// Echo...... 99
/// Zulu...... 98
/// Charlie.... <Zulu>
/// Delta...... 34
/// Echo....... 33
/// Alpha...... <Echo>
/// --etc---
///
/// </summary>
static void PrintSortedDataByValue()
{
ArrayList sortlistValue = new ArrayList();
sortlistValue.AddRange(Data.Keys);
for (int i = sortlistValue.Count-1; i > 0; i--)
{
for (int j = 0; j < i; j++)
{
if (((DataObject)Data[sortlistValue[j+1]]).Val > ((DataObject)Data[sortlistValue[j]]).Val){
string tmp = sortlistValue[j].ToString();
sortlistValue[j] = sortlistValue[j+1];
sortlistValue[j+1] = tmp;
}
}

}

Console.WriteLine("Sorted by value\n====================");

foreach (string s in sortlistValue)
{
DataObject obj = (DataObject)Data[s];
string tmpstr = s.Substring(0, 1).ToUpper() + s.Substring(1, s.Length - 1);

if (obj.Str == tmpstr)
{
Console.WriteLine(tmpstr + " " + obj.Val);
}
else
{
Console.WriteLine(tmpstr + " <" + obj.Str + ">");
}
}
Console.WriteLine("====================");
}
}
}

2009年6月1日 星期一

ACM Q101 - C#



using System;
using System.Collections.Generic;
using System.Text;
using System.Collections;

namespace test
{
class test
{

static void Main(){

int stack_n = int.Parse(Console.ReadLine());
Stack<int>[] intStack = new Stack<int>[stack_n];
int[] Block = new int[stack_n];

for (int i = 0; i < stack_n; i++)
{
Block[i] = i;
intStack[i] = new Stack<int>();
intStack[i].Push(Block[i]);
}

while (true)
{

string rl = Console.ReadLine();
string[] parts = rl.Split(' ');

if (parts.Length == 4)
{
int a = int.Parse(parts[1]);
int b = int.Parse(parts[3]);

if (parts[0] == "move" && parts[2] == "onto")
{

while (intStack[Block[a]].Peek() != a)
{
int tmp = intStack[Block[a]].Pop();
intStack[tmp].Push(tmp);
Block[tmp] = tmp;
}
while (intStack[Block[b]].Peek() != b)
{
int tmp = intStack[Block[b]].Pop();
intStack[tmp].Push(tmp);
Block[tmp] = tmp;
}
int tmp2 = intStack[Block[a]].Pop();
intStack[Block[b]].Push(tmp2);
Block[tmp2] = Block[b];
}
else if (parts[0] == "move" && parts[2] == "over")
{
while (intStack[Block[a]].Peek() != a)
{
int tmp = intStack[Block[a]].Pop();
intStack[tmp].Push(tmp);
Block[tmp] = Block[b];
}
int tmp2 = intStack[Block[a]].Pop();
intStack[Block[b]].Push(tmp2);
Console.WriteLine("moveover " + tmp2+" "+a +" "+b);
Block[tmp2] = Block[b];
}
else if (parts[0] == "pile" && parts[2] == "onto")
{
Stack<int> tmpstack = new Stack<int>();
while (intStack[Block[a]].Peek() != a)
{
int tmp = intStack[Block[a]].Pop();
tmpstack.Push(tmp);
}
tmpstack.Push(intStack[Block[a]].Pop());
while (intStack[Block[b]].Peek() != b)
{
int tmp = intStack[Block[b]].Pop();
intStack[tmp].Push(tmp);
Block[tmp] = tmp;
}
while (tmpstack.Count > 0)
{
int tmp = tmpstack.Pop();
intStack[Block[b]].Push(tmp);
Block[tmp] = Block[b];
}
}
else if (parts[0] == "pile" && parts[2] == "over")
{
Stack<int> tmpstack = new Stack<int>();
while (intStack[Block[a]].Peek() != a)
{
int tmp = intStack[Block[a]].Pop();
tmpstack.Push(tmp);
}
tmpstack.Push(intStack[Block[a]].Pop());
while (tmpstack.Count > 0)
{
int tmp = tmpstack.Pop();
intStack[Block[b]].Push(tmp);
Block[tmp] = Block[b];
}
}
}

if(parts[0]=="quit")
{
break;
}

}
for (int i = 0; i < stack_n; i++)
{
Console.Write(i + " :");
foreach (int j in intStack[i])
{
Console.Write(j);
}
Console.WriteLine();
}
}


}
}