瀏覽代碼

[C] Adding solution for problem 0004

Vinicius Teshima 1 年之前
父節點
當前提交
24ec981fbc
共有 4 個文件被更改,包括 68 次插入4 次删除
  1. 2 1
      c/.gitignore
  2. 52 0
      c/0004.c
  3. 8 3
      c/Makefile
  4. 6 0
      c/Project.ede

+ 2 - 1
c/.gitignore

@@ -4,4 +4,5 @@
 # Executables
 0001
 0002
-0003
+0003
+0004

+ 52 - 0
c/0004.c

@@ -0,0 +1,52 @@
+#include <stdio.h>
+#include <stdint.h>
+#include <stdbool.h>
+
+bool ispalindrome(const char *cstr, uint64_t cstr_size);
+
+int
+main(int argc, const char **argv)
+{
+	uint64_t res = 0;
+	uint64_t sum = 0;
+	uint64_t upper_limit = 999;
+	uint64_t n1 = upper_limit, n2 = upper_limit;
+	char str[16] = {0};
+	uint64_t str_size = 0;
+
+	while ( n2 > 0 ) {
+		sum = n1 * n2;
+		str_size = (uint64_t)sprintf(str, "%ld", sum);
+		if ( ispalindrome(str, str_size) ) {
+			if ( sum > res ) {
+				res = sum;
+			}
+		}
+		n1 -= ( n1 > 0 );
+		if ( n1 == 0 ) {
+			n1 = upper_limit;
+			--n2;
+		}
+	}
+
+	printf("Result = %ld!\n", res);
+	(void) argc; (void) argv;
+	return 0;
+}
+
+bool
+ispalindrome(const char *cstr, uint64_t cstr_size)
+{
+	uint64_t ptr_start = 0;
+	uint64_t ptr_end = cstr_size-1;
+
+	while ( ptr_start < ptr_end ) {
+		if ( cstr[ptr_start] != cstr[ptr_end] ) {
+			return false;
+		}
+		++ptr_start;
+		--ptr_end;
+	}
+	return true;
+}
+

+ 8 - 3
c/Makefile

@@ -21,13 +21,15 @@ C_LINK=$(CC) $(CFLAGS) $(LDFLAGS) -L.
 0002_OBJ= 0002.o
 0003_SOURCES=0003.c
 0003_OBJ= 0003.o
+0004_SOURCES=0004.c
+0004_OBJ= 0004.o
 VERSION=1.0
 DISTDIR=$(top)ProjectEuler_C-$(VERSION)
 top_builddir = 
 
-DEP_FILES=.deps/0001.P .deps/0002.P .deps/0003.P
+DEP_FILES=.deps/0001.P .deps/0002.P .deps/0003.P .deps/0004.P
 
-all: 0001 0002 0003
+all: 0001 0002 0003 0004
 
 DEPS_MAGIC := $(shell mkdir .deps > /dev/null 2>&1 || :)
 -include $(DEP_FILES)
@@ -45,6 +47,9 @@ DEPS_MAGIC := $(shell mkdir .deps > /dev/null 2>&1 || :)
 0003: $(0003_OBJ)
 	$(C_LINK) -o $@ $^ $(LDDEPS)
 
+0004: $(0004_OBJ)
+	$(C_LINK) -o $@ $^ $(LDDEPS)
+
 tags: 
 
 
@@ -55,7 +60,7 @@ clean:
 
 dist:
 	mkdir $(DISTDIR)
-	cp $(0001_SOURCES) $(0002_SOURCES) $(0003_SOURCES) $(ede_FILES) $(DISTDIR)
+	cp $(0001_SOURCES) $(0002_SOURCES) $(0003_SOURCES) $(0004_SOURCES) $(ede_FILES) $(DISTDIR)
 
 Makefile: Project.ede
 	@echo Makefile is out of date!  It needs to be regenerated by EDE.

+ 6 - 0
c/Project.ede

@@ -22,6 +22,12 @@
       :name "0003"
       :path ""
       :source '("0003.c")
+      :configuration-variables '("debug" ("CFLAGS" . "-g") ("LDFLAGS" . "-g")))
+    (ede-proj-target-makefile-program "0004"
+      :object-name "0004"
+      :name "0004"
+      :path ""
+      :source '("0004.c")
       :configuration-variables '("debug" ("CFLAGS" . "-g") ("LDFLAGS" . "-g"))))
   :configurations '("debug" "release")
   :object-name "ProjectEuler_C"