פורסם 2009 בינואר 516 שנים מתוך שיעורי בית: קבל משתנה מסוג STRING חפש את כל אפשוריות הפלינדרום שבו (פלינדרום זה -abcba , 123321) הדפס את הפלינדרום הארוך ביותר ואת אורכו. אני חושב שהצלחתי חלקית את התרגיל - אבל יש לי בעיה לגבי מערך סטריגנים, איך מגדירים את האורך של המערך בלי לדעת מה יהיה האורך שלו ? כי האורך תלוי במספר החלקים בסטרינג בהם יש שני תווים זהים , כמו [glow=red,2,300]A[/glow]vnjxcvhjxcv[glow=red,2,300]A[/glow] עריכה: הצלחתי , הבעיה היתה כשהאורך של המערך הראשון לא הספיק לאורך של המערך השני (שנשלח לבדיקה) , אבל אם מכפילים את האורך בריבוע האורך של המערך הראשוני (כי אלו סה"כ האפשרויות) אז זה עובד. אשמח לאפשוריות לייעל את התוכנית , כי כרגע היא לא ממש יעילה using System;using System.Collections.Generic;using System.Text;namespace ConsoleApplication1{ class Program { public static bool Palindrom(string str) { for (int i = 0; i < str.Length / 2; i++) { if (str[i] != str[str.Length - 1 - i]) return false; } return true; } public static string[] Check(string str) { int counter = 0; string sstr; string[] sstrArr = new string[str.Length]; for (int i = 0; i < str.Length - 1; i++) { for (int j = (1 + i); j < str.Length; j++) { if (str[i] == str[j]) { sstr = str.Substring(i, (j - i) + 1); Console.WriteLine(sstr); if(counter < str.Length) { sstrArr[counter] = sstr; counter++; } } } } return sstrArr; } static void Main(string[] args) { Console.WriteLine("Palindrom- Inster String"); string str = Console.ReadLine(); string[] strAAr = Check(str); bool flag; int longestL=0,L, Position=0; for (int i = 0; i < strAAr.Length; i++) { if (strAAr[i] != null) { flag=Palindrom(strAAr[i]); if (flag) { L = (strAAr[i]).Length; if (L > longestL) { longestL = L; Position = i; } } Console.WriteLine(strAAr[i]); } } if (longestL != 0) Console.WriteLine("The Longest Palidrom is: '{0}' it's length is {1}", strAAr[Position], longestL); else Console.WriteLine("NO PALINDROM FOUND!"); } }}
פורסם 2009 בינואר 516 שנים זו לא "פקודה", זה טיפוס.http://msdn.microsoft.com/en-us/library/6sh2ey19.aspx
ארכיון
דיון זה הועבר לארכיון ולא ניתן להוסיף בו תגובות חדשות.