using System; using System.Collections.Generic; using System.Linq; using System.Diagnostics; public static class SumOfMultiples { public static int Main() { Profile("Justin's implementation, find multiples of 3 and 5 up to 1M, 10 times\n", 10, () => { JSum(new[] { 2, 3, 5, 7, 11, 13 }, 1000000); } ); Console.WriteLine($"Justin's result == {JSum(new[] { 2, 3, 5, 7, 11 }, 1000000)}"); Profile("Nick's implementation, find multiples of 3 and 5 up to 1M, 10 times\n", 10, () => { NSum(new[] { 2, 3, 5, 7, 11, 13 }, 1000000); } ); Console.WriteLine($"Nicks's result == {NSum(new[] { 2, 3, 5, 7, 11 }, 1000000)}"); return 0; } public static long JSum(IEnumerable<int> multiples, int max) { long sum = 0; for(int i = 0; i < max; i++) { if(multiples.Any(x => i % x == 0)) { sum += i; } } return sum; } public static long NSum(IEnumerable<int> multiples, int max) { HashSet<long> result = new HashSet<long>(); foreach (int mult in multiples) { for (int i = 1; i < max; i++) { if ((i % mult == 0) && !result.Contains(i)) { result.Add((long)i); } } } return result.Sum(); } static double Profile(string description, int iterations, Action func) { // warm up func(); var watch = new Stopwatch(); // clean up GC.Collect(); GC.WaitForPendingFinalizers(); GC.Collect(); watch.Start(); for (int i = 0; i < iterations; i++) { func(); } watch.Stop(); Console.Write(description); Console.WriteLine(" Time Elapsed {0} ms", watch.Elapsed.TotalMilliseconds); return watch.Elapsed.TotalMilliseconds; } }
using System.IO; using System; class A { public void Hi() { Console.WriteLine("Hi"); } } class B:A{ } class C:A{ } class Program { static void Main() { A a = new A(); B b = new B(); Object o = new Object(); b.Hi(); a.Hi(); // b =a ; o = (object)b; a = (B)o; a.Hi(); b.Hi(); } }
// Fig. 8.9: Student Poll.cs // Poll analysis app. using System; class StudentPoll { static void Main() { // student response array (more typically, input at runtime) int[] responses = {1,2,5,4,3,5,2,1,3,13,1,4,3,3,3,2,3,3,2,14}; var frequency = new int[6]; // array of frequency counters int index = -1; // for each answer, selet responses element and use that value // as frequency index to determine element to increment foreach (var response in responses) { ++index; try { ++frequency[response]; } catch (IndexOutOfRangeException ex) { Console.WriteLine(ex.Message); Console.WriteLine( $"responses[{index}] = {responses[index]}\n"); } } Console.WriteLine($"{"Rating"}{"Frequency",10}"); // output each array element's value for (var rating = 1; rating < frequency.Length; ++rating) { Console.WriteLine($"{rating,6}{frequency[rating],10}"); } } }
using System.IO; using System; class Program { public int a,b,c; static void Main() { c=a+b; Console.WriteLine("c"); } }
using System; using System.Net; using System.Text; // for class Encoding using System.IO; // for StreamReader class Program { static void Main() { var request = (HttpWebRequest) WebRequest.Create("https://www.smsgatewaycenter.com/library/send_sms_community.php"); var postData = "&UserName=sgcsms2"; postData += "&Mask=SGCSMS"; postData += "&To=7770907999"; postData += "&Type=Bulk"; postData += "&Password=SGCsms123?"; postData += "&Language=English"; postData += "&Message=Dear Parents, MARKS OF Ajay Yadav OF WEEKLY TEST I IS BELOW:\nSUB MM OM\nENGLISH 100 50 \nHindi 100 2 \nMathematics 100 90 \nSCIENCE 100 1 \nTOT MARKS: 400 MARKS OBT: 310\nPER(%): 77.5% CLASS RANK: 2\n Regards, Adarsh School Merta City"; postData += "&format=json"; var data = Encoding.ASCII.GetBytes(postData); request.Method = "POST"; request.ContentType = "application/x-www-form-urlencoded"; request.ContentLength = data.Length; using(var stream = request.GetRequestStream()) { stream.Write(data, 0, data.Length); } var response = (HttpWebResponse) request.GetResponse(); var responseString = new StreamReader(response.GetResponseStream()).ReadToEnd(); Console.WriteLine(responseString); } }
using System.IO; using System; using System; using System.Collections.Generic; namespace SortedArray { class Finder { //Array size private int n = 3; //Used to save location of the search result public int resx = -1; public int resy = -1; //Two dimensional array private int[,] A; //Constructor public Finder() { //Sample two dimensional array A = new int[3, 3] { { 1, 2, 3 }, { 4, 5, 6 }, { 7, 8, 9 } }; } //Reset results public void Reset() { resx = -1; resy = -1; } //Find method using brute force public bool BruteSearch(int x) { //Loop through the entire array for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { if (A[i, j] == x) { //Save result for later access resx = i; resy = j; return true; } } } //If we reach this point then the array //does not contain the search key return false; } //This method searches a given row i using //binary search. l and r are left and right //indices because we are going to call this //method recursively. x the is the search key public bool BinarySearch(int i, int l, int r, int x) { //Single element base case if (l == r) { //Element found if (x == A[i, l]) { //Save results resx = i; resy = l; return true; } else { return false; } } else { //Find the middle element of the given row int m = (l + r) / 2; //Return if the middle element happens to be //the key we are looking for if (A[i, m] == x) { //Save result resx = i; resy = m; return true; } //If the key is greater than the middle //then search the right half else if (x > A[i, m]) { l = m + 1; } //Search the left half else { r = m - 1; } //Call recursively return BinarySearch(i, l, r, x); } } } class Program { static void Main(string[] args) { //Create a finder object Finder finder = new Finder(); //Search for 3 using brute force if (finder.BruteSearch(3)) { Console.Out.WriteLine("Element found:"); Console.Out.WriteLine("Row: " + finder.resx); Console.Out.WriteLine("Column: " + finder.resy); } else { Console.Out.WriteLine("Element not found"); } //Reset results to start again using //a different method finder.Reset(); //Binary search each row for (int i = 0; i < 3; i++) { if (finder.BinarySearch(i, 0, 2, 3)) { Console.Out.WriteLine("Element found:"); Console.Out.WriteLine("Row: " + finder.resx); Console.Out.WriteLine("Column: " + finder.resy); return; } } Console.Out.WriteLine("Element not found"); } } }
using System; using System.Net; using System.Text; // for class Encoding using System.IO; // for StreamReader class Program { static void Main() { var request = (HttpWebRequest) WebRequest.Create("https://203.112.144.87:8080/SMSApi/rest/send"); var postData = "&userId=YourUsername"; postData += "&senderId=SGCSMS"; postData += "&mobile=993xxxxxxx"; postData += "&sendMethod=simplemsg"; postData += "&password=******"; postData += "&msgType=TEXT"; postData += "&msg=Test Message"; postData += "&format=json"; var data = Encoding.ASCII.GetBytes(postData); request.Method = "POST"; request.ContentType = "application/x-www-form-urlencoded"; request.ContentLength = data.Length; using(var stream = request.GetRequestStream()) { stream.Write(data, 0, data.Length); } var response = (HttpWebResponse) request.GetResponse(); var responseString = new StreamReader(response.GetResponseStream()).ReadToEnd(); Console.WriteLine(responseString); } }
We use cookies to provide and improve our services. By using our site, you consent to our Cookies Policy. Accept Learn more