ACM 问题 (AARC'98) - Parkinson
解决旧的 ACM 问题
引言
这段代码是我在学习时编写的。我当时正在准备 ACM 考试,并解决了一些问题。解决这些问题已经有一段时间了,代码并不完美,但它能够解决问题。
背景
ACM 是一个很大的问题,它取决于解决问题的算法技能。 它需要 C、C++ 或 Java 来解决实际问题,并且需要一些 IO 来接收输入并产生输出。
问题陈述
Parkinson
Source file : parkinson.{c|cpp}
Input file: parkinson.in
Output file: parkinson.out
在医疗诊断中,出现许多关于将症状正确分配给特定疾病的问题。
在一项临床研究中,一些专门研究帕金森病的神经科医生为每位患者的 9 种帕金森症状(手臂摆动、言语、独立性等)分配了一个字母值(A、B、C..)。
每个患者记录都是一个包含 9 个字母值的序列。 $ 符号包含在记录的各个位置,以使其更易于阅读,但没有其他意义。
下面显示的示例输入和预期输出说明了许多有效和一些无效的记录格式。
设置一个算法来检查每个患者基于患者记录(9 个值的集合)的疾病发展情况。 3 组 3 个值代表一些相互关联的症状,并表示为三角形的 3 个角。 第一个字母值是第一个三角形的第一个角,第二个值是第二个三角形的第一个角 ...
然后根据预定义的模式计算每个三角形边的长度
A 和 B 之间的单位步长为 1,
B 和 C 之间为 2,
C 和 D 之间为 3,
D 和 E 之间为 4,
...
A 和 G 之间为 21。
然后使用每个三角形的边来计算三角形的值。
Value of the triangle = (edge1)² + (edge2)² + (edge3)² + 1
然后将症状组的 3 个值加起来。如果最终值是素数,则认为患者患有帕金森病。 否则,患者身体健康。
为了更好地理解该过程,请考虑正确的患者记录 A$AA$$BB$AC$BA。 首先查看症状组,然后查看执行的计算

该记录的最终值为 19,这是一个素数; 因此表明该患者患有帕金森病。
输入文件
示例输入文件每行包含一条数据记录,可能前面和/或后面有空格。 没有一行可以包含超过 70 个字符,但数据记录可能包含非法字符,并且字母值可能多于或少于 9 个。 输入文件由文件结尾终止。
输出文件
输出文件应包括数据记录的显示,如果是无效记录,则应显示无效记录的声明。 如果记录合法,则输出应指定患者身体健康还是患有帕金森病。
示例输入
ABCD
ABCDEFGH
A$$$BCD$HJUY$Q
AFW#GFDGFD
ABCD$EFG$$KP
AAABBACBA
示例输出
The data sequence ABCD is invalid.
The data sequence ABCDEFGH is invalid.
The data sequence A$$$BCD$HJUY$Q signals that patient has Parkinson disease.
The data sequence AFW#GFDGFD is invalid.
The data sequence ABCD$EFG$$KP signals that patient is in good health.
The data sequence AAABBACBA signals that patient has Parkinson disease.
代码和解释
注释
- $ 字符没有任何意义。
- 过滤掉 $ 后,生成的字符串的长度必须恰好为 9,因为它将给出三个三角形的边。
- 字母首先定义相似的边,意味着 AFD => 三角形 1 的第一条边是 A,三角形 2 的第一条边是 F,依此类推。
- 由此:A 和 B 之间的单位步长为 1,B 和 C 之间为 2,C 和 D 之间为 3,D 和 E 之间为 4。 显然,字母之间的步长正在增加,意味着 A-B 是 1,但 B-C 是 2,因此 A-C 是 1 + 2 = 3。
- 我们想要的值是角之间差异的平方和。
- 如果这个数字是素数,那么患者患有帕金森病。
代码如下
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
//defining the letters to check if the letters is in alphabet or not.
//[I know there is simpler way,
//but I've done this when I wasn't so good in C]
char stepsValues[27]={'A','B','C','D','E','F','G','H','I','J','K','L','M'
,'N','O','P','Q','R','S','T','U','V','W','X','Y','Z','$'};
//Computes the steps and the differences between them.
//Using the algorithm of the math for cumulative sum : Sn = [(n)*(n+1)]/2
//It gets the sum from 1 to the fist letter and the sum from 1 to the second,
//and subtracts them to get the diff between the 2nd and the 1st.
int GetSteps(int stIndex, int ndIndex)
{
if(stIndex==ndIndex)
return 0;
int stResult = (stIndex*(stIndex+1))/2;
int ndResult = (ndIndex*(ndIndex+1))/2;
return (ndResult - stResult);
}
//Gets the index of the letter.
int IndexOF(char c)
{
for(int i=0;i<26;i++)
{
if(c==stepsValues[i])
return i;
}
return -1;
}
//This function checks if the char is in the permitted chars.
bool IsInChars(char c)
{
for(int i=0;i<27;i++)
if(c == stepsValues[i])
return true;
return false;
}
//This method cleans up the [CORRECT] record and remove any $ from it.
//Cleans up the [CORRECT] record and remove any $ from it.
string ExcludeLetters(char* record)
{
string refinedRecord = "";
for(int i=0;i<strlen(record);i++) rdtriangle="ComputeTriangle(t3_1,t3_2,t3_3);
" ndtriangle="ComputeTriangle(t2_1,t2_2,t2_3);"
sttriangle="ComputeTriangle(t1_1,t1_2,t1_3);"
t3_3="GetSteps(IndexOF(refinedRecord[5]),IndexOF(refinedRecord[8]));"
t3_2="GetSteps(IndexOF(refinedRecord[2]),IndexOF(refinedRecord[8]));"
t3_1="GetSteps(IndexOF(refinedRecord[2]),IndexOF(refinedRecord[5]));"
t2_3="GetSteps(IndexOF(refinedRecord[4]),IndexOF(refinedRecord[7]));"
t2_2="GetSteps(IndexOF(refinedRecord[1]),IndexOF(refinedRecord[7]));"
t2_1="GetSteps(IndexOF(refinedRecord[1]),IndexOF(refinedRecord[4]));"
t1_3="GetSteps(IndexOF(refinedRecord[3]),IndexOF(refinedRecord[6]));"
t1_2="GetSteps(IndexOF(refinedRecord[0]),IndexOF(refinedRecord[6]));"
t1_1="GetSteps(IndexOF(refinedRecord[0]),IndexOF(refinedRecord[3]));"
if((number%i)="=0)" i="0;i<strlen(record);i++)" if(letters.length()!="9)"
letters="ExcludeLetters(record);" +="record[i];"
if(isinchars(record[i])&&!(record[i]="='$'))">>line;
if(!IsCorrect(line))
{
outFile<<"The data sequence " <<line << " is invalid\n";
continue;
}
int result = FinalResult(ExcludeLetters(line));
if(IsPrime(result))
{
outFile<<"The data sequence " <<line << " signals that
the patient has Parkinson disease\n";
continue;
}
else
{
outFile<<"The data sequence " <<line << " signals that
the patient is in good health\n";
continue;
}
}
return 0;
}
最后
这个问题是第一个,我将撰写有关我设法解决的下一个问题的文章。 谢谢。
历史
- 2008 年 6 月 16 日:首次发布