介绍
我在学习的时候写了这段代码。我正在测试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个值表示一些相互关联的症状,并表示为三角形的三个角。第一个字母值是第一个三角形的第一个角,第二个值是第二个三角形的第一个角……
然后根据预定义的模式计算每个三角形边的长度:
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
例
这个记录的最终值是19,这是一个素数; 从而表明患者具有帕金森病。
- 输入文件
示例输入文件每行包含一个数据记录,可能在空格之前和/或之后。没有行可能包含超过70个字符,但数据记录可能包含非法字符,并且多于或少于9个字母值。输入文件被文件结尾终止。
- 输出文件
如果是这样的话,输出文件应该包括数据记录的显示和无效记录的记录。如果记录是合法的,则输出结果应该说明患者是否身体健康,还是具有帕金森病。
示例输入
ABCD
ABCDEFGH
A$BCD$HJUY$QKP
AFW#GFDGFD
ABCD$EFG
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.KP signals that patient is in good health.
The data sequence AFW#GFDGFD is invalid.
The data sequence ABCD$EFG
The data sequence AAABBACBA signals that patient has Parkinson disease.笔记
这些字母定义了类似的边缘拳头,意思是AFD => triangle1的第一个边缘是A,triangle2的第一个边缘是F,依此类推。
由此可见:A与B之间的单位步长是1,B与C之间是2,C与D之间是3,D与E之间是4.很明显,字母之间的间隔越来越大,意味着AB是1 ,但是BC是2,所以AC是1 + 2 = 3。
我们想要的价值是角落之间的差异的平方的总和。
如果这个数字是主要的,那么病人有Parkinsons病。
代码如下:
#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; }