C语言第一次作业

发布在 C语言

2016年4月1日C语言作业答案及遇到的问题

题目及解答

2.一年中的第几天——改编自习题4.3


原题链接

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
#include<stdio.h>;
int main (void)
{
int year, month, day;
int count;
int isLeap;
while (scanf("%d%d%d", &year, &month, &day) == 3)
{
count = 0;
isLeap = (year % 4 == 0 && year % 100 != 0) || (year % 100 == 0 && year % 400 == 0);
count += day;
month -= 1;
switch (month)
{
case 12: count += 31; month -= 1;
case 11: count += 30; month -= 1;
case 10: count += 31; month -= 1;
case 9: count += 30; month -= 1;
case 8: count += 31; month -= 1;
case 7: count += 31; month -= 1;
case 6: count += 30; month -= 1;
case 5: count += 31; month -= 1;
case 4: count += 30; month -= 1;
case 3: count += 31; month -= 1;
case 2: count += 28 + isLeap; month -= 1;
case 1: count += 31; month -= 1;
default: break;
}
printf("该日期是这一年中的第%d天\n", count);
}
return 0;
}

3.税金额度计算——改编自习题4.4


原题链接

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
/* ZhihaoChen CSEE 1501 */
#include<stdio.h>;
/* Prototype Declaration */
void if_else_process (double x);
void switch_process (double x);
/* Main Function */
int main(void)
{
int number;
scanf("%d", &number);
int t; double x;
int count;
for (count = 0 ; count & number ; ++count)
{
scanf("%d%lf", &t, &x);
switch (t)
{
case 0:
if_else_process(x);
break;
case 1:
switch_process(x);
break;
case 2:
if_else_process(x);
switch_process(x);
break;
default:
break;
}
if (count != (number - 1))
printf("\n");
}
return 0;
}
/* data processing and output */
void if_else_process (double x)
{
double output;
if (x < 1000)
output = 0;
else if (x >= 1000 && x < 2000)
output = x * 0.05;
else if (x >= 2000 && x < 3000)
output = x * 0.10;
else if (x >= 3000 && x < 4000)
output = x * 0.15;
else if (x >= 4000 && x < 5000)
output = x * 0.20;
else if (x > 5000)
output = x * 0.25;
printf ("After if-else processing,the amount of tax to be paid is : %.2f\n", output);
}

void switch_process (double x)
{
double output;
switch((int)x / 1000)
{
case 0: output = 0; break;
case 1: output = x * 0.05; break;
case 2: output = x * 0.10; break;
case 3: output = x * 0.15; break;
case 4: output = x * 0.20; break;
default: output = x * 0.25; break;
}
printf("After switch processing,the amount of tax to be paid is : %.2f\n", output);
}

4.两个实数的四则运算——改编自习题4.5


原题链接

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
/* ZhihaoChen CSEE 1501 */
#include<stdio.h>;
/* Prototype Declaration */
void if_else_process(double a, double b, char c);
void switch_process(double a, double b, char c);
/* Main Function */
int main(void)
{
int t, count;
double a, b;
char c;
for (count = 0 ; (scanf("%d%lf%lf %c", &t, &a, &b, &c) != EOF) ; ++count)
{
if(count != 0)
printf("\n");
switch (t)
{
case 0:
if_else_process(a, b, c);
break;
case 1:
switch_process(a, b, c);
break;
case 2:
if_else_process(a, b, c);
switch_process(a, b, c);
break;
default:
break;
}
}
return 0;
}
/* data processing and output */
void if_else_process (double a, double b, char c)
{
double output = 0.0;
if (c == '+')
output = a + b;
if (c == '-')
output = a - b;
if (c == '*')
output = a * b;
if (c == '/')
output = a / b;
printf ("After if-else processing,the result is : %.2f\n", output);
}

void switch_process (double a, double b, char c)
{
double output = 0.0;
switch(c)
{
case '+': output = a + b; break;
case '-': output = a - b; break;
case '*': output = a * b; break;
case '/': output = a / b; break;
default: break;
}
printf("After switch processing,the result is : %.2f\n", output);
}

5.合并空格字符输出——改编自习题4.7


原题链接

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 <stdio.h>
#define CHAR 0
#define SPACE 1

int main (void)
{
char character;
int status = CHAR;
while ((character = getchar()) != EOF)
{
if ((character == ' ') && (status == CHAR))
{
status = SPACE;
printf(" ");
}
if ((character != ' ') && (status == CHAR))
{
printf("%c", character);
}
if ((character != ' ') && (status == SPACE))
{
status = CHAR;
printf("%c", character);
}
}
return 0;
}

6.求π的近似值——改编自习题4.10


原题链接

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include <stdio.h>
#include <math.h>

int main (void)
{
double item = 1.0;
double pi = 0.0;
int count;
for (count = 0 ; (item >= 1.0e-5) ; ++count)
{
item = 1.0/(2 * count + 1);
if (count % 2 == 0)
pi += item;
else
pi += (-item);
}
printf("%.9f\n", pi * 4);
return 0;
}

7.验证哥德巴赫猜想——改编自习题4.13


原题链接

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

int isPrime (int x);

int main (void)
{
int number, testnum;
scanf ("%d", &number);
int count, subnumber1, subnumber2;
for (count = 0 ; count < number ; ++count)
{
scanf("%d", &testnum);
for (subnumber1 = 2 ; subnumber1 <= (testnum / 2) ; ++subnumber1)
{
subnumber2 = testnum - subnumber1;
if (isPrime(subnumber2) && isPrime(subnumber1))
{
printf("%d=%d+%d\n", testnum, subnumber1, subnumber2);
break;
}
}
}
return 0;
}

int isPrime (int x)
{
int i;
for (i = 2 ; i <= sqrt(x) ; ++i)
if (x % i == 0) return 0;
return 1;
}

8.正整数相加求平均数——改编自习题4.17


原题链接

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
#include <stdio.h>
int main (void)
{
int number;
scanf("%d", &number);
int count, subcount, status; int n, positiveNumber;
long sumNumber;
double averageNumber;
for (count = 0 ; count < number ; ++count)
{
n = 0; sumNumber = 0;
positiveNumber = 0;
averageNumber = 0.0;
scanf("%d", &status);
switch(status)
{
case 0:
for (subcount = 0 ; subcount < 10 ; ++subcount)
{
scanf("%d", &n);
if (n <= 0) continue;
sumNumber += n;
positiveNumber += 1;
}
averageNumber = (double)sumNumber / positiveNumber;
if (positiveNumber != 0)
printf("In \"continue\" way,numbers=%d,average=%f\n", positiveNumber, averageNumber);
break;
case 1:
for (subcount = 0 ; subcount < 10 ; ++subcount)
{
scanf("%d", &n);
if(n > 0)
{
sumNumber += n;
positiveNumber += 1;
}
}
averageNumber = (double)sumNumber / positiveNumber;
if (positiveNumber != 0)
printf("In \"no continue\" way,numbers=%d,average=%f\n", positiveNumber, averageNumber);
break;
default:
break;
}
}
return 0;
}

要注意的问题

1.注意审题,比如第六题注意到“最后一项”的绝对值,再比如说第三题注意到输出格式逗号前后都没有空格

2.注意数据类型的范围

3.注意浮点数的科学计数法表示

4.注意条件运算要不要加等于号

5.逻辑错误比语法错误更难检查,要关注程序的细节

6.注意scanf在读取的字符(%c)时候会识别’ ‘,所以在格式化字符串中用’ %c’或者’%1s’均可(后者无法通过系统)

7.注意浮点数的运算存在精度损失,例:double i = 1/e-5 而(int)i=99999,在作为循环条件时需要格外注意,一般情况下最好避免,或者+0.0001再取整

评论和共享

寒假回家后买了一个树莓派来玩一玩,决定写博客来记录自己学到的东西。之前学习iOS的时候有很多时候都是单纯的解决了问题而没有归纳整理,结果到后面出现同样的问题的时候重复的在网上搜索,效率很低。这次尝试着记录自己的学习过程,看看效果如何。

第一步 准备

首先我在JD上买了一个树莓派二代B型,然而到货了之后,我才发现,光有板子是没有用的。

之后我还准备了以下物品

16G MicroSD卡*1(其实8G也足够了)

SD卡读卡器*1(MBA上没有这种MicroSD卡的插槽)

5V2.1A的电源适配器*1(除此以外供电也可以使用移动电源)

安卓手机的连接线*1

无源HDMI转VGA转接头*1(此处有伏笔)

网线*1

加上家里的显示器*1 USB鼠标*1 USB键盘*1

然后将键盘鼠标插入USB接口、HDMI线,电源线接好,MicroSD卡插入板子背面的插槽中
到这里第一步就结束了。

第二步 安装系统

1.下载树莓派镜像文件解压完约3G左右 树莓派镜像

2.取下MicroSD卡并通过读卡器连接至电脑

3.使用Win32 Disk Image 为MicroSD卡写入镜像文件(这里我是在Windows虚拟机下操作的)

第三步 启动树莓派

按照第一步的最后组装完树莓派,打开电源,显示屏也亮了起来。OK,大功告成~

然后第二天,我的树莓派就出现了问题…

问题 启动树莓派后 显示器显示无信号

1.我尝试了使用SDFormatter这个软件格式化了我的SD卡并重新写入系统镜像,失败

2.根据上网查找的结果,修改MicroSD卡下Config.txt文件,失败

3.最终发现是使用了无源HDMI转VGA转接头的问题,必须使用有源的HDMI转VGA转接头

以下引用网上给出的解释

HDMI连接器的第18针是一个+5V的供电,但是根据HDMI规范,其仅最多提供50mA的电流。

树莓派对第18针的供电,是将+5V输入,串接一个二极管D1(BAT54)作为保护送往18针。BAT54最高允许200mA电流(够宽松了其实)。

但是市售不带电源的HDMI→VGA转换器,往往会从HDMI 18针吸取高至500mA的电流,这样就会远远超出D1的耐受能力,而最终烧毁D1。
于是目前(在新的有源转接头到货以及确认我的二极管有没有烧坏之前),只能(也最好)使用SSH的方式登陆树莓派了

通过SSH登陆树莓派

其实我一直觉得,用ssh远程登录树莓派才显得比较Geek。

1.使用网线连接树莓派和路由器

2.在路由器的登陆页面(默认是192.168.1.1)找到你的树莓派设备,记下它的ip地址

3.在linux下,通过ssh -l pi xxx.xxx.xxx.xxx( 树莓派的ip) 登陆树莓派,密码默认为raspberry

4.作为测试,使用树莓派自带的gcc编译hello world

成功

 

最后再次提醒 请不要使用无源的HDMI转VGA转接头 请不要使用无源的HDMI转VGA转接头 请不要使用无源的HDMI转VGA转接头

评论和共享

博客上线

发布在 杂谈

折腾了2天半,总算是勉强能看了。

怎么说呢,最初我并不是很想现在搭个人博客的来着,因为截止到现在,已经是一月四日,很快就要期末考试了,这个时候不复习危机昏来花时间搭博客明显是一种作死行为。

但是由于内心的冲动,我还是这么做了。

记得大概五六年级的时候,我就曾经注册过一个discuz论坛。但是那个论坛终究也只是用的别人的域名,况且我并不认为那个时候我要论坛有什么用(当个版主,当个管理员)…所以后来就没有管了。

然后是初一初二的时候,那个时候用的是baidu的空间,当时baidu空间有各种各样的主题,各种各样的挂件,用着也还不错,我至今任然记得自己当时装X自己出了一道有关于魔兽的搜索题然后写了一篇文章大谈搜索算法还有剪枝策略的事(其实我当时连搜索是什么的不知道)

后来baidu空间关闭了,高中后由于住校+没有网,我也基本上放弃了写博客,偶尔写写说说,就当是自己还存在的证明。

然后到了大学,最终我下定了决心搭建这个博客,感觉可能有以下几个原因:

1.大学计算机基础的结课作业,我做了一个个人主页,我记得当时我提到,很多程序猿都有写博客、做个人主页的习惯。

2.安琪用WP搭的博客大大地刺激到了我…燃起了我搭博客的冲动;天哥花钱怒买阿里云的行为感染了我…激发了我的消费欲

3.这几个月学习iOS的过程中,从各种博客上学习到了很多,解决了很多问题。

…尽管我在昨天的时候还对linux一窍不通,买来的阿里云也完全不知道该怎么配置,经过了2天努力,我总算配置好了apache,mysql和php,装好了wp,回过头看看,虽然很简陋,虽然服务器还没有完全调好,但是我还是很开心的。

最后希望在这四年里,我能好好利用这个博客,做一些自己想做的事。

评论和共享

作者的图片

码龙黑曜

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


华中科技大学 本科在读


Wuhan, China