第一关:Hello World C

输入一行字符串(可能含空格),输出hello world\n,字符串

Sample Input 0

Welcome to C programming.

Sample Output 0

Hello, World!
Welcome to C programming.

解决方案

int main()
{
char s[100];
// *符: 用以表示该输入项读入后不赋予相应的变量,即跳过该输入值。
// %[] 扫描字符集合
scanf("%[^\n]%*c", &s);
printf("%s\n%s\n","Hello, World!",s);
return 0;
}

知识点

空格问题:

char s[20];
scanf("%s",s);
printf("%s\n",s);
// 输入:i love you
// 输出:
// i

解决空格问题:

char s[20];
// 读取非换行字符串,%*c表示读取换行,但不赋值
scanf("%[^\n]%*c",s);
printf("%s\n",s);
// 测试如下:
// i love you
// i love you

第二关:读取字符,字符串,句子

Sample Input 0

C
Language
Welcome To C!!

Sample Output 0

C
Language
Welcome To C!!

解决方案

char ch;
char str[100];
char sentence[100]; scanf("%c",&ch);
printf("%c\n",ch); // 这里加个%*c,把结尾的换行消耗了,因为下一个scanf读到换行结束,而
// scanf("%s",s);
// printf("%s\n",s);
// 结果:
// ccc
// ccc
// 是会跨过前面的空白符的 scanf("%s%*c",str);
printf("%s\n",str); scanf("%[^\n]",sentence);
printf("%s\n",sentence);

第三关:两数加减

前两个int,后两个float,输出和and差。

Sample Input

10 4
4.0 2.0

Sample Output

14 6
6.0 2.0

解决方案

int a,b;
float c,d;
scanf("%d%d",&a,&b);
printf("%d %d\n",a+b,a-b);
scanf("%f%f",&c,&d);
printf("%.1f %.1f\n",c+d,c-d);

知识点

还是在stdinstdout的输入输出上面。

第四关:函数

读取输入四个数字,一个数字一行,输出最大的数字。int类型。

Sample Input

3
4
6
5

Sample Output

6

解决方案

int max_of_four(int a,int b,int c,int d)
{
int maxNum = a>b?a:b;
maxNum = c>maxNum?c:maxNum;
maxNum = d>maxNum?d:maxNum;
return maxNum;
} int main() {
int a, b, c, d;
scanf("%d %d %d %d", &a, &b, &c, &d);
int ans = max_of_four(a, b, c, d);
printf("%d", ans);
return 0;
}

第五关:指针

输入两个数a和b,将更改为a+b, | a-b |。

void update(int *a,int *b)

Sample Input

4
5

Sample Output

9
1

解决方案:

#include <stdio.h>

void update(int *a,int *b) {
int sum = *a + *b;
int dif = *a - *b;
dif = dif>0?dif:-dif;
*a = sum;
*b = dif;
} int main() {
int a, b;
int *pa = &a, *pb = &b; scanf("%d %d", &a, &b);
update(pa, pb);
printf("%d\n%d", a, b); return 0;
}

第六关:条件判断

Given a positive integer denoting , do the following:

  • If 1-9, print the lowercase English word corresponding to the number (e.g., one for , two for , etc.).
  • If > 9, print Greater than 9.

Sample Input #01

8

Sample Output #01

eight

Sample Input #02

44

Sample Output #02

Greater than 9

解决方案:

char* readline();

int main()
{
char* n_endptr;
// 这里的readline是系统定义的,可以自动分配内存
// 在这里实际并不需要。
// 也可直接写为:
// char* n_endptr = malloc(1024);
// char* n_str = fgets(n_endptr,1023,stdin);
char* n_str = readline();
int n = strtol(n_str, &n_endptr, 10);
// 没读到整数字符串,或者不止有整数字符,退出
if (n_endptr == n_str || *n_endptr != '\0') { exit(EXIT_FAILURE); } char *numStr[10]={"zero","one","two","three","four","five","six",
"seven","eight","nine"};
if(n<9) printf("%s\n",numStr[n]);
else printf("Greater than 9\n");
return 0;
} char* readline() {
size_t alloc_length = 1024;
size_t data_length = 0;
char* data = malloc(alloc_length); while (true) {
char* cursor = data + data_length;
char* line = fgets(cursor, alloc_length - data_length, stdin); if (!line) { break; } data_length += strlen(cursor); if (data_length < alloc_length - 1 || data[data_length - 1] == '\n') { break; } size_t new_length = alloc_length << 1;
data = realloc(data, new_length); if (!data) { break; } alloc_length = new_length;
} if (data[data_length - 1] == '\n') {
data[data_length - 1] = '\0';
} data = realloc(data, data_length); return data;
}

知识点:

strtol:字符串转long

char str[30] = "Hoi 2030300 This is test"; // 0, ptr:Hoi ...
//char str[30] = "2030300 This is test"; // 2030300, ptr: This is test
char *ptr; // 整数结束的字符串
long ret; // 长整数 // 10表示十进制。如二进制
// char str[30] = "10010 This is test";
// ret = strtol(str, &ptr,2); // 18
ret = strtol(str, &ptr, 10);
printf("数字(无符号长整数)是 %ld\n", ret);
printf("字符串部分是 |%s|\n", ptr);

第七关:for循环

给两个整数,输出区间内的数字英文。1-9,输出对应英文,> 9,输出even还是odd。

Sample Input

8
11

Sample Output

eight
nine
even
odd

解决方案:

int main()
{
char *numStr[10]={"zero","one","two","three","four","five","six",
"seven","eight","nine"};
int a, b;
scanf("%d\n%d", &a, &b);
for(int i=a;i<=b;i++)
{
if(i<=9)
{
printf("%s\n",numStr[i]);
continue;
}
if(i%2==0) printf("even\n");
else printf("odd\n");
} return 0;
}

第八关:五数之和

输入数字:10000-99999

Sample Input 0

10564

Sample Output 0

16

解决方案:

int main() {
int n;
int sum = 0;
int tmp;
scanf("%d", &n);
while(n!=0)
{
tmp = n%10;
n /= 10;
sum+=tmp;
}
printf("%d\n",sum);
return 0;
}

第九关:位运算

给两个整数,n:一个整数,k:阈值(小于n)。输出i=1 ~ n和i+1 ~ n的and, or, xor的小于k的最大值。

Sample Input 0

5 4

Sample Output 0

2
3
3

解决方案:

void calculate_the_maximum(int n, int k) {
int maxAnd = 0;
int maxOr = 0;
int maxEx = 0;
int tmp;
for(int i=1;i<n;i++) {="" for(int="" j="i+1;j<=n;j++)" tmp="i" &="" j;="" <="" k="" ?="" tmp:0;="" maxand="maxAnd" :="" maxand;="" |="" maxor="maxOr" maxor;="" ^="" maxex="maxEx" maxex;="" }="" printf("%d\n%d\n%d\n",maxand,maxor,maxex);="" int="" main()="" n,="" k;="" scanf("%d="" %d",="" &n,="" &k);="" calculate_the_maximum(n,="" k);="" return="" 0;="" ```="" ##="" 第十关:打印模式字符串="" **sample="" input="" 1**="" ```shell="" 5="" output="" 4="" 3="" 2="" 1="" **解决方案:**="" ```c="" min(int="" a,int="" b)="" a="">b?b:a;
} int minDistance(int a,int b,int n)
{
return min(min(min(a,b),2*n-2-a),2*n-2-b);
} int main()
{
int n;
scanf("%d", &n);
// 2 * n - 1 lines
for(int i=0;i<2*n-1;i++)
{
for(int j=0;j<2*n-2;j++)
{
printf("%d ",n - minDistance(i,j,n));
}
printf("%d\n",n);
}
return 0;
}

知识点:

这一题关键是要找出距离外围的最小距离。

第十一关:运盒子

输入盒子个数n,接着n行:长、宽、高

通道高度只有41。输出能过通道的盒子体积(41不能过)。

Sample Input 0

4
5 5 5
1 2 40
10 5 41
7 2 42

Sample Output 0

125
80

解决方案:

#include <stdio.h>
#include <stdlib.h>
#define MAX_HEIGHT 41 typedef struct box
{
int width;
int height;
int length;
}box; int get_volume(box b) {
return b.width * b.height * b.length;
} int is_lower_than_max_height(box b) {
return b.height < MAX_HEIGHT?1:0;
} int main()
{
int n;
scanf("%d", &n);
box *boxes = malloc(n * sizeof(box));
for (int i = 0; i < n; i++) {
scanf("%d%d%d", &boxes[i].length, &boxes[i].width, &boxes[i].height);
}
for (int i = 0; i < n; i++) {
if (is_lower_than_max_height(boxes[i])) {
printf("%d\n", get_volume(boxes[i]));
}
}
return 0;
}

知识点:结构体。

第十一关:按三角形面积排序

面积公式:

输入三角形个数,和对应的三条边。

Sample Input 0

3
7 24 25
5 12 13
3 4 5

Sample Output 0

3 4 5
5 12 13
7 24 25

解决方案:

#include <stdio.h>
#include <stdlib.h>
#include <math.h> struct triangle
{
int a;
int b;
int c;
}; typedef struct triangle triangle; float areaSquare(const triangle *t)
{
float p = (t->a + t->b + t->c)/2.0;
return p*(p - t->a)*(p - t->b)*(p - t->c);
} int mycmp(const void *a, const void *b)
{
triangle *ta = (triangle *)a;
triangle *tb = (triangle *)b;
return areaSquare(ta)>areaSquare(tb);
} void sort_by_area(triangle* tr, int n) {
qsort(tr,n,sizeof(triangle),mycmp);
} int main()
{
int n;
scanf("%d", &n);
triangle *tr = malloc(n * sizeof(triangle));
for (int i = 0; i < n; i++) {
scanf("%d%d%d", &tr[i].a, &tr[i].b, &tr[i].c);
}
sort_by_area(tr, n);
for (int i = 0; i < n; i++) {
printf("%d %d %d\n", tr[i].a, tr[i].b, tr[i].c);
}
return 0;
}

知识点:

void qsort(void *base, size_t nmemb, size_t size,
int (*compar)(const void *, const void *));

结构体快排。

第十二关:反转字符串

Sample Input 0

6
16 13 7 2 1 12

Sample Output 0

12 1 2 7 13 16

解决方案:

#include <stdio.h>
#include <stdlib.h> void swap(int *a,int *b)
{
int tmp = *a;
*a = *b;
*b = tmp;
} int main()
{
int num, *arr, i;
scanf("%d", &num);
arr = (int*) malloc(num * sizeof(int));
for(i = 0; i < num; i++) {
scanf("%d", arr + i);
} for(i = 0;i<(num+1)/2;i++)
{
swap(&arr[i],&arr[num-1-i]);
} for(i = 0; i < num; i++)
printf("%d ", *(arr + i));
return 0;
}

第十三关:分词

Sample Input 0

This is C

Sample Output 0

This
is
C

解决方案

int main() {
char *s;
s = malloc(1024 * sizeof(char));
scanf("%[^\n]", s);
s = realloc(s, strlen(s) + 1);
char *token = strtok(s," ");
while(token!=NULL)
{
printf("%s\n",token);
token = strtok(NULL," ");
}
return 0;
}

知识点:

strtok,该函数返回被分解的一个子字符串,如果没有可检索的字符串,则返回一个空指针。

使用方法:

  • 第1次调用时,第1个参数要传入1个C的字符串,作为要分割的字符串

  • 后续调用时,第1个参数设置为空指针NULL

  • 上一个被分割的子字符串的位置会被函数内部记住,所以后续调用时,第1个参数设置为NULL

第十四关:数字频率

字符串中0-9数字频率,如1出现了两次,output的第二个位置就是2

Sample Input 0

a11472o5t6

Sample Output 0

0 2 1 0 1 1 1 1 0 0

解决方案:

int main() {
char str[1024];
char numCount[10] = {0};
scanf("%s",str);
for(int i=0;i<strlen(str);i++) {="" if(str[i]="">='0'&&str[i]<='9')
{
numCount[str[i]-'0']++;
}
}
for(int i=0;i<10;i++)
printf("%d ",numCount[i]);
return 0;
}

知识点:

数组map的思想。

第十五关:字符串排序

词典排序、词典逆序、不同字母的个数排序(相同按字典排序)、长度排序(相同按字典排序)。

Sample Input 0

4
wkue
qoi
sbv
fekls

Sample Output 0

fekls
qoi
sbv
wkue wkue
sbv
qoi
fekls qoi
sbv
wkue
fekls qoi
sbv
wkue
fekls

解决方案:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int lexicographic_sort(const void* aa, const void* bb) {
char *a = *(char **)aa;
char *b = *(char **)bb;
int alen = strlen(a);
int blen = strlen(b);
int minlen = alen>blen?blen:alen;
for(int i=0;i<minlen;i++) {="" if(a[i]!="b[i])" return="" a[i]-b[i];="" }="" if(alen="">blen) return 1;
return -1;
} int lexicographic_sort_reverse(const void* aa, const void* bb) {
char *a = *(char **)aa;
char *b = *(char **)bb;
int alen = strlen(a);
int blen = strlen(b);
int minlen = alen>blen?blen:alen;
for(int i=0;i<minlen;i++) {="" if(a[i]!="b[i])" return="" b[i]-a[i];="" }="" if(alen="">blen) return -1;
return 1;
} int distinctChars(const char *a)
{
int sum = 0;
int chrCount[256] = {0};
for(int i=0;i</minlen;i++)></minlen;i++)></string.h></stdlib.h></stdio.h></strlen(str);i++)></stdlib.h></stdio.h></math.h></stdlib.h></stdio.h></stdlib.h></stdio.h></n;i++)></stdio.h>

C语言练习:hackerrank十五关的更多相关文章

  1. SQL注入之Sqli-labs系列第四十一关(基于堆叠注入的盲注)和四十二关四十三关四十四关四十五关

    0x1普通测试方式 (1)输入and1=1和and1=2测试,返回错误,证明存在注入 (2)union select联合查询 (3)查询表名 (4)其他 payload: ,( ,( 0x2 堆叠注入 ...

  2. SQL注入之Sqli-labs系列第三十四关(基于宽字符逃逸POST注入)和三十五关

    开始挑战第三十四关和第三十五关(Bypass add addslashes) 0x1查看源码 本关是post型的注入漏洞,同样的也是将post过来的内容进行了 ' \ 的处理. if(isset($_ ...

  3. SQL注入之Sqli-labs系列第二十五关(过滤 OR & AND)和第二十五A关(过滤逻辑运算符注释符)

    开始挑战第二十五关(Trick with OR & AND) 第二十五关A(Trick with comments) 0x1先查看源码 (1)这里的or和and采用了i正则匹配,大小写都无法绕 ...

  4. SQL注入之Sqli-labs系列第十五关和第十六关(基于POST的时间盲注)

    开始挑战第十五关(Blind- Boolian Based- String)和 第十六关(Blind- Time Based- Double quotes- String) 访问地址,输入报错语句 ' ...

  5. webug第十五关:什么?图片上传不了?

    第十五关:什么?图片上传不了? 直接上传php一句话失败,将content type改为图片 成功

  6. Python Challenge 第十五关

    第15关,题目是 whom? 有一张图片,是个日历.日历的年份是 1XX6,中间是被挖去的洞.然后图中1月26日被画了个圈,当天是星期一.右下角的二月小图中有29号,可以得知这是闰年.然后查看源代码. ...

  7. 苹果新的编程语言 Swift 语言进阶(十五)--协议

    协议定义了适合某个特定任务或功能需要的方法.属性和其它需求的一个蓝图.协议本身不提供这些需求的实现,它只是描述了一个任务或功能实现的蓝图. 协议与java 语言中的接口定义类似,都是描述了一个实现可以 ...

  8. Swift5 语言指南(二十五) 自动引用计数(ARC)

    Swift使用自动引用计数(ARC)来跟踪和管理应用程序的内存使用情况.在大多数情况下,这意味着内存管理在Swift中“正常工作”,您不需要自己考虑内存管理.当不再需要这些实例时,ARC会自动释放类实 ...

  9. GO语言学习(十五)Go 语言指针

    Go 语言指针 Go 语言中指针是很容易学习的,Go 语言中使用指针可以更简单的执行一些任务. 接下来让我们来一步步学习 Go 语言指针. 我们都知道,变量是一种使用方便的占位符,用于引用计算机内存地 ...

随机推荐

  1. 【java虚拟机】jvm调优

    转自:https://www.cnblogs.com/starhu/p/6400348.html?utm_source=itdadao&utm_medium=referral 堆大小设置JVM ...

  2. Object 的wait()方法

    The java.lang.Object.wait() causes current thread to wait until another thread invokes the notify() ...

  3. docker搭建mysql集群

    目录 一.集群方案 二.安装PXC集群 三.Haproxy负载均衡 四.访问测试 五.节点宕机或重启 六.参考 一.集群方案 1.Replication 速度快,但仅能保证弱一致性,适用于保存价值不高 ...

  4. Ubuntu 16.04LTS下eclipse连接mysql

    第一部分:打开eclipse,新建一个web工程,新建一个类db_test.java(jdbc连接mysql的原理自行百度) import java.sql.*; public class db_te ...

  5. vue-cli3.x中的webpack配置,优化及多页面应用开发

    官方文档 vue-cli3以下版本中,关于webpack的一些配置都在config目录文件中,可是vue-cli3以上版本中,没有了config目录,那该怎么配置webpack呢? 3.x初始化项目后 ...

  6. 移动端ios上下滑动翻页事件失效

    移动端开发过程中,在添加上下滑动事件时候,引入了最常用的移动端库zepto.js及其touch模块,有一种现象,安卓的手机没有问题,上下滑动翻页很正常 :但是到了ios上面,好啊,上下滑动会出现弹性滚 ...

  7. APT组织跟踪与溯源

    前言 在攻防演练中,高质量的蓝队报告往往需要溯源到攻击团队.国内黑产犯罪团伙.国外APT攻击. 红队现阶段对自己的信息保护的往往较好,根据以往溯源成功案例来看还是通过前端js获取用户ID信息.mysq ...

  8. Django分页组件——Paginator

    from django.core.paginator import Paginator #导入Paginator objects = ['john','paul','george','ringo',' ...

  9. Python - 面向对象编程 - __new()__ 和单例模式 

    单例模式 这是一种设计模式 设计模式是前任工作的总结和提炼,通常,被人们广泛流传的设计模式都是针对某一特定问题的成熟的解决方案 使用设计模式是为了可重用代码.让代码更容易被他人理解.保证代码可靠性 单 ...

  10. linux网络编程(一)

    ============================================================== 第一天:基本概念.TCP.FTP: =================== ...