Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit b6d20d9

Browse files
Add C++ implementation
Signed-off-by: begeekmyfriend <begeekmyfriend@gmail.com>
1 parent b1c5b12 commit b6d20d9

File tree

3 files changed

+41
-0
lines changed

3 files changed

+41
-0
lines changed

‎0991_broken_calculator/Makefile‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
all:
2+
gcc -O1 -o test calculator.c

‎0991_broken_calculator/calculator.c‎

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
#include <stdio.h>
2+
#include <stdlib.h>
3+
4+
5+
int brokenCalc(int X, int Y)
6+
{
7+
int step = 0;
8+
while (X < Y) {
9+
Y = Y & 1 ? Y + 1 : Y / 2;
10+
step++;
11+
}
12+
step += X - Y;
13+
return step;
14+
}
15+
16+
int main(int argc, char **argv)
17+
{
18+
if (argc != 3) {
19+
fprintf(stderr, "Usage: ./test x y");
20+
exit(-1);
21+
}
22+
23+
int x = atoi(argv[1]);
24+
int y = atoi(argv[2]);
25+
printf("%d\n", brokenCalc(x, y));
26+
return 0;
27+
}

‎0991_broken_calculator/calculator.cc‎

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
class Solution {
2+
public:
3+
int brokenCalc(int X, int Y) {
4+
int step = 0;
5+
while (X < Y) {
6+
Y = Y & 1 ? Y + 1 : Y / 2;
7+
step++;
8+
}
9+
step += X - Y;
10+
return step;
11+
}
12+
};

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /