AARC'98 帕金森病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個值表示一些相互關聯的症狀,並表示為三角形的三個角。第一個字母值是第一個三角形的第一個角,第二個值是第二個三角形的第一個角……
然後根據預定義的模式計算每個三角形邊的長度:
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
$BCD$HJUY$Q
ABCDEFGH
A
AFW#GFDGFD
ABCD$EFG KP
AAABBACBA示例輸出
The data sequence ABCD is invalid.
$BCD$HJUY$Q signals that patient has Parkinson disease.
The data sequence ABCDEFGH is invalid.
The data sequence A
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.筆記
這些字母定義了類似的邊緣拳頭,意思是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; }