C语言第四次作业

发布在 C语言

第一题

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
#include <stdio.h>;
void swap(int *a, int *b);
int main (void)
{
int number[10];
int i, j;//循环变量
while (scanf("%d", &amp;number[0]) != EOF)
{
for (i = 1 ; i &lt; 10 ; i++)
scanf("%d", &amp;number[i]);
for (i = 0 ; i &lt; 9 ; i++)
{
for (j = 0 ; j &lt;= 8 - i ; j++)
{
if (number[j] &gt; number[j+1])
swap(&amp;number[j], &amp;number[j+1]);
}
}
for (i = 0 ; i &lt; 10 ; i++)
{
if (i != 0) printf("-&gt;");
printf("%d", number[i]);
}
putchar('\n');
}
return 0;
}
void swap(int *a, int *b)
{
int temp;
temp = *a;
*a = *b;
*b = temp;
return;
}

第二题

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include <stdio.h>;
int main (void)
{
long number;
char currentByte;
int count;
while (scanf("%ld", &amp;number) != EOF)
{
for (count = 7 ; count &gt;= 0 ; count--)
{
currentByte = (((0xf &lt;&lt; (count * 4)) &amp; number) &gt;&gt; (count * 4)) &amp; 0xf;
printf("%hd(%c) ", currentByte, currentByte);
}
putchar('\n');
}
return 0;
}

第三题

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
#include <stdio.h>;
void swapArray(int a[], int n);
void swap(int *a, int *b);
int main (void)
{
int number[100];
int totalNumber, individualNumber;
int count, i;//循环变量
scanf("%d", &amp;totalNumber);
for (count = 0 ; count &lt; totalNumber; count++)
{
scanf("%d", &amp;individualNumber);
for (i = 0 ; i &lt; individualNumber ; i++)
scanf("%d", &amp;number[i]);
swapArray(number, individualNumber);
for (i = 0 ; i &lt; individualNumber ; i++)
{
if (i != 0) putchar(' ');
printf("%d", number[i]);
}
putchar('\n');
}
return 0;
}
void swapArray(int a[], int n)
{
int startIndex = 0;
int endIndex = n - 1;
while (startIndex &lt;= endIndex)
{
swap(&amp;a[startIndex], &amp;a[endIndex]);
startIndex++;
endIndex--;
}
return;
}
void swap(int *a, int *b)
{
int temp;
temp = *a;
*a = *b;
*b = temp;
return;
}

第四题

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
#include <stdio.h>;
void swap(int *a, int *b);
void moveArray(int a[], int n, int m);//轮移数组,其中n为总个数,m为轮移次数
int main (void)
{
int number[22];
int individualNumber, middleNumber;
int i;//循环变量
while ((scanf("%d", &amp;individualNumber), individualNumber) != 0)
{
scanf("%d", &amp;middleNumber);
for (i = 0 ; i &lt; individualNumber ; i++)
scanf("%d", &amp;number[i]);
moveArray(number, individualNumber, middleNumber);
for (i = 0 ; i &lt; individualNumber ; i++)
{
if (i != 0) putchar(' ');
printf("%d", number[i]);
}
putchar('\n');
}
return 0;
}
void moveArray(int a[], int n, int m)
{
if (m == 0)
{
return;
}
else
{
int temp, count;
temp = a[0];
for (count = 0 ; count &lt; n - 1 ; count++)
a[count] = a[count+1];
a[n-1] = temp;
moveArray(a, n, --m);
}
}
void swap(int *a, int *b)
{
int temp;
temp = *a;
*a = *b;
*b = temp;
return;
}

评论和共享

iOS学习笔记(一)

发布在 iOS开发

最近开始了自己的第一个项目,到目前为止已经写完了基本的界面,将开发过程中遇到的一些问题列举如下:

自定义cell分割线

首先设置 tableView.separatorStyle = UITableViewCellSeparatorStyleNone;

然后在自定义的cell的实现文件内实现如下的方法

1
2
3
4
5
6
7
8
9
10
11
12
- (void)drawSeparator{
_separatorlineLayer = [CAShapeLayer layer];
CGMutablePathRef separatorShapePath = CGPathCreateMutable();
[_separatorLayer setFillColor:[UIColor clearColor].CGColor];
[_separatorLayer setStrokeColor:[UIColor headlineColor].CGColor];
_separatorLayer.lineWidth = 0.5f;
CGPathMoveToPoint(separatorShapePath, NULL, 0.0f, 0.0f);
CGPathAddLineToPoint(separatorShapePath, NULL, WIDTH, 0.0f);
[_headlineLayer setPath:separatorShapePath];
CGPathRelease(separatorShapePath);
[self.contentView.layer addSublayer:_separatorLineLayer];
}

其中,_separatorLayerCAShapeLayer的实例,setStrokeColor方法设定了线的颜色,CGPathMoveToPointCGPathAddLineToPoint设定了线的起始点。

同理,可以类似地实现画点或者更为复杂形状的效果。

iOS8之后cell的动态高度计算

首先使用autolayout布局,且确保cell的contentView至少top和bottom都和cell内部的View建立了约束。之后添加如下代码

1
2
tableView.estimatedRowHeight = 60.0f;
tableView.rowHeight = UITableViewAutomaticDimension;

需要注意的是,必须要为tebleView的estimiatedRowHeight属性设定值。

返回键盘的高度

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
- (void)viewDidLoad{
[super viewDidLoad];
//注册通知
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillShow:) name:UIKeyboardWillShowNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillHide:) name:UIKeyboardWillHideNotification object:nil];
}

- (void)dealloc {
//移除通知
[[NSNotificationCenter defaultCenter]removeObserver:self];
}

- (void)keyboardWillShow:(NSNotification *)notification{
//键盘弹出时调用
NSDictionary * userInfo = [notification userInfo];
NSValue * frameValue = [userInfo objectForKey:UIKeyboardFrameEndUserInfoKey];
CGRect keyboardRect = [frameValue CGRectValue];
NSInteger height = keyboardRect.size.height;
}

- (void)keyboardWillHide:(NSNotification *)notification{
//键盘隐藏时调用
}

height中储存的即为当前键盘的高度

小心block导致的retain cycle

如果在block中需要访问本类的实例变量,需要使用

__weak UIViewController * weakSelf = self;

self持有了block,而block通过捕获self来访问实例变量,导致保留换的产生,通过__weak打破保留环。

评论和共享

C语言第三次作业

发布在 C语言

第一题 C语言成绩


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
#include<stdio.h>
#include<string.h>
void swap(int *score_a, int *score_b, char *name_a, char *name_b);
int main (void)
{
int number, count;
while (scanf("%d", &number) != EOF)
{
putchar('\n');
int scores[number];
char names[number][81];
for (count = 0 ; count < number ; ++count)
scanf("%s%d", names[count], &scores[count]);
int i, j;
for (i = 0 ; i < number ; ++i)
for (j = i ; j < number ; ++j)
if (scores[i] < scores[j])
swap(&scores[i], &scores[j], &names[i][0], &names[j][0]);
for (count = 0 ; count < number ; ++count)
printf("%s %d\n", names[count], scores[count]);
putchar('\n');
}
return 0;
}

void swap(int *score_a, int *score_b, char *name_a, char *name_b)
{
int t;
char str_t[81];
t = *score_a;
*score_a = *score_b;
*score_b = t;
strcpy(str_t, name_a);
strcpy(name_a, name_b);
strcpy(name_b, str_t);
return;
}

第二题 冒泡排序


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
#include <stdio.h>
int main(void)
{
int sequence[10];
int count;
int i, j;
int temp;
while (scanf("%d", &sequence[0]) != EOF)
{
for (count = 1 ; count < 10 ; ++count)
scanf("%d", &sequence[count]);
for (i = 0 ; i < 10 ; ++i)
{
for (j = 0 ; j < 8 - i ; ++j)
{
if (sequence[j] > sequence[j+1])
{
temp = sequence[j];
sequence[j] = sequence[j+1];
sequence[j+1] = temp;
}
}
}
for(count = 0 ; count < 10 ; ++count)
printf("%d ", sequence[count]);
putchar('\n');
}
return 0;
}

第三题 n人报数(此题可查找“约瑟夫问题”)


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
#include <stdio.h>
int main (void)
{
int number,count;
int current;
int i;
while (scanf("%d", &number) != EOF)
{
int people[10000];
for (i = 0 ; i < number ; ++i)
people[i] = 1;
count = number;
current = 0;
i = 0;
while (count > 1)
{
if (i >= number)
i = 0;
if (people[i] == 1)
current += 1;
if (count == 1)
break;
if (current == 3)
{
people[i] = 0;
count -= 1;
current = 0;
}
i++;
}
for (i = 0 ; i < number ; ++i)
if (people[i] == 1)
{
printf("%d\n", i + 1);
break;
}
}
return 0;
}

第四题 定义宏交换参数


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include <stdio.h>
#define Swap(x,y) {x = x + y; y = x - y; x = x - y;}

int main (void)
{
int a, b;
int count = 0;
while (scanf("%d%d", &a, &b) != EOF)
{
if (count)
putchar('\n');
count += 1;
printf("Case %d:\n", count);
printf("Before Swap:a=%d b=%d\n", a, b);
Swap(a,b);
printf("After Swap:a=%d b=%d\n", a, b);

}
return 0;
}

第五题 字符串复制


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
#include <stdio.h>
void mycpy (char s[], char t[], int n);
int main (void)
{
int number;
char sentence[1001];
char result[1001];
int n;
scanf("%d", &number);
getchar();//读取\n
int count;
for (count = 0 ; count < number ; ++count)
{
fgets(sentence, 1001, stdin);
scanf("%d", &n);
getchar();//读取\n
mycpy(result, sentence, n);
printf("%s\n", result);
}
return 0;
}
void mycpy (char s[], char t[], int n)
{
int i;
for (i = 0 ; i < n ; ++i)
{
s[i] = t[i];
}
s[n] = '\0';
return;
}

第六题 统计


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
#include <stdio.h>
int main (void)
{
char currentChar;
int numberCount[10] = {0};
int englishCharCount = 0;
int otherCount = 0;
int count;
while ((currentChar = getchar()) != EOF)
{
switch (currentChar)
{
case '0': numberCount[0] += 1 ; break;
case '1': numberCount[1] += 1 ; break;
case '2': numberCount[2] += 1 ; break;
case '3': numberCount[3] += 1 ; break;
case '4': numberCount[4] += 1 ; break;
case '5': numberCount[5] += 1 ; break;
case '6': numberCount[6] += 1 ; break;
case '7': numberCount[7] += 1 ; break;
case '8': numberCount[8] += 1 ; break;
case '9': numberCount[9] += 1 ; break;
default: break;
}
if ((currentChar >= 'A' && currentChar <= 'Z') || (currentChar >= 'a' && currentChar <= 'z'))
englishCharCount += 1;
else if (!(currentChar >= '0' && currentChar <= '9'))
otherCount += 1;
}
for (count = 0 ; count < 10 ; ++count)
printf("Number %d: %d\n", count, numberCount[count]);
printf("characters: %d\n", englishCharCount);
printf("other: %d\n", otherCount);
return 0;
}

第七题 选择排序


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
#include <stdio.h>
int main (void)
{
int sequence[20];
int number;
int count;
int i, j;
int temp;
while (scanf("%d", &number), number != 0)
{
for (count = 0 ; count < number ; ++count)
scanf("%d", &sequence[count]);
for (i = 0 ; i < number ; ++i)
for (j = i ; j < number ; ++j)
if (sequence[i] > sequence[j])
{
temp = sequence[i];
sequence[i] = sequence[j];
sequence[j] = temp;
}
for (count = 0 ; count < number ; ++count)
{
if (count)
putchar(' ');
printf("%d", sequence[count]);
}
putchar('\n');
}
return 0;
}

评论和共享

  • 第 1 页 共 1 页
作者的图片

码龙黑曜

iOS开发者/计算机科学/兽人控


华中科技大学 本科在读


Wuhan, China