Monday, July 17, 2017

PAT (Basic Level) Practise (中文) 1001

卡拉兹(Callatz)猜想:
对任何一个自然数n,如果它是偶数,那么把它砍掉一半;如果它是奇数,那么把(3n+1)砍掉一半。这样一直反复砍下去,最后一定在某一步得到n=1。卡拉兹在1950年的世界数学家大会上公布了这个猜想,传说当时耶鲁大学师生齐动员,拼命想证明这个貌似很傻很天真的命题,结果闹得学生们无心学业,一心只证(3n+1),以至于有人说这是一个阴谋,卡拉兹是在蓄意延缓美国数学界教学与科研的进展……
我们今天的题目不是证明卡拉兹猜想,而是对给定的任一不超过1000的正整数n,简单地数一下,需要多少步(砍几下)才能得到n=1?
输入格式:每个测试输入包含1个测试用例,即给出自然数n的值。
输出格式:输出从n计算到1需要的步数。
输入样例:
3
输出样例:
5

//C++ Program
#include <iostream>
using namespace std;

int main(void) {
 int n, count = 0;
 cin>>n;
 if (n>0&&n<=1000) {
  while (n!=1) {
   if (n%2==0) {
    n /= 2;
   } else {
    n = (3*n+1)/2;
   }
   ++count;
  }
  cout<<count<<endl;
 }

 return 0;
}

Tuesday, September 27, 2016

Android Studio Project: Experiment Manager

Experiment Manager, focusing on biological research.

This application makes experiments arrangement as its main function, supplemented by columns such as biological information and data searching. What’s more, it can save your experiments experiences. With this application, you will be able to do every experiment more methodically. When not doing an experiment, you can either know about biological related news or seeking for the latest information from professional websites. How flexible and convenient!

Experiment Manager


Click To Download On Amazon Appstore

Project site

Thursday, September 22, 2016

Android Studio Project: Outside News Updated

The new version of Outside News repairs a bug that the application couldn't exit from MainActivity directly.

Installation package

Project site

Thursday, September 8, 2016

LeetCode Online Judge-10. Regular Expression Matching

Implement regular expression matching with support for '.' and '*'.
'.' Matches any single character.
'*' Matches zero or more of the preceding element.

The matching should cover the entire input string (not partial).

The function prototype should be:
bool isMatch(const char *s, const char *p)

Some examples:
isMatch("aa","a") → false
isMatch("aa","aa") → true
isMatch("aaa","aa") → false
isMatch("aa", "a*") → true
isMatch("aa", ".*") → true
isMatch("ab", ".*") → true
isMatch("aab", "c*a*b") → true

Tell you a secret. I initially added only 1 line and it was accepted!
Well, that's "return s.matches(p);" in Java. Ha-ha!

OK, I did know this answer was not expected for training.
So here comes my recursive program.

//Java Program: Regular Expression Matching
public class Solution {
    public boolean isMatch(String s, String p) {

if (p.length()==0) {
if (s.length()==0) {
return true;
} else {
return false;
}
}

if (p.length()==1) {
if (s.length()==1&&(p.charAt(0)=='.'||s.charAt(0)==p.charAt(0))) {
return true;
}
return false;
}

if (s.length()>0&&p.length()>0&&(p.charAt(p.length()-1)=='.'||s.charAt(s.length()-1)==p.charAt(p.length()-1))) {
return isMatch(s.substring(0, s.length()-1), p.substring(0, p.length()-1));
}

if (p.length()>1&&p.charAt(p.length()-1)=='*') {
if (s.length()==0) {
return isMatch(s, p.substring(0, p.length()-2));
}
if (p.charAt(p.length()-2)!='.'&&s.charAt(s.length()-1)!=p.charAt(p.length()-2)) {
return isMatch(s, p.substring(0, p.length()-2));
}
if (isMatch(s, p.substring(0, p.length()-2))) {
return true;
}
if (isMatch(s.substring(0, s.length()-1), p.substring(0, p.length()-2))) {
return true;
}
return isMatch(s.substring(0, s.length()-1), p);
}

return false;

    }
}