0%

作者:Prateek Gianchandani

译者:吴发伟

原文网址

版权声明:自由转载-非商用-保持署名

本文我们将看看应用在本地存储数据有哪些方法以及这些不同方法的安全性。
我们将会在一个demo上这些这些测试,你可以从我的github账号上下载这个例子程序。对于CoreData的例子,你可以从下载例子程序。本例有一个不同点就是我们将会在模拟器上运行这些应用,而不是在设备上运行。这样做的目的是为了证明在前面文章中的操作都可以通过Xcode来把这些应用运行在模拟器上。当然,你也可以把这应用安装到设备上。

Read more »

HDOJ1007 Quoit Design

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
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
using namespace std;
const double INF = 1e20;
const int N = 100005;

struct Point
{
double x;
double y;
}point[N];
int n;
int tmpt[N];

bool cmpxy(const Point& a, const Point& b)
{
if(a.x != b.x)
return a.x < b.x;
return a.y < b.y;
}

bool cmpy(const int& a, const int& b)
{
return point[a].y < point[b].y;
}

double min(double a, double b)
{
return a < b ? a : b;
}

double dis(int i, int j)
{
return sqrt((point[i].x-point[j].x)*(point[i].x-point[j].x)
+ (point[i].y-point[j].y)*(point[i].y-point[j].y));
}

double Closest_Pair(int left, int right)
{
double d = INF;
if(left==right)
return d;
if(left + 1 == right)
return dis(left, right);
int mid = (left+right)>>1;
double d1 = Closest_Pair(left,mid);
double d2 = Closest_Pair(mid+1,right);
d = min(d1,d2);
int i,j,k=0;
//分离出宽度为d的区间
for(i = left; i <= right; i++)
{
if(fabs(point[mid].x-point[i].x) <= d)
tmpt[k++] = i;
}
sort(tmpt,tmpt+k,cmpy);
//线性扫描
for(i = 0; i < k; i++)
{
for(j = i+1; j < k && point[tmpt[j]].y-point[tmpt[i]].y<d; j++)
{
double d3 = dis(tmpt[i],tmpt[j]);
if(d > d3)
d = d3;
}
}
return d;
}

int main(){

freopen("/Users/RichardLu/Code/C++/Algorithm/largeNumberSort/largeNumberSort/largeNumberSort/testcase.in", "r", stdin);
freopen("/Users/RichardLu/Code/C++/Algorithm/largeNumberSort/largeNumberSort/largeNumberSort/testcase.out", "w", stdout);

while(true)
{
scanf("%d",&n);
if(n==0)
break;
for(int i = 0; i < n; i++)
scanf("%lf %lf",&point[i].x,&point[i].y);
sort(point,point+n,cmpxy);
printf("%.2lf\n",Closest_Pair(0,n-1)/2);
}
fclose(stdin);
fclose(stdout);

return 0;
}

ACMCoder2009 数列求和

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 <iomanip>
#include <math.h>

using namespace std;



int main(){

freopen("testcase.in", "r", stdin);
freopen("testcase.out", "w", stdout);
double n,m;
while (cin>>n>>m&&n>=0&&m>=0) {
m-=1;
double sum = n;
while (m--) {
n = sqrt((double)n);
sum += n;
}
cout<<setiosflags(ios::fixed)<<setprecision(2)<<(double)sum<<endl;
}
fclose(stdin);
fclose(stdout);

return 0;
}

ACMCoder2010 水仙花数

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
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
#include <iostream>
#include <map>
#include <string>
#include <math.h>

using namespace std;



int main(){

freopen("testcase.in", "r", stdin);
freopen("testcase.out", "w", stdout);

int a,b;
while (cin>>a>>b) {
bool flag = false;
for (int i = a; i <= b; ++i) {

int temp = i,result=0;
while (temp) {
result+=pow(temp%10, 3);
temp = temp / 10;
}
if (result == i) {
if (!flag) {
cout<<result;
}else{
cout<<" "<<result;
}
flag = true;
}
}
if (!flag) {
cout<<"no"<<endl;
}else
cout<<endl;
}


fclose(stdin);
fclose(stdout);

return 0;
}

1
2
3
4
5
6
7
8
9
10
11
12
13
14
#include <iostream>
using namespace std;
int main(){

freopen("testcase.in", "r", stdin);
freopen("testcase.out", "w", stdout);



fclose(stdin);
fclose(stdout);

return 0;
}

HDOJ1002 A + B Problem II

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
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
//
// main.cpp
// largeNumberSort
//
// Created by Richard on 9/9/15.
// Copyright (c) 2015 luxin. All rights reserved.
//


#include <string>
#include <iostream>

#define MAX_NUM 2000
using namespace std;

int add1[MAX_NUM],add2[MAX_NUM],res[MAX_NUM],tmp;
char tmp1[MAX_NUM],tmp2[MAX_NUM];
int N;

int main() {
cin>>N;
int count = 0;
tmp = 0;
while (count < N) {

memset(add1,0,sizeof(add1));
memset(add2,0,sizeof(add2));
memset(res,0,sizeof(res));
memset(tmp1,0,sizeof(tmp1));
memset(tmp2,0,sizeof(tmp2));

cin>>tmp1>>tmp2;

int len1 = static_cast<int>(strlen(tmp1));
int len2 = static_cast<int>(strlen(tmp2));

for (int i = len1-1,k = 0; i >= 0; --i,++k) {
add1[k] = tmp1[i] - '0';

}

for (int i = len2-1,k = 0; i >= 0; --i,++k) {
add2[k] = tmp2[i] - '0';

}

if (len1 >= len2) {
for (int i = 0; i <= len1; ++i) {
res[i] = (add1[i] + add2[i] + tmp) % 10;
tmp = (add1[i] + add2[i] + tmp) / 10;
}

}else if (len1 < len2) {
for (int i = 0; i <= len2; ++i) {
res[i] = (add1[i] + add2[i] + tmp) % 10;
tmp = (add1[i] + add2[i] + tmp) / 10;
}
}

int len = 0;
if (len1 >= len2) len = len1;
else len = len2;
//TODO: priint it
cout<<"Case "<<(count+1)<<":\n"<<tmp1<<" + "<<tmp2<<" = ";
if (res[len] != 0) cout<<res[len];
for (int i = len-1; i >= 0; --i) {
cout<<res[i];
}
cout<<"\n";
if (count != N-1) {
cout<<"\n";
}
count++;
}

}

HDOJ1003 Max Sum

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
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
#include <stdio.h>
#define MAX_LEN 100000UL

int max_subsq(int *array, unsigned int size, unsigned int *start, unsigned int *end)
{
int max_sum = -2000, sum = 0;
int curstart = *start = 0;
unsigned int i;

for (i = 0; i < size; i++){
if (sum < 0){
sum = array[i];//丢弃之前的子串
curstart = i; //将当前位置作为新的起始位置
} else {
sum += array[i];
}

if (sum > max_sum){
max_sum = sum;
*start = curstart;
*end = i;
}
}

return max_sum;
}

int main(void)
{
int A[MAX_LEN];
int i = 0, round, n, max;
unsigned int start, end;
freopen("/Users/RichardLu/Code/C++/Algorithm/largeNumberSort/largeNumberSort/largeNumberSort/testcase.in", "r", stdin);
freopen("/Users/RichardLu/Code/C++/Algorithm/largeNumberSort/largeNumberSort/largeNumberSort/testcase.out", "w", stdout);
scanf("%d", &round);

while (i++ < round)
{
scanf("%d", &n);
for (int j = 0; j < n; j++){
scanf("%d", &A[j]);
}
max = max_subsq(A, n, &start, &end);

printf("Case %d:\n", i);
printf("%d %d %d\n", max, start + 1, end + 1);

if (i != round) printf("\n");
}
fclose(stdin);
fclose(stdout);
return 0;
}

HDOJ1004 Let the Balloon Rise

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
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
//
// main.cpp
// largeNumberSort
//
// Created by richard on 9/9/15.
// Copyright (c) 2015 luxin. All rights reserved.
//
#include <iostream>
#include <map>
#include <string>

using namespace std;



int main(){

// freopen("/Users/RichardLu/Code/C++/Algorithm/largeNumberSort/largeNumberSort/largeNumberSort/testcase.in", "r", stdin);
// freopen("/Users/RichardLu/Code/C++/Algorithm/largeNumberSort/largeNumberSort/largeNumberSort/testcase.out", "w", stdout);


map<string, int> string_int_map;
string a;
int count = 0;
while (cin >> count&&count) {
string_int_map.clear();
while (count--) {
cin >> a;
string_int_map[a]++;
}

string most = "";
for (map<string, int>::iterator it = string_int_map.begin(); it != string_int_map.end(); ++it) {
if (it->second > string_int_map[most]) {
most = it->first;
}
}
cout <<most<<endl;
}

// fclose(stdin);
// fclose(stdout);

return 0;
}

iOS 9 系列教程 在UIKit中实践面向协议编程

原文在此 翻译 by skyfly.xyz

在 WWDC2015 上那次关于 Swift 面向协议编程的发人深省的演讲后(是的,“很难啃”的那部分)似乎每个人都在谈论协议扩展这个令人兴奋的新的语言特性,同时它也使每个人都感到过困惑。

Read more »