【Objective-C】类与结构体的区别

  • 只能在类里面写方法,不能在结构体里面写方法
  • 类——对象,结构体——值
  • 类——引用类型
    • 位于栈上的指针(引用)
    • 位于堆上的实体对象
  • 结构体——值类型
    • 实例直接位于栈中
  • 拷贝行为:
    • classname *a = b; a和b都是指针(指针存储在栈上),都指向同一个对象(对象存储在堆中),对象的值改变,a和b同时改变
    • structname a = b; 进行的是值拷贝(复制值后存储在栈上),如果改变b的值,a的值不会跟着改变
  • 传参行为:
    • 传参的本质上就是拷贝,因为对象传入的是指针,所以值改变的函数调用完毕之后,对象的值会改变,而结构体的值不会改变(因为结构体是值传递)
    • 在main函数调用子函数的时候,系统会为子函数建立一个栈,对象传参后存放的是引用(指针),结构体传参后存储的是结构体的值的拷贝

【Objective-C】栈(stack)和堆(heap)的区别

栈(stack)和堆(heap)的区别:

  • 栈:存储值类型(有时候翻译成“堆栈”)
    • 无ARC(自动引用计数)负担,由系统自动管理,以执行函数为单位(一个函数一个栈)
    • 空间大小编译时决定(根据参数和局部变量可以确定)
    • 函数执行时,系统自动分配一个栈
    • 函数执行结束,系统会立即回收stack
    • 函数之间通过拷贝值传递
    • 具有局限性,大小有限额,超出会stack overflow(栈溢出)(一般是超大递归、死循环情况)

  • 堆:存储引用类型对象
    • 分配由程序员手动请求([a alloc])(c语言里面的malloc)
    • 释放有两种方式,可以手工,也可以ARC机制自动释放
    • 函数之间通过拷贝引用(指针)传递
    • 具有全局性,总体大小无限制(受限于系统内存整体大小)

【Objective-C】java中的interface与Objective-C中的interface的区别

 

  • java中的interface
    • interface叫做接口,是一种特殊的抽象类
    • 一个接口中,所有方法为公开、抽象方法;所有的属性都是公开、静态、常量。
    • 一个类只能继承一个类,但是能实现多个接口,这样可以实现变相的多继承
    • 接口和接口之间也可以是继承关系,而且允许接口之间实现多继承
    • 类必须实现接口中的方法,否则它是一个抽象类

  • Objective-C中的@interface
    • Objective-C里面的@interface与java里面的interface不一样,就是写在头文件里面的,作为类的一个外界可以调用的函数的声明

    • 最好将Objective-C中的interface理解为“类的声明部分”,protocol理解为“正式协议”,protocol相当于java中的interface
    • interface和implementation共同代表一个类,两者的组合相当于java中的class

1079. Total Sales of Supply Chain (25)-PAT甲级真题(dfs,bfs,树的遍历)

A supply chain is a network of retailers(零售商), distributors(经销商), and suppliers(供应商)– everyone involved in moving a product from supplier to customer.

Starting from one root supplier, everyone on the chain buys products from one’s supplier in a price P and sell or distribute them in a price that is r% higher than P. Only the retailers will face the customers. It is assumed that each member in the supply chain has exactly one supplier except the root supplier, and there is no supply cycle.

Now given a supply chain, you are supposed to tell the total sales from all the retailers.

Input Specification:

Each input file contains one test case. For each case, the first line contains three positive numbers: N (<=105), the total number of the members in the supply chain (and hence their ID’s are numbered from 0 to N-1, and the root supplier’s ID is 0); P, the unit price given by the root supplier; and r, the percentage rate of price increment for each distributor or retailer. Then N lines follow, each describes a distributor or retailer in the following format:

Ki ID[1] ID[2] … ID[Ki]

where in the i-th line, Ki is the total number of distributors or retailers who receive products from supplier i, and is then followed by the ID’s of these distributors or retailers. Kj being 0 means that the j-th member is a retailer, then instead the total amount of the product will be given after Kj. All the numbers in a line are separated by a space.

Output Specification:

For each test case, print in one line the total sales we can expect from all the retailers, accurate up to 1 decimal place. It is guaranteed that the number will not exceed 1010.

Sample Input:
10 1.80 1.00
3 2 3 5
1 9
1 4
1 7
0 7
2 6 1
1 8
0 9
0 4
0 3
Sample Output:
42.4

题目大意:给一棵树,在树根出货物的价格为p,然后从根结点开始每往下走一层,该层的货物价格将会在父亲结点的价格上增加r%,给出每个叶结点的货物量,求他们的价格之和
分析:树的遍历,可以采用dfs或者bfs两种方法。
采用dfs,建立结构体数组保存每一个结点的孩子结点的下标,如果没有孩子结点,就保存这个叶子结点的data(销售的量)。深度优先遍历的递归出口,即当前下标的结点没有孩子结点的时候,就把ans += data(货物量)* pow(1 + r, depth)计算货物量*价格引起的涨幅百分比。如果有孩子结点,就dfs深度优先遍历每一个孩子结点,并且在当前depth层数的基础上+1。最后输出ans * p(销售价格),即总价格

 

iOS开发之touchesCancelled

touchesCancelled:

当我们正在触摸屏幕的时候,如果出现了低电量、有电话呼入等等这样的系统事件时候,低电量或者电话的窗口会置为前台,这个时候touchesCancelled方法就会被调用。

因为在软件运行过程中不可避免的会发生由iOS系统发出的一些事件,导致触摸事件的中断,所以一般建议要实现touchesCancelled这个方法,一般情况下直接调用touchesEnd即可。

touchesCancelled:Sent to the responder when a system event (such as a low-memory warning) cancels a touch event.

1012. The Best Rank (25)-PAT甲级真题

To evaluate the performance of our first year CS majored students, we consider their grades of three courses only: C – C Programming Language, M – Mathematics (Calculus or Linear Algebra), and E – English. At the mean time, we encourage students by emphasizing on their best ranks — that is, among the four ranks with respect to the three courses and the average grade, we print the best rank for each student.

For example, The grades of C, M, E and A – Average of 4 students are given as the following:

StudentID C M E A
310101 98 85 88 90
310102 70 95 88 84
310103 82 87 94 88
310104 91 91 91 91
Then the best ranks for all the students are No.1 since the 1st one has done the best in C Programming Language, while the 2nd one in Mathematics, the 3rd one in English, and the last one in average.

Input

Each input file contains one test case. Each case starts with a line containing 2 numbers N and M (<=2000), which are the total number of students, and the number of students who would check their ranks, respectively. Then N lines follow, each contains a student ID which is a string of 6 digits, followed by the three integer grades (in the range of [0, 100]) of that student in the order of C, M and E. Then there are M lines, each containing a student ID.

Output

For each of the M students, print in one line the best rank for him/her, and the symbol of the corresponding rank, separated by a space.

The priorities of the ranking methods are ordered as A > C > M > E. Hence if there are two or more ways for a student to obtain the same best rank, output the one with the highest priority.

If a student is not on the grading list, simply output “N/A”.

Sample Input
5 6
310101 98 85 88
310102 70 95 88
310103 82 87 94
310104 91 91 91
310105 85 90 90
310101
310102
310103
310104
310105
999999
Sample Output
1 C
1 M
1 E
1 A
3 A
N/A

题目大意:现已知n个考生的3门分数,平均分可以按照这三门算出来。然后分别对这四个分数从高到低排序,这样对每个考生来说有4个排名。k个查询,对于每一个学生id,输出当前id学生的最好的排名和它对应的分数,如果名次相同,按照A>C>M>E的顺序输出~如果当前id不存在,输出N/A~
分析:
1、用结构体存储学生的id、四门成绩、四门排名、最好的排名的对应的科目下标~
2、排名并列应该1、1、3、4、5,而不是1、1、2、3、4,否则会有一个测试点不过
3、平均分是四舍五入的,所以需要按照+0.5后取整,保证是四舍五入的(听说不四舍五入也能通过…)
4、存储的时候就按照ACME的顺序存储可以简化程序逻辑~
5、用exist数组保存当前id是否存在,这个id对应的stu结构体的下标是多少。用i+1可以保证为0的都是不存在的可以直接输出N/A,其余不为0的保存的值是对应的结构体index + 1的值~