-
Notifications
You must be signed in to change notification settings - Fork 458
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: YdrMaster <[email protected]>
- Loading branch information
Showing
8 changed files
with
88 additions
and
33 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,7 @@ | ||
#include <iostream> | ||
#include "../exercise.h" | ||
|
||
int main(int argc, char **argv) { | ||
// TODO: 在控制台输出 "Hello, InfiniTensor!" 并换行 | ||
std::cout << "Hello, InfiniTensor!" << std::endl; | ||
std::cout : "Hello, InfiniTensor!" + std::endl; | ||
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 |
---|---|---|
@@ -1,8 +1,8 @@ | ||
#include <iostream> | ||
#include "../exercise.h" | ||
|
||
int main(int argc, char **argv) { | ||
// TODO: 补全变量定义并打印加法运算 | ||
auto x = 1; | ||
// x ? | ||
std::cout << x << " + " << x << " = " << x + x << std::endl; | ||
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 |
---|---|---|
@@ -1,19 +1,16 @@ | ||
#include <cassert> | ||
#include <iostream> | ||
#include "../exercise.h" | ||
|
||
// NOTE: 这是函数声明 | ||
int add(int, int); | ||
// TODO: 在这里声明函数 | ||
|
||
int main(int argc, char **argv) { | ||
assert(add(123, 456) == 123 + 456); | ||
ASSERT(add(123, 456) == 123 + 456, "add(123, 456) should be 123 + 456"); | ||
|
||
auto x = 1, y = 2; | ||
std::cout << x << " + " << y << " = " << add(x, y) << std::endl; | ||
return 0; | ||
} | ||
|
||
// TODO: 补全函数定义 | ||
// TODO: 补全函数定义,但不要移动代码行 | ||
// THINK: `static` 修饰函数有什么效果? | ||
static int add(int a, int b) { | ||
return a + b; | ||
} |
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 |
---|---|---|
@@ -1,17 +1,24 @@ | ||
// READ: <https://stackoverflow.com/questions/156767/whats-the-difference-between-an-argument-and-a-parameter> | ||
// TODO: 为下列 ASSERT 填写正确的值 | ||
|
||
#include <iostream> | ||
#include "../exercise.h" | ||
|
||
static void func(int param) { | ||
std::cout << "befor add: " << param << std::endl; | ||
param += 1; | ||
std::cout << "after add: " << param << std::endl; | ||
} | ||
void func(int); | ||
|
||
int main(int argc, char **argv) { | ||
auto arg = 99; | ||
ASSERT(arg == ?, "arg should be ?"); | ||
std::cout << "befor func call: " << arg << std::endl; | ||
func(arg); | ||
ASSERT(arg == ?, "arg should be ?"); | ||
std::cout << "after func call: " << arg << std::endl; | ||
return 0; | ||
} | ||
|
||
static void func(int param) { | ||
ASSERT(param == ?, "param should be ?"); | ||
std::cout << "befor add: " << param << std::endl; | ||
param += 1; | ||
ASSERT(param == ?, "param should be ?"); | ||
std::cout << "after add: " << param << std::endl; | ||
} |
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 |
---|---|---|
@@ -1,15 +1,16 @@ | ||
#include <iostream> | ||
#include "../exercise.h" | ||
|
||
void func(int param) { | ||
int func(int param) { | ||
static int static_ = param; | ||
std::cout << "static_ = " << static_++ << std::endl; | ||
return static_; | ||
} | ||
|
||
int main(int argc, char **argv) { | ||
func(5); | ||
func(4); | ||
func(3); | ||
func(2); | ||
func(1); | ||
ASSERT(func(5) == ?, "static variable value incorrect"); | ||
ASSERT(func(4) == ?, "static variable value incorrect"); | ||
ASSERT(func(3) == ?, "static variable value incorrect"); | ||
ASSERT(func(2) == ?, "static variable value incorrect"); | ||
ASSERT(func(1) == ?, "static variable value incorrect"); | ||
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
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 |
---|---|---|
@@ -1,15 +1,19 @@ | ||
#include <iostream> | ||
#include "../exercise.h" | ||
|
||
unsigned long long fibonacci(int i) { | ||
static unsigned long long cache[128]{0, 1}, next = 2; | ||
for (; next <= i; ++next) { | ||
// TODO: 改正函数实现,实现正确的缓存优化斐波那契计算 | ||
static unsigned long long fibonacci(int i) { | ||
// TODO: 为缓存设置正确的初始值 | ||
static unsigned long long cache[128], next = 2; | ||
// TODO: 设置正确的循环条件 | ||
for (;; ++next) { | ||
cache[next] = cache[next - 1] + cache[next - 2]; | ||
} | ||
return cache[i]; | ||
} | ||
|
||
int main(int argc, char **argv) { | ||
auto fib100 = fibonacci(100); | ||
ASSERT(fib100 == 3736710778780434371, "fibonacci(100) should be 3736710778780434371"); | ||
std::cout << "fibonacci(100) = " << fib100 << std::endl; | ||
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,44 @@ | ||
#ifndef __EXERCISE_H__ | ||
#define __EXERCISE_H__ | ||
|
||
#include <iostream> | ||
|
||
template<class T> | ||
struct Colorful { | ||
int color; | ||
T value; | ||
}; | ||
|
||
template<class T> | ||
inline std::ostream &operator<<(std::ostream &os, Colorful<T> const &cf) { | ||
return os << "\x1b[" << cf.color << 'm' << cf.value << "\x1b[0m"; | ||
} | ||
|
||
#define COLORFUL(VALUE, NAME) \ | ||
template<class T> \ | ||
inline Colorful<T> NAME(T &&val) { \ | ||
return {(VALUE), std::forward<T>(val)}; \ | ||
} | ||
|
||
COLORFUL(31, red) | ||
COLORFUL(32, green) | ||
COLORFUL(33, yellow) | ||
COLORFUL(34, blue) | ||
|
||
#undef COLORFUL | ||
|
||
#define ASSERT(COND, MSG) \ | ||
if (!(COND)) { \ | ||
std::cerr << red("Assertion failed: ") << std::endl \ | ||
<< std::endl \ | ||
<< #COND << std::endl \ | ||
<< std::endl \ | ||
<< green("Message:") << std::endl \ | ||
<< std::endl \ | ||
<< MSG << std::endl \ | ||
<< std::endl; \ | ||
exit(1); \ | ||
} | ||
|
||
|
||
#endif// __EXERCISE_H__ |