-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
191 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
set(EXECUTABLE_OUTPUT_PATH ${OUTPUT_BASE}/cprogram) | ||
|
||
link_libraries(m) | ||
|
||
add_executable(reverse_words reverse_words.c) | ||
add_executable(min_common_value min_common_value.c) | ||
add_executable(bookshop bookshop.c) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
# C Program | ||
|
||
《C 语言程序设计》课程相关题目 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
#include <stdio.h> | ||
#include <string.h> | ||
|
||
struct Book { | ||
int id; | ||
char title[50]; | ||
char author[50]; | ||
int price; | ||
}; | ||
|
||
struct BookShop { | ||
char name[32]; | ||
struct Book books[100]; | ||
int nbooks; | ||
}; | ||
|
||
int main(int argc, char const* argv[]) { | ||
struct BookShop shops[10]; | ||
int nshops; | ||
scanf("%d", &nshops); | ||
for (int i = 0; i < nshops; i++) { | ||
struct BookShop* shop = &shops[i]; | ||
scanf("%s", shop->name); | ||
scanf("%d", &shop->nbooks); | ||
for (int j = 0; j < shop->nbooks; j++) { | ||
struct Book* book = &shop->books[j]; | ||
scanf("%d", &book->id); | ||
scanf("%s", book->title); | ||
scanf("%s", book->author); | ||
scanf("%d", &book->price); | ||
} | ||
} | ||
|
||
// input an author name, find all the books written by the author, and print the | ||
// shop name, book title, and price of each book | ||
char author[50]; | ||
scanf("%s", author); | ||
for (int i = 0; i < nshops; i++) { | ||
struct BookShop* shop = &shops[i]; | ||
for (int j = 0; j < shop->nbooks; j++) { | ||
struct Book* book = &shop->books[j]; | ||
if (strcmp(book->author, author) == 0) { | ||
printf("%s %s %d\n", shop->name, book->title, book->price); | ||
} | ||
} | ||
} | ||
|
||
return 0; | ||
} | ||
|
||
/*give an example input | ||
3 | ||
shop1 | ||
3 | ||
1 book1 author1 105 | ||
2 book2 author1 520 | ||
3 book3 author2 200 | ||
shop2 | ||
2 | ||
4 book4 author1 350 | ||
5 book5 author2 240 | ||
shop3 | ||
4 | ||
6 book6 author3 230 | ||
7 book7 author4 380 | ||
8 book8 author1 105 | ||
9 book9 author5 260 | ||
author1 | ||
*/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
#include <stdio.h> | ||
#define INT_MAX 1000000 | ||
|
||
int main(int argc, char const* argv[]) { | ||
int n1, n2; | ||
int arr1[50], arr2[50]; | ||
|
||
scanf("%d %d", &n1, &n2); | ||
for (int i = 0; i < n1; i++) { | ||
scanf("%d", &arr1[i]); | ||
} | ||
for (int i = 0; i < n2; i++) { | ||
scanf("%d", &arr2[i]); | ||
} | ||
|
||
int* ptr1 = arr1; | ||
int* ptr2 = arr2; | ||
|
||
int min = INT_MAX; | ||
|
||
while (ptr1 < arr1 + n1) { | ||
while (ptr2 < arr2 + n2) { | ||
if (*ptr1 == *ptr2) { | ||
if (*ptr1 < min) { | ||
min = *ptr1; | ||
} | ||
} | ||
ptr2++; | ||
} | ||
ptr2 = arr2; | ||
ptr1++; | ||
} | ||
|
||
if (min == INT_MAX) { | ||
printf("-1\n"); | ||
} else { | ||
printf("%d\n", min); | ||
} | ||
|
||
return 0; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
/** | ||
* Reverse the words in a string using pointer | ||
* | ||
* Example: | ||
* Input: "Hello World" | ||
* Output: "World Hello" | ||
*/ | ||
#include <stdio.h> | ||
#include <string.h> | ||
|
||
void simplify(char* str) { | ||
char* p1 = str; | ||
char* p2 = str; | ||
int n = 0; | ||
|
||
while (*p2) { | ||
if (*p2 == ' ' && (n == 0 || *(p2 + 1) == ' ' || *(p2 + 1) == '\0')) { | ||
p2++; | ||
} else { | ||
*p1 = *p2; | ||
p1++; | ||
p2++; | ||
n++; | ||
} | ||
} | ||
|
||
*p1 = '\0'; | ||
} | ||
|
||
void reverse(char* start, char* end) { | ||
char temp; | ||
while (start < end) { | ||
temp = *start; | ||
*start = *end; | ||
*end = temp; | ||
start++; | ||
end--; | ||
} | ||
} | ||
|
||
int main(int argc, char const* argv[]) { | ||
char str[100]; | ||
scanf("%[^\n]", str); | ||
|
||
simplify(str); | ||
|
||
reverse(str, str + strlen(str) - 1); | ||
|
||
char* start = str; | ||
char* end = str; | ||
|
||
while (*end) { | ||
if (*(end + 1) == '\0') { | ||
reverse(start, end); | ||
} else if (*end == ' ') { | ||
reverse(start, end - 1); | ||
while (*(end + 1) && *(end + 1) == ' ') { | ||
end++; | ||
} | ||
start = end + 1; | ||
} | ||
end++; | ||
} | ||
|
||
printf("%s\n", str); | ||
|
||
return 0; | ||
} |