通过 Brew 安装 poppler

1
brew install poppler

分割成单页 PDF

1
pdfseparate target.pdf %d.pdf

Code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
class ViewController: UIViewController {
@IBOutlet weak var targetView: UIView!
override func viewDidLoad() {
super.viewDidLoad()

let shapeLayer = CAShapeLayer()
shapeLayer.strokeColor = UIColor.red.cgColor
shapeLayer.fillColor = nil
shapeLayer.lineDashPattern = [3, 2, 6, 4]
shapeLayer.frame = targetView.bounds
shapeLayer.lineWidth = 0.5
shapeLayer.lineCap = .square
shapeLayer.path = UIBezierPath(rect: targetView.bounds).cgPath
targetView.layer.addSublayer(shapeLayer)
}
}

Demo

https://oss.hdvsyu.com/IMG_0A2DFA41E0CB-1.jpeg

Show Code

1
2
3
4
5
6
func viewTapped (_ sender: UITapGestureRecognizer) {
guard let touchedView = sender.view else { return }
let point = sender.location(in: touchedView)
let convertedPoint = targetView.convert(point, from: touchedView)
print(targetView.layer.contains(convertedPoint))
}

Method One

1
2
3
4
5
6
7
8
let image: UIImage?
let scale = UIScreen.main.scale
UIGraphicsBeginImageContextWithOptions(view.bounds.size, true, scale)
if let context = UIGraphicsGetCurrentContext() {
view.layer.render(in: context)
}
image = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()

Method Two

1
2
3
4
let render = UIGraphicsImageRenderer(size: view.bounds.size)
let image = render.image { context in
view.layer.render(in: context.cgContext)
}

iOS 使用 UNUserNotificationCenter 类去管理通知相关的活动

请求通知

1
2
3
4
let center = UNUserNotificationCenter.current()
center.requestAuthorization(options: [.alert, .badge, .sound]) { (result, error) in
self.showAlertDialog(title: "request notification", message: result.description)
}

获取通知状态

1
2
3
4
5
6
7
8
9
10
11
12
13
let center = UNUserNotificationCenter.current()
center.getNotificationSettings { (settings) in
switch (settings.authorizationStatus) {
case .authorized:
self.showAlertDialog(title: "notification state", message: "authorized")
case .denied:
self.showAlertDialog(title: "notification state", message: "denied")
case .notDetermined:
self.showAlertDialog(title: "notification state", message: "not determined")
default:
self.showAlertDialog(title: "notification state", message: "unknown")
}
}

跳转到设置页面

1
2
3
4
5
let application = UIApplication.shared
let url = URL.init(string: UIApplication.openSettingsURLString)!
if (application.canOpenURL(url)) {
application.open(url, options: [:], completionHandler: nil)
}

使用 csrutil status 命令检查SIP的状态

1
2
3
csrutil status
// output
System Integrity Protection status: enabled.

如果你想启用或者禁用SIP,你必须在 Recovery OS 界面的命令行上使用 csrutil 操作

  1. 重启电脑,按住 Command + R 键进入 Recovery OS
  2. 在菜单项 Utilities 中打开 Terminal
  3. 在命令行中输入 csrutil enable 启用SIP或者 csrutil disable 禁用SIP,并重启电脑

Reference

[1] https://developer.apple.com/

Hello, World!

1
2
3
4
5
6
7
8
#import <Foundation/Foundation.h>

int main(int argc, const char * argv[]) {
@autoreleasepool {
NSLog(@"Hello, World!");
}
return 0;
}

Command Line Compile and Run

1
clang -fobjc-arc hello.m -o hello && ./hello

Instance and Method

Objective-C 采用特定的语法对类和实例应用方法: [ClassOrInstance method] 以及将特定的值作为方法参数 [ClassOrInstance argc: value]

Objective-C Class

一个程序在逻辑上分为3个部分:

  • @interface 部分
  • @implementation 部分
  • program 部分

其中,@interface 部分用于描述类和类的方法; @implementation 部分用于描述数据(类对象的实例变量存储的数据),并实现在接口中声明方法的实际代码;program 部分的程序代码实现了程序的预期目的

@interface 部分

按照约定,类名以大写字母开头,但这不是必须的

1
2
3
@interface NewClassName: ParentClassName
-(returnType) methodName: (parameterType) parameterName [argName]: (otherType) otherName
@end

实例方法以负号(-)开头,实例方法能够对类的实例执行一些操作,如设置值;

类方法以正号(+)开头,类方法是对类本身执行某些操作的方法,如创建类的新实例

@implementation 部分

1
2
3
4
5
6
@implementation NewClassName
{
memberDeclarations;
}
methodDefinitions;
@end

NewClassName 表示的名称与 @interface 部分的名称相同

@implementation 部分中的 methodDefinitions 部分包含在 @interface 部分指定的每个方法的代码中,每种方法的定义通过方法的类型(或者实例)、它的返回值和参数进行标识,并将方法的代码放入一对花括号中

program 部分

program 部分包含解决特定问题的代码,可以跨越多个文件,但必须存在仅有一个的 main 函数

新建一个类的实例:instance = [[ClassName alloc] init]

调用实例的方法:[instance method: value]

Data Type

Objective-C 的基本类型有:charintfloatdouble
Objective-C 的限定词有:longlong longshortunsignedsigned
id 类型:id 数据类型可存储任何类型的对象

Loop Structure

for 语句

1
2
3
for (init_experssion; loop_condition; loop_expression) {
program statement;
}

while 语句

1
2
3
while (expression) {
program statement
}

do 语句

1
2
3
do {
program statement
} while (expression)

breakcontinue

Choice Structure

if 语句

1
2
3
4
5
6
7
if (expression1) {
program statement
} else if (expression2) {
program statement
} else {
program statement
}

switch 语句

1
2
3
4
5
6
7
8
9
10
11
switch (expression) {
case value1:
program statement
break;
case value2:
program statement
break;
default:
program statement
break;
}

conditional 运算符

条件运算符也叫三目运算符

1
condition ? expression1 : expression2

存取方法

在接口部分使用 @property 指令标识属性,在实现部分使用 @synthesize 指令标识属性,这样编译器就会自动生成存取方法,无需自己编写 gettersetter 方法了;当然,你也可以不在实现部分使用 @synthesize 指令,那么编译器会生成以下划线(_)开头的实例变量名

Root Class

1
2
3
@interface ClassName: NSObject
statement
@end

NSObject 是任何类的根类

使用 @try 处理异常

1
2
3
4
5
6
@try {
statement
}
@catch (NSeception *exception) {
statement
}

override init method

1
2
3
4
5
6
7
8
- (instancetype) init
{
self = [super init];
if (self) {
// initial statement
}
return self;
}

instancetype 是关键字

Category

将类的定义模块化到相关方法的分类中

1
2
3
@interface ClassName(CategoryName)
category method code
@end

Class’s Extension

类的扩展是一个未命名的分类

1
2
3
4
5
@interface ClassName ()
{
property code
}
method code

Protocol

使用 @protocol 指令定义一个协议

定义:

1
2
3
@protocol ProtocolName
protocol interface
@end

使用:

1
2
3
4
5
@interface InterfaceName: ParentClassName <ProtocolName, otherProtocolName>
method
@optional
optional method
@end

对于没有实现的协议方法,使用 @optional 指令修饰,该指令之后列出的所有方法都是可选的

Conditional Compile

1
2
3
4
5
6
7
#ifdef condition
# define var value
#elif condition
# define var value
#else
# define var value
#endif

Cocoa

术语 Cocoa 总的来说指的是 Foundation 框架、Application Kit 框架和名为 Core Data 的第三方框架

Cocoa Touch

术语 Cocoa Touch 是指 FoundationCore DataUIKit 框架

Reference

[1] Kochan S G . Objective-C程序设计[M]. 电子工业出版社, 2012.

如果某个数 K 的平方乘以 N 以后,结果的末尾几位数等于 K,那么就称这个数为“N-自守数”。例如 3×$92^2$
=25392,而 25392 的末尾两位正好是 92,所以 92 是一个 3-自守数。

本题就请你编写程序判断一个给定的数字是否关于某个 N 是 N-自守数。

输入格式:

输入在第一行中给出正整数 M(≤20),随后一行给出 M 个待检测的、不超过 1000 的正整数。

输出格式:

对每个需要检测的数字,如果它是 N-自守数就在一行中输出最小的 N 和 N$K^2$ 的值,以一个空格隔开;否则输出 No。注意题目保证 N<10。

输入样例:

3
92 5 233

输出样例:

3 25392
1 25
No

分析:

计算一个数num的平方乘以一个1到9的数,如果算出来的数末尾某几位数等于num,则它就是N-自守数

Code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
#include <iostream>
#include <string>

using namespace std;

void is_self_guard_and_output(int num) {
for (int var1 = 1; var1 < 10; var1++) {
string s = to_string(var1 * num * num);
if (to_string(num).size() > s.size()) continue;
if (s.substr(s.size() - to_string(num).size(), to_string(num).size()) == to_string(num)) {
cout << var1 << " " << s << endl;
return;
}
}
cout << "No\n";
}

int main() {
int m = 0, num = 0;
cin >> m;
for (int var1 = 0; var1 < m; var1++) {
cin >> num;
is_self_guard_and_output(num);
}
return 0;
}

给定两个字符串 A 和 B,本题要求你输出 A+B,即两个字符串的并集。要求先输出 A,再输出 B,但重复的字符必须被剔除

输入格式:

输入在两行中分别给出 A 和 B,均为长度不超过$10^6$的、由可见 ASCII 字符 (即码值为32~126)和空格组成的、由回车标识结束的非空字符串。

输出格式:

在一行中输出题面要求的 A 和 B 的和。

输入样例:

This is a sample test
to show you_How it works

输出样例:

This ampletowyu_Hrk

分析:

用visit表示某个字符是否已经输出过了,循环遍历字符串,将未输出的字符输出,且将visit中对应的位置置1

Code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include <iostream>
#include <string>

using namespace std;

int main() {
string a, b;
getline(cin, a);
getline(cin, b);
int visit[200] = {0};
for (auto it : a + b) {
if (visit[it] == 0) cout << it;
visit[it] = 1;
}
return 0;
}

月饼是久负盛名的中国传统糕点之一,自唐朝以来,已经发展出几百品种。

若想评比出一种“最好吃”的月饼,那势必在吃货界引发一场腥风血雨…… 在这里我们用数字说话,给出全国各地各种月饼的销量,要求你从中找出销量冠军,认定为最好吃的月饼。

输入格式:

输入首先给出两个正整数 N(≤1000)和 M(≤100),分别为月饼的种类数(于是默认月饼种类从 1 到 N 编号)和参与统计的城市数量。

接下来 M 行,每行给出 N 个非负整数(均不超过 1 百万),其中第 i 个整数为第 i 种月饼的销量(块)。数字间以空格分隔。

输出格式:

在第一行中输出最大销量,第二行输出销量最大的月饼的种类编号。如果冠军不唯一,则按编号递增顺序输出并列冠军。数字间以 1 个空格分隔,行首尾不得有多余空格。

输入样例:

5 3
1001 992 0 233 6
8 0 2018 0 2008
36 18 0 1024 4

输出样例:

2018
3 5

大意及分析:

累加第i种月饼的销量,使用max_element函数求出vector中的最大值,遍历vector,若当前位置的值与max_element函数求出的最大值相等,则是冠军,格式化输出

Code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
#include <iostream>
#include <algorithm>
#include <vector>

using namespace std;

int main() {
unsigned int n = 0, m = 0, t = 0, flag = 0;
cin >> n >> m;
vector<unsigned int> v(n+1, 0);
for (int var1 = 0; var1 < m; var1++) {
for (int var2 = 1; var2 <= n; var2++) {
cin >> t;
v[var2] += t;
}
}
unsigned int max = *max_element(v.begin(), v.end());
cout << max << endl;
for (unsigned int var1 = 1; var1 <= n; var1++) {
if (v[var1] == max) {
if (flag) cout << " ";
cout << var1;
flag = 1;
}
}
return 0;
}
0%