Ëã·¨Éè¼ÆÓë·ÖÎö£¨µÚ2°æ£© Íõºì÷ ºúÃ÷ ϰÌâ´ð°¸

ϰÌâ3

1£® ¼ÙÉèÔÚÎı¾\ÖвéÕÒģʽ\£¬Ð´³ö·Ö±ð²ÉÓÃBFËã·¨ºÍKMP

Ëã·¨µÄ´®Æ¥Åä¹ý

//BFËã·¨

#include using namespace std;

int BF(char S[], char T[]) {

int index = 0; int i = 0, j = 0; while ((S[i] != '\\0') && (T[j] != '\\0')) {

if (S[i] == T[j]) { i++; j++; } else { ++index; i = index; j = 0; } }

if (T[j] == '\\0') return index + 1; else return 0; }

int main() { char s1[19]=\

char s2[7]=\

cout<< BF( s1, s2) <

//KMPËã·¨

#include using namespace std;

void GetNext(char T[ ], int next[ ]) //ÇóģʽTµÄnextÖµ {

int i, j, len; next[0] = -1;

for (j = 1; T[j]!='\\0'; j++) //ÒÀ´ÎÇónext[j] {

for (len = j - 1; len >= 1; len--) //ÏàµÈ×Ó´®µÄ×î´ó³¤¶ÈΪj-1 { for (i = 0; i < len; i++) //ÒÀ´Î±È½ÏT[0]~T[len-1]ÓëT[j-len]~T[j-1] if (T[i] != T[j-len+i]) break; if (i == len) { next[j] = len; break; } }//for if (len < 1)

next[j] = 0; //ÆäËûÇé¿ö£¬ÎÞÏàµÈ×Ó´® }//for }

int KMP(char S[ ], char T[ ]) //ÇóTÔÚSÖеÄÐòºÅ {

int i = 0, j = 0;

int next[80]; //¼Ù¶¨Ä£Ê½×Ϊ80¸ö×Ö·û GetNext(T, next);

while (S[i] != '\\0' && T[j] != '\\0') {

if (S[i] == T[j]) {

i++; j++; }

else { j = next[j]; if (j == -1) {i++; j++;}

} }

if (T[j] == '\\0') return (i - strlen(T) +1); //·µ»Ø±¾ÌËÆ¥ÅäµÄ¿ªÊ¼Î»Öà else return 0; }

int main() { char s1[]=\ char s2[]=\

cout<

return 0; }

2.·Öʽ»¯¼ò¡£Éè¼ÆËã·¨£¬½«Ò»¸ö¸ø¶¨µÄÕæ·ÖÊý»¯¼òΪ×î¼ò·ÖÊýÐÎʽ¡£ÀýÈ磬½«6/8»¯¼òΪ3/4¡£

#include using namespace std;

int main() {

int n;//·Ö×Ó int m;//·Öĸ

int factor;//×î´ó¹«Òò×Ó int factor1;

cout<<\ÊäÈëÒ»¸öÕæ·ÖÊýµÄ·Ö×ÓÓë·Öĸ£º \ cin>>n>>m;

int r = m % n;//ÒòΪÊÇÕæ·ÖÊý ËùÒÔ·Öĸһ¶¨´óÓÚ·Ö×Ó factor1=m; factor=n; while (r != 0) {

factor1 =factor; factor = r;

r = factor1% factor; }

cout<<\Êä³ö¸ÃÕæ·ÖÊýµÄ×î¼ò·ÖÊý£º \return 0; }

3. Éè¼ÆËã·¨£¬ÅжÏÒ»¸ö´óÕûÊýÄÜ·ñ±»11Õû³ý¡£¿ÉÒÔͨ¹ýÒÔÏ·½·¨£º½«¸ÃÊýµÄÊ®½øÖƱíʾ

´ÓÓÒ¶Ë¿ªÊ¼£¬Ã¿Á½Î»Ò»×é¹¹³ÉÒ»¸öÕûÊý£¬È»ºó½«ÕâЩÊýÏà¼Ó£¬ÅÐ¶ÏÆäºÍÄÜ·ñ±»11Õû³ý¡£ÀýÈ磬½«562843748·Ö¸î³É5,62,84,37,48£¬È»ºóÅжÏ(5+62+84+37+48)ÄÜ·ñ±»11Õû³ý //½«Ò»¸ö´óÕûÊý¿´³ÉÒ»¸öÊý×é

//Êý×éµÄÆæÊýλ¶ÔÓ¦ÊýµÄ10±¶¼ÓÉÏÊý×éżÊý¶ÔÓ¦ÊýµÄ±¾Éí //ÑéÖ¤½á¹ûÄÜ·ñ±»11Õû³ý

#include using namespace std;

int main() { int a[9]={5,6,2,8,4,3,7,4,8}; int result=0; //resultΪÌâĿҪÇóµÄ¸÷λ֮ºÍ for(int i=0;i!=9;++i) { if(i%2==0) result+=a[i]; //i ΪżÊýλʱ£¬½á¹û¼ÓÉÏÆä¶ÔÓ¦Êý×éÊýµÄ±¾Éí else result+=a[i]*10; //i ÎªÆæÊýλʱ£¬½á¹û¼ÓÉ϶ÔÓ¦Êý×éÊýµÄ10±¶ } if(result==0) cout<<\¸ÃÕûÊýÄܱ»11Õû³ý\ else

cout<<\¸ÃÕûÊý²»Äܱ»11Õû³ý\ return 0; }

4. Êý×ÖÓÎÏ·¡£°ÑÊý×Ö 1,2,?,9Õâ9¸öÊý×ÖÌîÈëÒÔϺ¬Óмӡ¢¼õ¡¢³Ë¡¢³ýµÄËÄÔòÔËËãʽÖУ¬Ê¹µÃ¸ÃµÈʽ³ÉÁ¢¡£ÒªÇó9¸öÊý×Ö¾ù³öÏÖÒ»´ÎÇÒ½ö³öÏÖÒ»´Î£¬ÇÒÊý×Ö1²»ÄܳöÏÖÔڳ˺ͳýµÄһλÊýÖУ¨¼´ÅųýÔËËãʽÖÐһλÊýΪ1µÄƽ·²ÇéÐΣ©¡£

??¡Á?£«???¡Â?£­?? = 0

ÁªÏµ¿Í·þ£º779662525#qq.com(#Ìæ»»Îª@)