某同學幫國外某MM做的題。
演算法描述:
首先將兩個字串和一個臨界值作為參數傳入函數,比如"aaaaaaaaaa","bbaaababaa",2,然後在函數中依次比較兩個字串的每個字元,當不同的字元數超過指定的臨界值後,繼續比較,並將大於等於臨界值且最大數目的第一個字串中的相同字元轉換為大寫,比如,根據以上輸入,那麼輸出為:aaAAAaaaAA。要求:只能用if,else,遞迴及字串處理的相關函數。
歡迎各種拍磚。
//Code By Pnig0s1992 //Date:2012,3,13 #include <stdio.h> #include <Windows.h> #include <string.h> #include <stdlib.h> #define MAXSIZE 20 int SuperStr(CHAR lpFirst[],CHAR lpLast[],int iCheck); int main(int argc,char * argv[]) { CHAR lpFirst[] = "aaaaaaaaaa"; CHAR lpSecond[] = "bbaaababaa"; int iCheck = 2; SuperStr(lpFirst,lpSecond,iCheck); printf("n修改後的第一條字串:%s",lpFirst); system("pause"); return 0; } int SuperStr(CHAR lpFirst[],CHAR lpLast[],int iCheck) { CHAR cTempF = *lpFirst; CHAR cTempL = *lpLast; int iTempMark; static CHAR cSave; static LPSTR lpTempStr = (LPSTR)HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,20); static int iCount = 0; int iReturn = 0; if(cTempF == '[]' || cTempL == '[]') { return 0; } int iSame = SuperStr(++lpFirst,++lpLast,iCheck); if(cTempF == cTempL) { cTempF-=32; cSave = cTempF; iCount++; iReturn = 1; }else { if(iCount<iCheck) iCount =0; } if(iCount >=iCheck) { if(iCount == 1) { strnset(lpFirst-1,cTempF,iCount); iCount = 0; return 0; }else { if(iSame == 0) { strnset(lpFirst+1,cSave,iCount); iCount = 0; } } } return iReturn; }
本文出自 “About:Blank H4cking” 部落格,請務必保留此出處http://pnig0s1992.blog.51cto.com/393390/805284