NGUI之UIGrid自适应屏幕

今天做项目遇到了一个很无奈的事. 在使用NGUI做Scroll View功能时我在Scroll View下面使用了UIGrid进行排列拖动项.

但是最后发现UIGrid居然不可以设置锚点.每个可拖动项也不可以设置锚点,因为即使加了脚本后可以设置也会出现问题,运行后拖动选择项会出现抖动现象,因为他们是有锚点的会根据位置调整一下,出现抖动的反应.

后来我发现虽然不可以在UIGrid上设置锚点但是可以控制UIGrid上Transform的大小这样里面的选择项也会跟着放大和缩小了.找到方法后我开始想怎么实现它最好.然后我找到了UIStretch这个脚本,可根据设置改变对象的大小.但上面的设置选项并没有符合我要求的选项.索性我就自己加了一条.

//首先在脚本的前面加入一个选择项
public enum Style
	{
		None,
		Horizontal,
		Vertical,
		Both,
		BasedOnHeight,
                BasedOnWidth,//fgreen加入的代码;
		FillKeepingRatio, 
		FitInternalKeepingRatio 
	}

//然后在UPDATE里面加入if判断根据自己设置的默认宽高进行比例缩放
  if (style == Style.BasedOnHeight)
  {
   size.x = relativeSize.x * rectHeight;
   size.y = relativeSize.y * rectHeight;
  }
 else if (style == Style.BasedOnWidth)//fgreen加入的代码
 {
    size.x = relativeSize.x * Screen.height/854;//我项目中设置默认的是854的高和480的宽;
    size.y = relativeSize.y * Screen.width/480;//当屏幕宽带大于或小于它们时求它们的除数;
    size.z = size.x;
 }


 

 

Unity3D 5.0 刚体API更改

刚才做项目时发现一个问题,在Unity5.0 以后的版本中 不可以直接对物体引用刚体的API如

hit.rigidbody.AddForce此用法在5.0以后已经不可以使用了而取而代之的是 使用

hit.GetComponent<Rigidbody>().AddForce来代替它.

相同的其他刚体API也是这样的,如发现其他函数的调用有问题也可以试试先获取组件再调用哦.

 

反转单向链表

   
 网上有许多代码 看了一下发现他们的好多反转后都缺失了头结点.
 所以自己又写了一个.

//
#include "stdafx.h"
#include "iostream"
using namespace std;
struct ListNode
{
ListNode* next;
int data;
	ListNode()
	{
	next=NULL;
	data=0;
 
	}
};
 
ListNode *ReverseList(ListNode *&list)
{
	ListNode *head = list, *last = NULL;
	list = list->next;
 	while (list){
		head->next = last;
		last = head;
		head = list;
		list = list->next;
	}
	head->next = last;
	return head;
}




//创建链表添加结点;
void CreateNode(ListNode*&head, int data)
{
	ListNode *fist = head;
	while (head != NULL)
	{
		if (head->next == NULL)
		{
			ListNode *tem = new ListNode();
			tem->data = data;
			head->next = tem;
			break;
		}
		head = head->next;
	}
	head = fist;
}
int main(int argc, char* argv[])
{
ListNode *head=new ListNode();
CreateNode(head,1);
CreateNode(head,2);
CreateNode(head,3);
CreateNode(head,4);
CreateNode(head,5);
CreateNode(head,6);
CreateNode(head,7);
ListNode *tem=head;

while (head!=NULL)
{
cout<<head->data<<endl;
head=head->next;
}
ListNode* re=ReverseList(tem);
cout<<"反转链表:"<<endl;
while (re!=NULL)
{
cout<<re->data<<endl;
re=re->next;
}
getchar();
return 0;
}

翻转句子中单词的顺序

     题目描述:翻转句子中单词的顺序,但单词内字符的顺序不变。句子中单词以空格符隔开。
     为简单起见,标点符号和普通字母一样处理。如:"I am a student."翻转成"student. a am I"。
             在上述题目中我看到网上好多答案在最后一个单词是没有经过翻转的,假如句子翻转后最后
     一个单词不是一个字母(也就是原句中的第一个单词不是一个字母),那么就会出现最后一个单词没有翻转的     情况.所以在最后还要进行一次单词翻转.
     如: “My name is fgreen”翻转后应该是”fgreen is name My”
    代码如下:
// ConsoleApplication1.cpp : 定义控制台应用程序的入口点。
//

#include "stdafx.h"
#include "iostream"
using namespace std;
void ReverseWord(char *str,int start,int end)
{
	char tem=' ';
	while (start<end)
	{
		 tem=str[start];	
		 str[start]=str[end];
		 str[end]=tem;
		 ++start;
		 --end;
	}
}
void ReverseSentence(char *str)
{ 
	if(str==NULL||*str=='\0')
		return;
	ReverseWord(str,0,strlen(str)-1);
	int i=0;
	int t=i;
	while (true)
	{
         if(str[i]==' ')
		 {   
			 ReverseWord(str,t,i-1); 
			 t=i+1;		
		 }
        ++i;
		if(str[i]=='\0')//最后一个单词也要翻转;
        { ReverseWord(str,t,i-1); 
		  break;
		}
	}
}
int main(int argc, char* argv[])
{
	char str[]="My name is fgreen";
	ReverseSentence(str);
	cout<<str<<endl;
	getchar();
	return 0;
}

 

为什么在16位机器中有符号整形的范围是-32768-32767?

        如果以最高位为符号位,二进制原码最大为0111  1111  1111  1111 =  215 – 1 =32767

                                             最小为1111  1111  1111  1111 =-215 – 1=-32767

此时0有两种表示方法,即正0和负0:0000  0000  0000  0000 = 1000  0000  0000  0000 = 0

所以,二进制原码表示时,范围是-32767~-0和0~32767,因为有两个零的存在,所以不同的数值个数一共只有2的16次方减1个,比16位二进制能够提供的2的16次方个编码少1个。

但是计算机中采用二进制补码存储数据,即正数编码不变,从0000  0000  0000  0000到0111  1111  1111  1111依旧表示0到32767,而负数需要把除符号位以后的部分取反加1,即-32767的补码为1000  0000  0000  0001。

到此,再来看原码的正0和负0:0000000000000000和1000000000000000,补码表示中,前者的补码还是0000000000000000,后者经过非符号位取反加1后,同样变成了0000  0000  0000  0000,也就是正0和负0在补码系统中的编码是一样的。但是,我们知道,16位二进制数可以表示2的16次方个编码,而在补码中零的编码只有一个,也就是补码中会比原码多一个编码出来,这个编码就是1000000000000000,因为任何一个原码都不可能在转成补码时变成1000  0000  0000  0000。所以,人为规定1000  0000  0000  0000这个补码编码为-32768。

所以,补码系统中,范围是-32768 – 32767。

因此,实际上,二进制的最小数确实是1111111111111111,只是二进制补码的最小值才是1000000000000000,而补码的1111111111111111是二进制值的-1。

重点是:

1.负数在计算机中是补码表示的!

2.正0和负0在计算机里面表示重叠了

3.人为规定负数里面1000 0000 0000 0000是负数里面最小的,-32768就是它。

不使用递归实现斐波那契数列

由于递归的写法效率太过低下,要求用非递归实现。

 

定义斐波那契数列如下:
{ 0                        n=0
{ 1                        n=1
{ f(n-1)+f(n-2)    n>1

// ConsoleApplication4.cpp : 定义控制台应用程序的入口点。
int fbnqsl(int n)
{
int tem = 0, h1 = 0, h2 = 1;
if (n < 0)
return -1;
if (1 == n || 0 == n)
return n;
for (int i = 2; i < n; ++i)
{
tem = h2;
h2 += h1;
h1 = tem;
}
return h1 + h2;
}
int main(int argc, char* argv[])
{
cout << fbnqsl(10);
getchar();
return 0;
}

 

第一个只出现一次的字符

在一个字符串中找到第一个只出现一次的字符。如输入aabbccddvllllsd,则输出v。听说此题目为2006年谷歌笔试.

C++代码如下:

// ConsoleApplication2.cpp : 定义控制台应用程序的入口点。
//

#include "stdafx.h"
#include "iostream"
using namespace std;

char FindFirstShow(char *data)
{
	int s[256]={0};
	char *str=data;
	while(*str!='\0')
	{   
        ++s[*str];
		++str;
	}
  for(char* i=data;*i!='\0';++i)
	  if(s[*i]==1)
	  {
		  return *i;
	  }
	  return NULL;	
}
int main(int argc, char* argv[])
{
	char *str="aabbccddvllllsd";
	cout<<"第一个不重复的字符是"<<FindFirstShow(str)<<endl;
	getchar();
	return 0;
}

 

 

在一个有序数组中查找2个数和为SUM的个数

输入一个递增排序的数组和一个数字sum,在数组中查找两个数,使得它们的和正好是s。
如果有多对数字的和等于sum,输出有多少对数满足。

// ConsoleApplication1.cpp : 定义控制台应用程序的入口点。
//

#include "stdafx.h"
#include "iostream"
using namespace std;
//查找函数;
int  FindSum(int *arr,int length,int sum)
{
	int *end=arr+length-1,*start=arr,number=0;

	while (start!=end)
	{  
		if(*start+*end<sum)
		   ++start;
		else if(*start+*end>sum)
			--end;
		else
		{  
			 ++number;
			 --end;//这里写++start也可以
		}
	}
	return number;
}
int main(int argc, char* argv[])
{

	int a[]={1,2,3,4,5};//测试数组
	cout<<"数组中和为7的有"<<FindSum(a,5,7)<<"对"<<endl;
	getchar();
	return 0;
}

链表中倒数第k个结点

输入一个链表,输出该链表的倒数第k个结点。设尾结点为倒数第1个结点,例如一个链表

从头到尾结点的值依次为1、2、3、4、5、6,则倒数第三个结点为4。

// ConsoleApplication3.cpp : 定义控制台应用程序的入口点。
//

#include "stdafx.h"
#include "iostream"
using namespace std;
struct ListNode
{
ListNode* next;
int data;
ListNode()
{
next=NULL;
data=0;

}
};

//找到链表倒数第K个点;
ListNode *FindLastNode(ListNode*head,int k)
{
if(head==NULL||k<1)
return NULL;
ListNode* ret=head;
for(int i=1;i!=k;++i)
{
if(head->next!=NULL)
head=head->next;
else
return NULL;

}
while (head->next!=NULL)
{
head=head->next;
ret=ret->next;
}
return ret;
}

//创建链表添加结点;
ListNode *CreateNode(ListNode* head,int data)
{
ListNode *fist=head;

while (head!=NULL)
{
if(head->next==NULL)
{
ListNode *tem=new ListNode();
tem->data=data;
head->next=tem;
break;
}
head=head->next;
}
return fist;
}
int main(int argc, char* argv[])
{
ListNode *head=new ListNode();
head=CreateNode(head,1);
head=CreateNode(head,2);
head=CreateNode(head,3);
head=CreateNode(head,4);
head=CreateNode(head,5);
head=CreateNode(head,6);
head=CreateNode(head,7);
ListNode *tem=head;
while (head!=NULL)
{
cout<<head->data<<endl;
head=head->next;
}
int k=4;
cout<<"倒数第"<<k<<"个是:"<<FindLastNode(tem,k)->data<<endl;
getchar();
return 0;
}