HertzLee
BAN USERJava implementation:
when call, we call firstly printResult(input,"")
public static void printResult(String inputSequence,String result)
{
int len=inputSequence.length();
if(len==0)
{
System.out.println(result.toString());
return ;
}
char c=inputSequence.charAt(0);
String substring=inputSequence.substring(1);
if(!dict.containsKey(c))
{
//invalid character, skip
printResult(substring,result);
return ;
}
ArrayList<Character> ds= dict.get(c);
for(int i=0;i<ds.size();i++)
{
String r2= result+ ds.get(i);
printResult(substring, r2);
}
}
public class Main {
/**
* @param args the command line arguments
*/
public static void main(String[] args) throws IOException {
BufferedReader reader=new BufferedReader(
new InputStreamReader(System.in) );
while(true)
{
System.out.print("input integer:");
String line= reader.readLine();
if(line.isEmpty())
{
break;
}
int digit=Integer.parseInt(line);
System.out.println("processing : "+ digit );
boolean isP=checkPowerOf2(digit);
if(isP)
{
System.out.println(digit+" is power of 2");
}else
{
System.out.println(digit+" is NOT power of 2");
}
}
reader.close();
}
private static boolean checkPowerOf2(int digit) {
long p2=2;
while(true)
{
if(p2>digit)
{
return true;
}
if(digit%p2!=0)
{
return false;
}
p2*=2;
}
}
}
/*
- HertzLee February 16, 2011* File: main.cpp
* Author: Aaron
*
* Created on February 9, 2011, 6:21 PM
*/
#include <cstdlib>
#include <vector>
#include <ctype.h>
#define MAX_COUNT 10240
using namespace std;
/*
*
*/
void count_words(char* data,int* count);
int main(int argc, char** argv) {
int times=100;
int n=100;
vector<float> u;
char l[]="my test ; z";
while(true)
{
printf("input:");
char input[MAX_COUNT]={};
fgets(input, MAX_COUNT ,stdin);
input[strlen(input)-1]=0;
if(
strcasecmp((input), "quit") ==0
)
{
break;
}
int c=0;
count_words(input,&c);
printf("count :%d\n", c);
}
return 0;
}
bool is_invalid_in_word(char c)
{
bool v= !(
( (c>='a') && (c<='z') )
||
( (c>='A') && (c<='Z') )
);
return v;
//in your case,just change this function
//as below:
/**
if(c=='x')
{
return true;
}
if(c=='y')
{
return true;
}
if(c=='z')
{
return true;
}
return false;
*/
}
void count_words(char* data,int* count)
{
if(data==NULL)
{
*count=0;
return ;
}
int len=strlen(data);
bool inWord=false;
for(int i=0;i<len;i++)
{
char c=data[i];
bool invalid=is_invalid_in_word(c)
;
if(inWord)
{
if(invalid)
{
//*count=*count+1;
inWord=false;
}else
{
//skip
}
}else
{
if(invalid)
{
//skip
}else
{
*count=*count+1;
inWord=true;
}
}
}
printf("str:%s len :%d\n",data, len);
}