-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCharCodeCalculation.cpp
More file actions
64 lines (55 loc) · 1.15 KB
/
CharCodeCalculation.cpp
File metadata and controls
64 lines (55 loc) · 1.15 KB
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
54
55
56
57
58
59
60
61
62
63
64
#include<iostream>
#include<algorithm>
#include<cmath>
#include<vector>
#include <string>
#include <iostream>
using namespace std;
int calc(const string& x)
{
string result1;
string result2;
for (char s : x)
{
result1 += to_string(static_cast<int>(s));
}
for (char re1 : result1)
{
if (re1 == '7')
{
re1 = '1';
}
result2 += re1;
}
int sumresult1 = 0;
int sumresult2 = 0;
for (char ch : result1) {
sumresult1 += ch - '0';
}
for (char ch : result2) {
sumresult2 += ch - '0';
}
int result = sumresult1 - sumresult2;
return result;
}
int main() {
cout << calc("FVJFVDF");
return 0;
}
/*Description:
Given a string, turn each character into its ASCII character code and join them together to create a number - let's call this number total1:
'ABC' --> 'A' = 65, 'B' = 66, 'C' = 67 --> 656667
Then replace any incidence of the number 7 with the number 1, and call this number 'total2':
total1 = 656667
^
total2 = 656661
^
Then return the difference between the sum of the digits in total1 and total2:
(6 + 5 + 6 + 6 + 6 + 7)
- (6 + 5 + 6 + 6 + 6 + 1)
-------------------------
6
Fundamentals
Arrays
Strings
Mathematics*/