C语言练习:hackerrank十五关
第一关: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);
知识点:
还是在stdin和stdout的输入输出上面。
第四关:函数
读取输入四个数字,一个数字一行,输出最大的数字。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., onefor ,twofor , 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十五关的更多相关文章
- SQL注入之Sqli-labs系列第四十一关(基于堆叠注入的盲注)和四十二关四十三关四十四关四十五关
		0x1普通测试方式 (1)输入and1=1和and1=2测试,返回错误,证明存在注入 (2)union select联合查询 (3)查询表名 (4)其他 payload: ,( ,( 0x2 堆叠注入 ... 
- SQL注入之Sqli-labs系列第三十四关(基于宽字符逃逸POST注入)和三十五关
		开始挑战第三十四关和第三十五关(Bypass add addslashes) 0x1查看源码 本关是post型的注入漏洞,同样的也是将post过来的内容进行了 ' \ 的处理. if(isset($_ ... 
- SQL注入之Sqli-labs系列第二十五关(过滤 OR & AND)和第二十五A关(过滤逻辑运算符注释符)
		开始挑战第二十五关(Trick with OR & AND) 第二十五关A(Trick with comments) 0x1先查看源码 (1)这里的or和and采用了i正则匹配,大小写都无法绕 ... 
- SQL注入之Sqli-labs系列第十五关和第十六关(基于POST的时间盲注)
		开始挑战第十五关(Blind- Boolian Based- String)和 第十六关(Blind- Time Based- Double quotes- String) 访问地址,输入报错语句 ' ... 
- webug第十五关:什么?图片上传不了?
		第十五关:什么?图片上传不了? 直接上传php一句话失败,将content type改为图片 成功 
- Python Challenge 第十五关
		第15关,题目是 whom? 有一张图片,是个日历.日历的年份是 1XX6,中间是被挖去的洞.然后图中1月26日被画了个圈,当天是星期一.右下角的二月小图中有29号,可以得知这是闰年.然后查看源代码. ... 
- 苹果新的编程语言 Swift 语言进阶(十五)--协议
		协议定义了适合某个特定任务或功能需要的方法.属性和其它需求的一个蓝图.协议本身不提供这些需求的实现,它只是描述了一个任务或功能实现的蓝图. 协议与java 语言中的接口定义类似,都是描述了一个实现可以 ... 
- Swift5 语言指南(二十五) 自动引用计数(ARC)
		Swift使用自动引用计数(ARC)来跟踪和管理应用程序的内存使用情况.在大多数情况下,这意味着内存管理在Swift中“正常工作”,您不需要自己考虑内存管理.当不再需要这些实例时,ARC会自动释放类实 ... 
- GO语言学习(十五)Go 语言指针
		Go 语言指针 Go 语言中指针是很容易学习的,Go 语言中使用指针可以更简单的执行一些任务. 接下来让我们来一步步学习 Go 语言指针. 我们都知道,变量是一种使用方便的占位符,用于引用计算机内存地 ... 
随机推荐
- uwp 动画之圆的放大与缩小
			xml code --------------------------------------------------- <Page x:Class="MyApp.MainPage&q ... 
- jQuery中的基本选择器(四、一):* 、 . 、element(直接标签名)、 或者用逗号隔开跟多个
			<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ... 
- mysql基础操作(二):简单查询DQL
			-- 1.查询所有字段 select * from student; -- 2.查询指定的字段 select id from student; select id, name from student ... 
- Python - 面向对象编程 - 公共属性、保护属性、私有属性
			公共属性 在 Python 的类里面,所有属性和方法默认都是公共的 class PoloBlog: # 公共属性 sum = 0 # 构造方法 def __init__(self, name): se ... 
- Linux常用命令(二)之权限管理、文件搜索、帮助、压缩命令及管道
			在(一)中提到过rwx的含义,但是我们还需深入理解,明白其真正的含义和权限,对于文件和目录,rwx权限是不同的,尤其是目录的权限往往是被忽略的: 对于目录,其权限和对应的操作: r-ls w-touc ... 
- java变量类型和常量类型
			变量类型 局部变量 实例变量 类变量 public class 变量类型 { //属性:变量 //必须先定义再使用,并初始化 //布尔型:默认值为false //3. 类变量(静态变量) static ... 
- 我的第一个npm包:wechat-menu-editor 基于Vue的微信自定义菜单编辑器
			wechat-menu-editor 微信自定义菜单编辑器 前言 在做微信公众号相关开发时,基本上会去开发的功能就是微信自定义菜单设置的功能,本着不重复造轮子的原则,于是基于Vue封装的一个微信自定义 ... 
- centos7安装privoxy
			本文分为三部分,第一部分是在阿里云的ECS上安装Privoxy,第二部分是在AWS的EC2上安装Privoxy,第三部分是Privoxy的配置. 第一部分:阿里云ECS安装Privoxy 配置yum源 ... 
- 学习Tomcat(一)之容器概览
			Tomcat是Apache软件基金会的一个顶级项目,由Apache.Sun和其它一些公司及个人共同开发,是目前比较流行的Web服务器之一.Tomcat是一个开源的.小型的轻量级应用服务器,具有占用系统 ... 
- MapReduce 示例:减少 Hadoop MapReduce 中的侧连接
			摘要:在排序和reducer 阶段,reduce 侧连接过程会产生巨大的网络I/O 流量,在这个阶段,相同键的值被聚集在一起. 本文分享自华为云社区<MapReduce 示例:减少 Hadoop ... 
