-
Notifications
You must be signed in to change notification settings - Fork 0
/
day1.cpp
53 lines (49 loc) · 943 Bytes
/
day1.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
#include <iostream>
#include <cstring>
using namespace std;
const int MAXLENGTH = 10000;
int captcha(char* s)
{
int sum = 0;
char* p = s;
while (*p != '\0')
{
if (*p == *(p+1))
sum += *p - '0';
if (*(p+1) == '\0' && *p == *s)
sum += *p - '0';
p++;
}
return sum;
}
int captcha2(char* s)
{
int sum = 0;
char* p = s;
int halfway = strlen(s) / 2;
while (*p != '\0')
{
char* q = p;
for (int i = 0; i < halfway; i++)
{
if (*(q + 1) != '\0')
q++;
else
q = s;
}
if (*p == *q)
sum += *p - '0';
p++;
}
return sum;
}
int main()
{
for (;;)
{
char s[MAXLENGTH];
cout << "Input some numbers:" << endl;
cin.getline(s, MAXLENGTH);
cout << "The sum is " << captcha2(s) << "." << endl << endl;
}
}