Categories
不学无术 木有技术

Python入门讲义

Python入门

By nincompoop

图文PDF下载:
Python讲义

参考教程:
http://www.cnblogs.com/known/archive/2010/07/31/1789249.html
 

官方介绍:Python是一种简单易学,功能强大的编程语言,它有高效率的高层数据结构,简单而有效地实现面向对象编程。Python简洁的语法和对动态输入的支持,再加上解释性语言的本质,使得它在大多数平台上的许多领域都是一个理想的脚本语言,特别适用于快速的应用程序开发。
创造者:Guido van Rossum(吉多·范罗苏姆【荷兰】)
它的特色:简单、易学、免费、开源、高层语言、可移植性、解释性、面向对象、可扩展性、可嵌入性、丰富的库。
官网:http://python.org/
Python支持命令式程序设计、面向对象程序设计、函数式编程、面向切面编程、泛型编程多种编程范式。与Scheme、Ruby、Perl、Tcl等动态语言一样,Python具备垃圾回收功能,能够自动管理存储器使用。它经常被当作脚本语言用于处理系统管理任务和网络程序编写,然而它也非常适合完成各种高级任务。Python虚拟机本身几乎可以在所有的作业系统中运行。

版本:3.x与2.x (Python 3.0于2008年12月3日发布,此版不完全兼容之前的Python源代码。不过,很多新特性后来也被移植到旧的Python 2.6/2.7版本。)
 

Hello World 程序

下面是一个在标准输设备上输出Hello World的简单程序,这种程序通常作为开始学习编程语言时的第一个程序:

  • 适用于Python 3.0以上版本以及Python 2.6Python 2.7
  • 适用于Python 2.6以下版本以及Python 2.6Python 2.7
print("Hello, world!")
print "Hello, world!"

将这行程序码保存为myhello.py。然后在Linux终端机下输入python myhello.py,或者在Windows命令编辑字符下输入myhello.py运行。Python也可以单步直译运行。运行Python解释器进入交互式命令行的环境,你可以在提示符号>>>旁输入print(“Hello, world!”),按Enter键输出结果:

  • 适用于Python 3.0以上版本以及Python 2.6Python 2.7
  • 适用于Python 2.6以下版本以及Python 2.6Python 2.7
>>> print("Hello, world!")
Hello, world!
>>> print "Hello, world!"
Hello, world!
注意,低于3.0版本的Python,”Hello, world!”周围不需要括号。Python 3.x与Python 2.x的print语法是不一样的。

Python语法

Python的设计目标之一是让代码具备高度的可阅读性。它设计时尽量使用其它语言经常使用的标点符号和英文单字,让代码看起来整洁美观。它不像其他的静态语言如C、Pascal那样需要重复书写声明语句,也不像它们的语法那样经常有特殊情况和惊喜。

缩进(告别{}!)

Python开发者有意让违反了缩进规则的程序不能通过编译,以此来强制程序员养成良好的编程习惯。并且Python语言利用缩进表示语句块的开始和退出(Off-side规则),而非使用花括号或者某种关键字。增加缩进表示语句块的开始,而减少缩进则表示语句块的退出。缩进成为了语法的一部分。例如if语句:
 
根据PEP的规定,必须使用4个空格来表示每级缩进。使用Tab字符和其它数目的空格虽然都可以编译通过,但不符合编码规范支持Tab字符和其它数目的空格仅仅是为兼容很旧的的Python程序和某些有问题的编辑程序。

语句和控制流

  • if语句,当条件成立时运行语句块。经常与elseelif(相当于else if) 配合使用。
  • for语句,遍列列表、字符串、字典、集合等迭代器,依次处理迭代器中的每个元素。
  • while语句,当条件为真时,循环运行语句块。
  • try语句。与except,finally配合使用处理在程序运行中出现的异常情况。
  • class语句。用于定义类型。
  • def语句。用于定义函数和类型的方法。
  • pass语句。表示此行为空,不运行任何操作。
  • assert语句。用于程序调适阶段时测试运行条件是否满足。
  • with语句。Python2.6以后定义的语法,在一个场景中运行语句块。比如,运行语句块前加密,然后在语句块运行退出后解密。
  • yield语句。在迭代器函数内使用,用于返回一个元素。自从Python 2.5版本以后。这个语句变成一个运算符。
  • raise语句。制造一个错误。
  • import语句。导入一个模块或包。
  • from import语句。从包导入模块或从模块导入某个对象。
  • import as语句。将导入的对象赋值给一个变量。
  • in语句。判断一个对象是否在一个字符串/列表/元组里。

表达式

Python的表达式写法与C/C++类似。只是在某些写法有所差别。

  • 主要的算术运算符C/C++类似+, -, *, /, //, **, ~, %分别表示加法或者取正、减法或者取负、乘法、除法、整除、乘方、取补、取模。>>, <<表示右移和左移。&, |, ^表示二进制的AND, OR, XOR运算。>, <, ==, !=, <=, >=用于比较两个表达式的值,分别表示大于、小于、等于、不等于、小于等于、大于等于。在这些运算符里面,~, |, ^, &, <<, >>必须应用于整数。
  • Python使用andornot表示逻辑运算
  • is, is not用于比较两个变量是否是同一个对象。in, not in用于判断一个对象是否属于另外一个对象。
  • Python支持“列表推导式”(list comprehension),比如计算0-9的平方和:

 

  • Python使用lambda表示匿名函数。匿名函数体只能是表达式。比如:

  • Python使用y if cond else x表示条件表达式。意思是当cond为真时,表达式的值为y,否则表达式的值为x。相当于C++和Java里的cond?y:x。

  • Python区分列表(list)和元组(tuple)两种类型。list的写法是[1,2,3],而tuple的写法是(1,2,3)。可以改变list中的元素,而不能改变tuple。在某些情况下,tuple的括号可以省略。tuple对于赋值语句有特殊的处理。因此,可以同时赋值给多个变量,比如:

  • Python使用‘(单引号)“(双引号)来表示字符串。与Perl、Unix Shell语言或者Ruby、Groovy等语言不一样,两种符号作用相同。一般地,如果字符串中出现了双引号,就使用单引号来表示字符串;反之则使用双引号。如果都没有出现,就依个人喜好选择。出现在字符串中的(反斜杠)被解释为特殊字符,比如n表示换行符。表达式前加r指示Python不解释字符串中出现的。这种写法通常用于编写正则表达式或者Windows文件路径。
  • Python支持列表切割(list slices),可以取得完整列表的一部分。支持切割操作的类型有str, bytes, list, tuple等。它的语法是…[left:right]或者…[left:right:stride]。假定nums变量的值是[1, 3, 5, 7, 8, 13, 20],那么下面几个语句为真:
    • nums[2:5] == [5, 7, 8] 从下标为2的元素切割到下标为5的元素,但不包含下标为5的元素。
    • nums[1:] == [3, 5, 7, 8, 13, 20] 切割到最后一个元素。
    • nums[:-3] == [1, 3, 5, 7] 从最开始的元素一直切割到倒数第3个元素。
    • nums[:] == [1, 3, 5, 7, 8, 13, 20] 返回所有元素。改变新的列表不会影响到nums。
    • nums[1:5:2] == [3, 7] 从下标为1的元素切割到下标为5的元素但不包含下标为5的元素,且步长为2

函数

Python的函数支持递归、默认参数值、可变参数,但不支持函数重载。为了增强代码的可读性,可以在函数后书写“文档字符串”(Documentation Strings,或者简称docstrings),用于解释函数的作用、参数的类型与意义、返回值类型与取值范围等。可以使用内置函数help()打印出函数的使用帮助。比如:

对象的方法

对象的方法是指绑定到对象的函数。调用对象方法的语法是instance.method(arguments)。它等价于调用Class.method(instance, arguments)。当定义对象方法时,必须显式地定义第一个参数,一般该参数名都使用self,用于访问对象的内部数据。这里的self相当于C++, Java里面的this变量,但是我们还可以使用任何其它合法的参数名,比如this 和 mine等,selfC++,Java里面的this不完全一样,它可以被看作是一个习惯性的用法,我们传入任何其它的合法名称都行,比如:

Python认识一些以“__”开始并以“__”结束的特殊方法名,它们用于实现运算符重载和实现多种特殊功能。

类型

Python采用动态类型系统。在编译的时候,Python不会检查对象是否拥有被调用的方法或者属性,而是直至运行时,才做出检查。所以操作对象时可能会抛出异常。不过,虽然Python采用动态类型系统,它同时也是强类型的。Python禁止没有明确定义的操作,比如数字加字符串。
与其它面向对象语言一样,Python允许程序员定义类型。构造一个对象只需要像函数一样调用类型即可,比如,对于前面定义的Fish类型,使用Fish()。类型本身也是特殊类型type的对象(type类型本身也是type对象),这种特殊的设计允许对类型进行反射编程。
Python内置丰富的数据类型。与Java、C++相比,这些数据类型有效地减少代码的长度。下面这个列表简要地描述了Python内置数据类型(适用于Python 3.x):

类型

描述

例子

str

一个由字符组成的不可更改的有串行。在Python 3.x里,字符串由Unicode字符组成。 'Wikipedia'
"Wikipedia"
"""Spanning
multiple
lines"""
bytes 一个由字节组成的不可更改的有串行。 b'Some ASCII'
b"Some ASCII"
list 可以包含多种类型的可改变的有串行 [4.0, 'string', True]
tuple 可以包含多种类型的不可改变的有串行 (4.0, 'string', True)
set,frozenset 与数学中集合的概念类似。无序的、每个元素唯一。 {4.0, 'string', True}
frozenset([4.0, 'string', True])
dict 一个可改变的由键值对组成的无串行。 {'key1': 1.0, 3: False}
int 精度不限的整数 42
float 浮点数。精度与系统相关。 3.1415927
complex 复数 3+2.7j
bool 逻辑值。只有两个值:真、假 True
False
除了各种数据类型,Python语言还用类型来表示函数、模块、类型本身、对象的方法、编译后的Python代码、运行时信息等等。因此,Python具备很强的动态性。

开发环境

适用于Python的集成开发环境(IDE)软件,除了标准二进制发布包所附的IDLE之外,还有许多其他选择。其中有些软件设计有语法着色、语法检查、运行调试、自动补全、智能感知等便利功能。由于Python的跨平台出身,这些软件往往也具备各种操作系统的版本或一定的移植性。
而很多并非集成开发环境软件的文本编辑器,也对Python有不同程度的支持,并且加上专门为Python设计的编辑器插件也会有很高的可用性。
专门为Python设计的IDE软件:

  • Eric:基于PyQt的自由软件,功能强大。支持自动补全、智能感知、自动语法检查、工程管理、svn/cvs集成、自动单元测试等功能。调试功能与Visual Studio和Eclipse类似。目前同时有两个版本。Eric4支持Python2.x,Eric5支持Python3.x。使用前需要先安装相应的PyQt版本。
  • IDLE:Python“标准”IDE。一般随Python而安装,支持较少的编辑功能。调试功能也比较弱。
  • KomodoKomodo Edit:后者是前者的免费精简版。也可以用于PHP,Ruby,Javascript,Perl,Web和云开发。
  • PyCharm:由JetBrains打造,该公司的Java IDE软件IntelliJ拥有海量的用户;PyCharm具备一般IDE的功能,比如, 调试、语法高亮、Project管理、代码跳转、智能提示、自动完成、单元测试、版本控制等等,同时另外,PyCharm还提供了一些很好的功能用于Django开发,同时支持Google App Engine,更酷的是,PyCharm支持IronPython。PyCharm是商业软件,目前已经到2.5版本。
  • PythonWin:包含在pywin32内的编辑器,仅适用于Windows。
  • SPE(Stani’s Python Editor):功能较多的免费软件,依赖wxPython
  • Ulipad:功能较全的免费软件,依赖wxPython
  • WingIDE:可能是功能最全的IDE,但不是免费软件。
  • PyScripter:功能较全的开源IDE,使用Delphi开发。

通用IDE / 文本编辑器:

 

官网(英文):http://www.djangoproject.com/

自学材料(中文):http://djangobook.py3k.cn/2.0/

Django是一个开放源代码Web应用框架,由Python写成。采用了MVC软件设计模式,即模型M,视图V和控制器C。它最初是被开发来用于管理劳伦斯出版集团旗下的一些以新闻内容为主的网站的。并于2005年7月在BSD许可证下发布。这套框架是以比利时吉普赛爵士吉他手Django Reinhardt来命名的。
Django的主要目标是使得开发复杂的、数据库驱动的网站变得简单。Django注重组件的重用性和“可插拔性”,敏捷开发DRY法则(Don’t Repeat Yourself)。在Django中Python被普遍使用,甚至包括配置文件和数据模型。
Django 可以运行在启用了 mod python 的 Apache 2 上,或是任何WSGI兼容的Web服务器。 Django也有启动FastCGI服务的能力,因此能够应用于任何支持FastCGI的机器上。
下列数据库引擎被Django官方支持:

Microsoft SQL Server 的适配器正在开发中,处于试验阶段。(注:SQL Server的支持在1.0版本中已经被完全去除)
Django1.0已经可以利用Jython运行在任何J2EE服务器。

Categories
不学无术 未分类

SRTP学分认定办法

SRTP学分认定办法(附附件表格) (3)
 
测试附件上传

Categories
不学无术

1010. Radix (25)

Given a pair of positive integers, for example, 6 and 110, can this equation 6 = 110 be true? The answer is “yes”, if 6 is a decimal number and 110 is a binary number.
Now for any pair of positive integers N1 and N2, your task is to find the radix of one number while that of the other is given.
Input Specification:
Each input file contains one test case. Each case occupies a line which contains 4 positive integers:
N1 N2 tag radix
Here N1 and N2 each has no more than 10 digits. A digit is less than its radix and is chosen from the set {0-9, a-z} where 0-9 represent the decimal numbers 0-9, and a-z represent the decimal numbers 10-35. The last number “radix” is the radix of N1 if “tag” is 1, or of N2 if “tag” is 2.
Output Specification:
For each test case, print in one line the radix of the other number so that the equation N1 = N2 is true. If the equation is impossible, print “Impossible”. If the solution is not unique, output the smallest possible radix.
Sample Input 1:

6 110 1 10

Sample Output 1:

2

Sample Input 2:

1 ab 1 2

Sample Output 2:

Impossible

=================================================================
貌似是浙大PAT通过率最低的一题,6%
一开始很不解,为啥通过率这么低,明明是很容易读和做的一题,而且内存、时间都挺宽裕。
做起来才发现,对于我来说有这些问题:
1)思维定势。一开始误以为只可能进制到36为止,因为题目输入的一个数的进制最高只能到36,后来想想,这货是无穷无尽的,
假定A为已知进制为r1的数,B为待测进制(r2)的数,那么当第一次遇到r2, s.t. B@r2 > A@r1时,才是终止条件。
2)收敛速度。解决问题1后,竟然尼玛有一个运行超时了,仔细想想,r2按照线性递增测算的话,遇到以下输入将会是无穷无尽的时间才能判断
e.g.

zzzzzzzzzz 1 1 36

所以r2肯定不能一个一个来。
然后写了个简单的回溯,弄了个加速度的东西accRate,具体见代码》。如果一路成功会越走越快,如果遇到失败,那么退回backup的值,然后加速度回到1,然后继续。粗略一算这货的收敛速度或许是log级别的,直接放到pat判题,然后全过了:)
另外,发现c++用里面的pow算乘方其实挺快的,本来以为速度慢在这里,呵呵。
还有使用long long是因为按照题目的限定来说,最大的数 zzzzzzzzzz 放在一个long里面似乎不够用…当然用double或许可以,不过…浮点运算对cpu来说是个恐怖的事情吧
=================================================================
以下是
我的参考答案,肯定不是最好的
里面有些不必要的东西,懒得删了。

#include 
#include 
#include 
#include 
using namespace std;
char *N1, *N2;
long tag, radix;
long valueOfChar(char c)
{
	//get the value of a char
	//0~9,a-z
	long iC = (long)c;
	if(iC >= 0x30 && iC <= 0x39)
		return iC - 0x30;
	else if(iC >=0x61 && iC <= 0x7a) //a-z
		return iC - 0x57;
	else //wrong input
	{
		cerr << "??? " << c < 1
		int X = valueOfChar(input[0]);
		long long Z = X * pow(radix, size-1);
		return Z + getDecVal(input+1, radix, size-1);
	}
}
long getMinPossibleRadix(char* input)
{
	//获得最小可能的基数
	long maxDigit = 0;
	long nowDigit;
	while( (*input) != '')
	{
		nowDigit = valueOfChar((*input));
		if(nowDigit > maxDigit)
			maxDigit = nowDigit;
		input ++;
	}
	return maxDigit + 1;
}
char *trial;
int main()
{
	clock_t start,mid;
	N1 = new char[10];
	N2 = new char[10];
	long minPossibleRadix;
	cin >> N1 >> N2 >> tag >> radix;
	start = clock();
	long long lDecVal;
	if( tag == 1)
	{
		lDecVal = getDecVal(N1,radix);
		minPossibleRadix = getMinPossibleRadix(N2);
		trial = N2;
	}
	else
	{
		lDecVal = getDecVal(N2,radix);
		minPossibleRadix = getMinPossibleRadix(N1);
		trial = N1;
	}
	long long rVal = -1;
	int strSize = strlen(trial);
	long long accRate = 1;//加速度
	long long i= minPossibleRadix;
	long long backup = i;
	bool inTrial = false; //正在尝试的状态
	while(true)
	{
		rVal = getDecVal(trial,i,strSize);
		if(rVal == lDecVal)
		{
			cout << i;
			//mid = clock();
			//cout << mid - start << endl;
			return 0;
		}
		else if(rVal > lDecVal)
		{
			if(!inTrial)
				break;
			else
			{
				//回溯
				inTrial = false;
				if( i == backup + 1)
					break;
				i = backup;
				accRate = 1;
			}
		}
		else
		{
			inTrial = true;
			backup = i;
			i += accRate;
			accRate *= 10;
			//cout << i << endl;
		}
	}
	mid = clock();
	//cout << mid - start << endl;
	cout << "Impossible";
	return 0;
}
Categories
不学无术

1004. Counting Leaves (30)

1004. Counting Leaves (30)

时间限制
400 ms
内存限制
32000 kB
代码长度限制
16000 B
判题程序
Standard
作者
CHEN, Yue
A family hierarchy is usually presented by a pedigree tree. Your job is to count those family members who have no child. 
Input
Each input file contains one test case. Each case starts with a line containing 0 < N < 100, the number of nodes in a tree, and M (< N), the number of non-leaf nodes. Then M lines follow, each in the format:

ID K ID[1] ID[2] ... ID[K]

where ID is a two-digit number representing a given non-leaf node, K is the number of its children, followed by a sequence of two-digit ID’s of its children. For the sake of simplicity, let us fix the root ID to be 01. 
Output
For each test case, you are supposed to count those family members who have no child for every seniority level starting from the root. The numbers must be printed in a line, separated by a space, and there must be no extra space at the end of each line.
The sample case represents a tree with only 2 nodes, where 01 is the root and 02 is its only child. Hence on the root 01 level, there is 0 leaf node; and on the next level, there is 1 leaf node. Then we should output “0 1” in a line.
Sample Input

2 1
01 1 02

Sample Output

0 1

======================================================
恩恩,数叶子,倒腾了一晚上,一直报段错误,最后知道真相的我眼泪掉下来》。。
原来是指针指来指去,本来要指子节点的ID结果弄成了自己的ID,然后数组就不对了》。。
思路比较简单,他要hierarchy,我们就搞一个
他要Node,我们就弄个结构给他
Node之间用指针传上就行了..虽然带两个*号的东西好久没用了,但是心里想着这货是指针的数组就行了…
为了节约时间,最后找结果的时候用的静态数组100*100,比较无耻,反正题目给够了内存…
每一行计算的时候,把自己的孩子加到数组的下一行里面,搜索到世界的尽头…然后就结束了
=======================================================

#include 
using namespace std;
struct Node
{
	Node **childNodes; //array of ptrChilds
	int ID; //ID  = array index + 1 ??
	int childSize;
	int nowIndex; //If nowIndex ==0 , this node must be a childless one.
	//int level; //root level = 0
	Node(int id, int cSize = 100)
	{
		ID = id;
		childSize = cSize;
		childNodes = new Node*[childSize];
		nowIndex = 0;
	}
	bool insertChildNode(Node* pNode)
	{
		if(nowIndex > childSize)
		{
			cerr << "child over size!" << endl;
			return false;
		}
		childNodes[nowIndex++] = pNode;
		return true;
	}
};
static int M,N;
static Node** allNodes;
static int lvls[100][100] = {0};
int main()
{
	//--First line
	cin >> N;
	cin >> M;
	//--init Node* array
	if(N == 0 || M == 0)
	{
		cout << "1";
		return 0;
	}
//We presume that there is no gap between IDs (eg: 01 02 05 06 07)
//otherwise we need to create more new Nodes
	allNodes = new Node*[N];
	for(int i=0; ilevel = 0; //root node level
	//--read M lines for the child info
	int id, K;
	for(int line=0; line> id;
		id--;  //turn to the index
		Node* pParent = allNodes[id];
		cin >> K;
		int cId;
		while(K > 0)
		{
			//link every child
			cin >> cId;
			cId --;
			pParent->insertChildNode(allNodes[cId]);
			K--;
		}
	}
	//--finished reading
	Node* root = allNodes[0];
	//--init first line
	if(root->nowIndex == 0)
	{
		cout << 1;
		return 0;
	}
	else
	{
		cout << 0;
	}
	for(int i=0; inowIndex ; i++)
	{
		Node* childNode = root->childNodes[i];
		lvls[0][i] =childNode->ID -1;
	}
	//GOGOGO!!!
	int row=0, col=0;
	int nextColOffset = 0;
	int count = 0;
	while(true)
	{
		int posNow = lvls[row][col];
		if( posNow == 0)
		{
			if(col == 0)			//end of all things
				break;
			row ++;
			col = 0;
			cout << " " << count;
			count = 0; //reset line count
			nextColOffset = 0;
			continue; //move to the head of next line
		}
		Node* now = allNodes[posNow];
		if(now->nowIndex <= 0)
			count ++;
		else
		{
			//put its children into next line
			for(int i=0; inowIndex; i++)
			{
				lvls[row+1][nextColOffset++] = now ->childNodes[i] ->ID -1;
			}
		}
		col ++;
	}
	return 0;
}
Categories
不学无术

1050. String Subtraction (20)

1050. String Subtraction (20)
时间限制
10 ms
内存限制
32000 kB
代码长度限制
16000 B
判题程序
Standard
Given two strings S1 and S2, S = S1 – S2 is defined to be the remaining string after taking all the characters in S2 from S1. Your task is simply to calculate S1 – S2 for any given strings. However, it might not be that simple to do it fast.
Input Specification:
Each input file contains one test case. Each case consists of two lines which gives S1 and S2, respectively. The string lengths of both strings are no more than 104. It is guaranteed that all the characters are visible ASCII codes and white space, and a new line character signals the end of a string.
Output Specification:
For each test case, print S1 – S2 in one line.
Sample Input:

They are students.
aeiou

Sample Output:

Thy r stdnts.

===================================================
不用动啥脑子的一道题,只要避免使用cin/cout就行了。反正内存给了好大好大,要争取时间的话,就给一共128个ascii字符做个map,里面存放是不是被ban掉了。
===================================================

#include 
using namespace std;
static char S1[10000];  //S1
static char S2[10000];  //S2
static char S3[10000];  //result
static bool mapAscii[127];
int main()
{
	char* pS1 = &S1[0];
	char* pS2 = &S2[0];
	gets(pS1);
	gets(pS2);
	int asc;
	while( (*pS2) != '')
	{
		asc = (int)(*pS2);
		mapAscii[asc] = true;
		pS2++;
	}
	char* pS3 = &S3[0];
	while( (*pS1) != '')
	{
		asc = (int)(*pS1);
		if(!mapAscii[asc])
		{
			(*pS3) = (char) asc;
			pS3++;
		}
		pS1++;
	}
	(*pS3) = ''; // EOL
	puts(&S3[0]);
	//system("pause");
	return 0;
}
Categories
不学无术 木有技术 爪机爪机

[转]android 人脸识别

source:http://blog.sina.com.cn/s/blog_677fb16e010148be.html

import android.app.Activity;
import android.widget.TextView;
import android.os.Bundle;
import android.media.FaceDetector;    // 人脸识别接口
import android.widget.ImageView;
import android.graphics.BitmapFactory;
import android.graphics.Bitmap;
import android.graphics.PointF;
import android.graphics.Matrix;
import android.util.Log;
import android.graphics.Canvas;
import android.graphics.Paint;
public class MyDetectActivity extends Activity {
       private ImageView mImageView;    // 图片显示控件
       private Bitmap mBitmap;
       private float mScale = 1F;
       @Override
       public void onCreate(Bundle savedInstanceState) {
                super.onCreate(savedInstanceState);
                setContentView(R.layout.main);
                mImageView = (ImageView) this.findViewById(R.id.image);
                detect();      // 识别函数
       }
       private void handleFace(FaceDetector.Face f) {        // 在图片上对每张脸进行处理
                PointF midPoint = new PointF();
                int r = ((int) (f.eyesDistance() * mScale * 1.5));         // 取眼睛间距离
                f.getMidPoint(midPoint);       // 取脸的中点
                midPoint.x *= mScale;
                midPoint.y *= mScale;
                Canvas c = new Canvas(mBitmap);
                Paint p = new Paint();
                p.setAntiAlias(true);
                p.setAlpha(0x80);
                c.drawCircle(midPoint.x, midPoint.y, r, p)        // 用半透明标出人脸区域;
                mImageView.setImageBitmap(mBitmap);          // 显示图片
       }
       private void detect() {
                Matrix matrix = new Matrix();
                FaceDetector.Face[] mFaces = new FaceDetector.Face[3];         // 定义最多识别三张脸
                int mNumFaces = 0;
                mBitmap = BitmapFactory.decodeResource(getResources(), R.drawable.baby);     // 取原始图
                if (mBitmap == null) {
                         return;
                }
                if (mBitmap.getWidth() > 256) {
                         mScale = 256.0F / mBitmap.getWidth();
                }
                matrix.setScale(mScale, mScale);
                Bitmap faceBitmap = Bitmap.createBitmap(mBitmap, 0, 0, mBitmap
                                   .getWidth(), mBitmap.getHeight(), matrix, true);        // 生成缩放后的新图
                mScale = 1.0F / mScale;
                if (faceBitmap != null) {
                         FaceDetector detector = new FaceDetector(faceBitmap.getWidth(),
                                            faceBitmap.getHeight(), mFaces.length); // 创建识别器
                         mNumFaces = detector.findFaces(faceBitmap, mFaces);    // 识别
                         if (mNumFaces > 0) {
                                   for (int i = 0; i < mNumFaces; i++) {
                                            handleFace(mFaces[i]);        // 调用函数对人脸画面进行处理
                                   }
                         }
                }
       }
}
Categories
不学无术

1041. Be Unique (20)

一道难题?主要是木有方法。

1041. Be Unique (20)

时间限制  
20 ms
内存限制
32000 kB
代码长度限制
16000 B
判题程序
Standard
作者
CHEN, Yue
Being unique is so important to people on Mars that even their lottery is designed in a unique way.  The rule of winning is simple: one bets on a number chosen from [1, 104].  The first one who bets on a unique number wins.  For example, if there are 7 people betting on 5 31 5 88 67 88 17, then the second one who bets on 31 wins.
Input Specification:
Each input file contains one test case.  Each case contains a line which begins with a positive integer N (<=105) and then followed by N bets.  The numbers are separated by a space.
Output Specification:
For each test case, print the winning number in a line.  If there is no winner, print “None” instead.
Sample Input 1:

7 5 31 5 88 67 88 17

Sample Output 1:

31

Sample Input 2:

5 888 666 666 888 888

Sample Output 2:

None

本人解答:

//#include 
//#include 
#include 
#include 
using namespace std;
const int MAX_NUM_A = 10001;
//const int MAX_NUM_B = 10000;
int storage[MAX_NUM_A] = {0};
//list possibles;
int possibles[MAX_NUM_A] = {0};
//0代表尚未存储,-1代表已经超额,其余代表输入时的顺序(第几个输入的这个数)
//int sequence[MAX_NUM_B];
//list ary_list;
/*
inline void remove(int r)
{
	//remove item from array
	ary_list.remove(r);
}
*/
int main()
{
	clock_t  clockBegin, clockMid, clockEnd;
	int possibleN = 0;
	int N;
	//cin >> N;
	scanf("%u",&N);
	int input;
	clockBegin = clock();
	for (int i=0; i> input;
		scanf("%u", &input);
		if(storage[input] == 0)
		{
			//尚未输入过这个数
			storage[input] = i+1; //记录输入的顺序
			//possibleN ++;
			//possibles.push_back(input);
			possibles[possibleN++] = input;
		}
		else
		{
			//重复了!
			//if(storage[input] > 0)
			//{
				//possibles.remove(input);
				//possibleN --;
			//}
			storage[input] = -1;
		}
	}
	/*
	if(possibles.size() <= 0)
	{
		cout << "None";
		return 0;
	}
	*/
	clockMid = clock();
	//cout << clockMid - clockBegin << endl;
	printf("%un",clockMid - clockBegin);
	//int count = ary_list.size();
	int min_seq = MAX_NUM_A;
	int min_val = -1;
	int i=0;
	while(i < possibleN)
	{
		int temp = storage[possibles[i]];
		if(temp > 0)
		{
			//if(temp < min_seq)
			//{
				min_seq = temp;
				min_val = possibles[i];
				break;
			//}
		}
		//possibles.pop_front();
		i ++ ;
	}
	clockEnd = clock();
	//cout << clockEnd - clockMid << endl;
	printf("%un",clockEnd - clockMid);
	if(min_val != -1)
	{
		//cout << min_val;
		printf("%u",min_val);
		return 0;
	}
	//cout << "None";
	printf("None");
	return 0;
}

知道结论是什么么?
结论是sscan比cin效率高很多!