Repository navigation
Expand file tree
/
Copy pathP022.cpp
More file actions
111 lines (111 loc) · 2.74 KB
/
Copy pathP022.cpp
File metadata and controls
111 lines (111 loc) · 2.74 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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
#include <iostream>
#include <fstream>
#include <vector>
#include <string>
#include <algorithm>
using namespace std;
#define MAXNAME 13
struct Text{
char Nome[13];
bool operator<(const Text&b)const{
unsigned int c=0;
do{
if(this->Nome[c]<b.Nome[c]){
return true;
}else if(this->Nome[c]>b.Nome[c]){
return false;
}
}while(++c<MAXNAME);
return false;
}
}Tmp;
unsigned int P022(){
ifstream File("P022.txt",ios::in|ios::binary|ios::ate);
if(File.is_open()){
File.seekg (0, ios::beg);
vector<Text>Lista;
unsigned char c;
unsigned int i=0;
do{
if(File.eof()){
c=0;
}else{
File.read ((char*)(&c),sizeof(char));
}
switch(c){
case '"':
break;
case 0:
case ',':
Tmp.Nome[i++]=0;
if(i>=MAXNAME){
throw("Aumente o MAXNAME");
}
Lista.push_back(Tmp);
i=0;
break;
default:
if(c>='A' && c<='Z'){
Tmp.Nome[i++]=c;
}
break;
}
}while(c);
sort(Lista.begin(), Lista.end());
vector<Text>::iterator iter;
unsigned int Sg=0;
for(iter=Lista.begin(),i=1;iter!=Lista.end();iter++,i++){
unsigned int j=0,S=0;
do{
S+=iter->Nome[j]-'A'+1;
}while(iter->Nome[++j]);
Sg+=S*i;
}
File.close();
return Sg;
}else{
throw("Arquivo não existe");
return 0;
}
}
/*
#define MAXNAME 13
struct Text{
char*Nome;
bool operator<(const Text&b)const{
unsigned int c=0;
do{
if(this->Nome[c]<b.Nome[c]){
return true;
}else if(this->Nome[c]>b.Nome[c]){
return false;
}
}while(++c<MAXNAME);
return false;
}
}Names[]={
#include "P022.txt"
};
unsigned int P022(){
unsigned int i;
vector<Text>Lista;
for(i=0;i<sizeof(Names)/sizeof(Text);i++){
Lista.push_back(Names[i]);
}
sort(Lista.begin(), Lista.end());
vector<Text>::iterator iter;
unsigned int Sg=0;
for(iter=Lista.begin(),i=1;iter!=Lista.end();iter++,i++){
unsigned int j=0,S=0;
do{
S+=iter->Nome[j]-'A'+1;
}while(iter->Nome[++j]);
Sg+=S*i;
}
return Sg;
}
*/
int main() {
cout<<P022()<<endl;
return 0;
}