C语言练习:hackerrank十五关
文章目錄
- 第一關:Hello World C
- 第二關:讀取字符,字符串,句子
- 第三關:兩數加減
- 第四關:函數
- 第五關:指針
- 第六關:條件判斷
- 第七關:for循環
- 第八關:五數之和
- 第九關:位運算
- 第十關:打印模式字符串
- 第十一關:運盒子
- 第十一關:按三角形面積排序
- 第十二關:反轉字符串
- 第十三關:分詞
- 第十四關:數字頻率
- 第十五關:字符串排序
第一關: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.0Sample 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 5Sample 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 5Sample 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
8Sample Output #01
eightSample Input #02
44Sample 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 11Sample 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
10564Sample 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 4Sample 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;tmp = tmp < k ? tmp:0;maxAnd = maxAnd < tmp ? tmp : maxAnd; tmp = i | j;tmp = tmp < k ? tmp:0;maxOr = maxOr < tmp ? tmp : maxOr; tmp = i ^ j;tmp = tmp < k ? tmp:0;maxEx = maxEx < tmp ? tmp : maxEx; }}printf("%d\n%d\n%d\n",maxAnd,maxOr,maxEx); }int main() {int n, k;scanf("%d %d", &n, &k);calculate_the_maximum(n, k);return 0; }第十關:打印模式字符串
Sample Input 1
5Sample Output 1
5 5 5 5 5 5 5 5 5 5 4 4 4 4 4 4 4 5 5 4 3 3 3 3 3 4 5 5 4 3 2 2 2 3 4 5 5 4 3 2 1 2 3 4 5 5 4 3 2 2 2 3 4 5 5 4 3 3 3 3 3 4 5 5 4 4 4 4 4 4 4 5 5 5 5 5 5 5 5 5 5解決方案:
int min(int a,int b) {return 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 linesfor(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 42Sample Output 0
125 80解決方案:
#include <stdio.h> #include <stdlib.h> #define MAX_HEIGHT 41typedef 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 5Sample 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 12Sample 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 CSample 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
a11472o5t6Sample 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 feklsSample Output 0
fekls qoi sbv wkuewkue sbv qoi feklsqoi sbv wkue feklsqoi 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<strlen(a);i++){chrCount[a[i]]++;}for(int i=0;i<256;i++) {if(chrCount[i]!=0) sum++;}return sum; }int sort_by_number_of_distinct_characters(const void* aa, const void* bb) {char *a = *(char **)aa;char *b = *(char **)bb;int na = distinctChars(a);int nb = distinctChars(b);if(na==nb) return lexicographic_sort(aa,bb);return na - nb;}int sort_by_length(const void* aa, const void* bb) {char *a = *(char **)aa;char *b = *(char **)bb;if(strlen(a)==strlen(b)) return lexicographic_sort(aa, bb);return strlen(a)-strlen(b); }void string_sort(char** arr,const int len,int (*cmp_func)(const void* a, const void* b)){qsort(arr,len,sizeof(char *), cmp_func); }int main() {int n;scanf("%d", &n);char** arr;arr = (char**)malloc(n * sizeof(char*));for(int i = 0; i < n; i++){*(arr + i) = malloc(1024 * sizeof(char));scanf("%s", *(arr + i));*(arr + i) = realloc(*(arr + i), strlen(*(arr + i)) + 1);}string_sort(arr, n, lexicographic_sort);for(int i = 0; i < n; i++)printf("%s\n", arr[i]);printf("\n");string_sort(arr, n, lexicographic_sort_reverse);for(int i = 0; i < n; i++)printf("%s\n", arr[i]); printf("\n");string_sort(arr, n, sort_by_length);for(int i = 0; i < n; i++)printf("%s\n", arr[i]); printf("\n");string_sort(arr, n, sort_by_number_of_distinct_characters);for(int i = 0; i < n; i++)printf("%s\n", arr[i]); printf("\n"); }總結
以上是生活随笔為你收集整理的C语言练习:hackerrank十五关的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 吴裕雄--天生自然 物理学习与探索笔记:
- 下一篇: 175. Combine Two Tab