aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--challenge-113/abigail/README.md2
-rw-r--r--challenge-113/abigail/c/ch-1.c44
-rw-r--r--challenge-113/abigail/c/ch-2.c49
3 files changed, 95 insertions, 0 deletions
diff --git a/challenge-113/abigail/README.md b/challenge-113/abigail/README.md
index b4469041da..a1e156b3c0 100644
--- a/challenge-113/abigail/README.md
+++ b/challenge-113/abigail/README.md
@@ -20,6 +20,7 @@ Output: 1
### Solutions
* [AWK](awk/ch-1.awk)
* [Bash](bash/ch-1.sh)
+* [C](c/ch-1.c)
* [Perl](perl/ch-1.pl)
### Blog
@@ -57,6 +58,7 @@ Output: 1
### Solutions
* [AWK](awk/ch-2.awk)
* [Bash](bash/ch-2.sh)
+* [C](c/ch-1.c)
* [Perl](perl/ch-2.pl)
### Blog
diff --git a/challenge-113/abigail/c/ch-1.c b/challenge-113/abigail/c/ch-1.c
new file mode 100644
index 0000000000..e673fa7281
--- /dev/null
+++ b/challenge-113/abigail/c/ch-1.c
@@ -0,0 +1,44 @@
+# include <stdlib.h>
+# include <stdio.h>
+# include <string.h>
+# include <stdbool.h>
+
+/*
+ * See ../README.md
+ */
+
+/*
+ * Run as: cc -o ch-1.o ch-1.c; ./ch-1.o < input-file
+ */
+
+typedef long long number;
+typedef short digit;
+
+unsigned short tens [] = {0, 0, 1, 2, 1, 0, 2, 6, 3, 8};
+
+int main (void) {
+ number N;
+ digit D;
+
+ while (scanf ("%lld %hd", &N, &D) == 2) {
+ digit D10 = D == 0 ? 100 : 10 * D;
+ if (N >= D10 || (N % (D ? D : 10) == 0)) {
+ printf ("1\n");
+ continue;
+ }
+ bool valid = false;
+ for (unsigned short i = 1; i <= tens [D]; i ++) {
+ number T = N - 10 * i - D;
+ if (T >= 0 && T % D == 0) {
+ printf ("1\n");
+ valid = true;
+ break;
+ }
+ }
+ if (!valid) {
+ printf ("0\n");
+ }
+ }
+
+ return (0);
+}
diff --git a/challenge-113/abigail/c/ch-2.c b/challenge-113/abigail/c/ch-2.c
new file mode 100644
index 0000000000..fa2f79c27a
--- /dev/null
+++ b/challenge-113/abigail/c/ch-2.c
@@ -0,0 +1,49 @@
+# include <stdlib.h>
+# include <stdio.h>
+# include <string.h>
+
+/*
+ * See ../README.md
+ */
+
+/*
+ * Run as: cc -o ch-2.o ch-2.c; ./ch-2.o < input-file
+ */
+
+typedef long long number;
+
+int main (void) {
+ char * line = NULL;
+ size_t len = 0;
+
+ while (getline (&line, &len, stdin) != -1) {
+ size_t offset = 0;
+ int skip;
+ long long n, sum;
+
+ /*
+ * Read the numbers, calculate the sum.
+ */
+ sum = 0;
+ while (sscanf (line + offset, "%lld%n", &n, &skip) == 1) {
+ sum += n;
+ offset += skip;
+ }
+
+ /*
+ * Read the numbers again, write output.
+ */
+ offset = 0;
+ while (sscanf (line + offset, "%lld%n", &n, &skip) == 1) {
+ if (offset) {
+ printf (" ");
+ }
+ printf ("%lld", sum - n);
+ offset += skip;
+ }
+ printf ("\n");
+ }
+ free (line);
+
+ return (0);
+}