How can i make a matrix as the one seen below, but with simply 1 for loop (instead of two):
1 2 3 4 5 6 7 8
9 10 11 12 13 14 15 16
17 18 19 20 21 22 23 24
25 26 27 28 29 30 31 32
33 34 35 36 37 38 39 40
41 42 43 44 45 46 47 48
49 50 51 52 53 54 55 56
57 58 59 60 61 62 63 64
Ive made the one above using 2 nested for loops, the first to create the rows and the second for the columns. How can i achieve the same result but with one for loop? Preferably implementing the / and % operators.
It is enough to have a loop from 1 to 63. Divide by 8 for the row and calculate the remainder by 8 for the column (plus one is because the loop starts from zero):
int[,] matrix= new int[8,8];
for (int i = 0; i <= 63; i++)
matrix[i/8,i%8] = i+1;
Related
I have this string
1 3 9 13 38 40 52 54 59 98 107 113:274:8 8 9 5 20 5 25 14 5 10 10
I need to split numbers so use this code
string[] lineElements = lines[i].Split(' ');
but I don't want numbers after first " : "
I only want to split this
1 3 9 13 38 40 52 54 59 98 107 113
what can I do?
You already have the answer, use String.Split
string[] lineElements = lines[i].Split(':')[0].Split(' ');
You may have issues if the string does not contain :, but I do not think so since you only want the first string.
I am writing code for an embedded device, I have a map of numbers, shown below is a subset ranging from 151 to 66, the adjacent numbers are linked numbers, so for 151, its linked numbers would be 150 - 148 - 146 - 136 - 131 - 91 - and so on, I have a method which accepts two parameters int startNum and int endNum and the goal is to return an array containing 2 integers which will link me from startNum to endNum, so for an example see below
startNum = 126
endNum = 75
linked nums would be 105,90, since 126 brings me to 105, 105 brings me to 90 and 90 takes me to 75, and there are several other paths which will do this, my question is is it possible to figure out these linked numbers without using nested for loops and testing every possible combination.
151 = 150 - 148 - 146 - 136 - 131 - 91 -
150 = 149 - 147 - 145 - 135 - 130 - 90 -
149 = 148 - 146 - 144 - 134 - 129 - 89 -
148 = 147 - 145 - 143 - 133 - 128 - 88 -
147 = 146 - 144 - 142 - 132 - 127 - 87 -
146 = 143 - 139 - 137 - 127 - 125 - 89 -
145 = 144 - 142 - 140 - 130 - 125 - 85 -
144 = 143 - 141 - 139 - 129 - 124 - 84 -
143 = 142 - 140 - 138 - 128 - 123 - 83 -
142 = 141 - 139 - 137 - 127 - 122 - 82 -
141 = 140 - 138 - 136 - 126 - 121 - 81 -
140 = 139 - 137 - 135 - 125 - 120 - 80 -
139 = 136 - 132 - 130 - 120 - 118 - 82 -
138 = 137 - 135 - 133 - 123 - 118 - 78 -
137 = 136 - 134 - 132 - 122 - 117 - 77 -
136 = 135 - 133 - 131 - 121 - 116 - 76 -
135 = 134 - 133 - 132 - 131 - 130 - 129 -
134 = 133 - 131 - 129 - 119 - 114 - 74 -
133 = 132 - 130 - 128 - 118 - 113 - 73 -
132 = 131 - 130 - 129 - 128 - 127 - 126 -
131 = 130 - 128 - 126 - 116 - 111 - 71 -
130 = 129 - 127 - 125 - 115 - 110 - 70 -
129 = 126 - 122 - 120 - 110 - 108 - 72 -
128 = 127 - 125 - 124 - 116 - 110 - 74 -
127 = 126 - 124 - 122 - 112 - 107 - 67 -
126 = 123 - 119 - 117 - 107 - 105 - 69 -
125 = 124 - 123 - 122 - 121 - 120 - 119 -
124 = 123 - 121 - 119 - 109 - 104 - 64 -
123 = 120 - 116 - 114 - 104 - 102 - 66 -
122 = 121 - 119 - 118 - 110 - 104 - 68 -
121 = 120 - 118 - 116 - 106 - 101 - 61 -
120 = 119 - 117 - 115 - 105 - 100 - 60 -
119 = 116 - 112 - 110 - 100 - 98 - 62 -
118 = 117 - 115 - 113 - 103 - 98 - 58 -
117 = 116 - 114 - 112 - 102 - 97 - 57 -
116 = 115 - 113 - 111 - 101 - 96 - 56 -
115 = 112 - 108 - 106 - 96 - 94 - 58 -
114 = 113 - 111 - 109 - 99 - 94 - 54 -
113 = 112 - 110 - 108 - 98 - 93 - 53 -
112 = 111 - 109 - 107 - 97 - 92 - 52 -
111 = 108 - 104 - 102 - 92 - 90 - 54 -
110 = 109 - 107 - 105 - 95 - 90 - 50 -
109 = 108 - 106 - 104 - 94 - 89 - 49 -
108 = 105 - 101 - 99 - 89 - 87 - 51 -
107 = 106 - 104 - 102 - 92 - 87 - 47 -
106 = 105 - 103 - 101 - 91 - 86 - 46 -
105 = 104 - 102 - 100 - 90 - 85 - 45 -
104 = 103 - 101 - 99 - 89 - 84 - 44 -
103 = 102 - 100 - 98 - 88 - 83 - 43 -
102 = 101 - 99 - 97 - 87 - 82 - 42 -
101 = 100 - 98 - 96 - 86 - 81 - 41 -
100 = 99 - 97 - 95 - 85 - 80 - 40 -
99 = 96 - 92 - 90 - 80 - 78 - 42 -
98 = 97 - 95 - 93 - 83 - 78 - 38 -
97 = 94 - 90 - 88 - 78 - 76 - 40 -
96 = 95 - 93 - 91 - 81 - 76 - 36 -
95 = 94 - 93 - 92 - 91 - 90 - 89 -
94 = 93 - 92 - 91 - 90 - 89 - 88 -
93 = 92 - 91 - 90 - 89 - 88 - 87 -
92 = 91 - 89 - 87 - 77 - 72 - 32 -
91 = 90 - 89 - 88 - 87 - 86 - 85 -
90 = 89 - 87 - 85 - 75 - 70 - 30 -
89 = 86 - 82 - 80 - 70 - 68 - 32 -
88 = 87 - 85 - 83 - 73 - 68 - 28 -
87 = 85 - 84 - 81 - 78 - 70 - 36 -
86 = 85 - 83 - 82 - 74 - 68 - 32 -
85 = 82 - 78 - 76 - 66 - 64 - 28 -
84 = 83 - 81 - 79 - 69 - 64 - 24 -
83 = 81 - 80 - 77 - 74 - 66 - 32 -
82 = 81 - 80 - 79 - 78 - 77 - 76 -
81 = 78 - 74 - 72 - 62 - 60 - 24 -
80 = 79 - 77 - 75 - 65 - 60 - 20 -
79 = 76 - 72 - 70 - 60 - 58 - 22 -
78 = 77 - 75 - 74 - 66 - 60 - 24 -
77 = 74 - 70 - 68 - 58 - 56 - 20 -
76 = 75 - 73 - 71 - 61 - 56 - 16 -
75 = 73 - 72 - 69 - 66 - 58 - 24 -
74 = 65 - 63 - 60 - 47 - 41 - 32 -
73 = 70 - 66 - 64 - 54 - 52 - 16 -
72 = 65 - 64 - 56 - 51 - 48 - 24 -
71 = 67 - 65 - 59 - 58 - 53 - 32 -
70 = 69 - 67 - 66 - 58 - 52 - 16 -
69 = 67 - 63 - 59 - 54 - 39 - 24 -
68 = 63 - 59 - 56 - 53 - 41 - 32 -
67 = 65 - 64 - 61 - 58 - 50 - 16 -
66 = 60 - 56 - 51 - 48 - 36 - 21 -
I would model these lists of linked numbers as a graph i.e. numbers as nodes/vertices and links between nodes as edges. The first line in your example looks as follows:
151 = 136 - 148 - 146 - 150 - 91 - 131 -
So a node 151 would be connected with a node 136, the node 136 with a node 148, the node 148 with a node 146 etc.
EDIT
After a comment from #cahinton I'm not sure if above interpretation of these lists is correct. Another one is that a node 151 would be connected with nodes: 136, 148, 146, 150...
You can read line by line adding nodes and edges to your graph. Each edge should have the same weight = 1. When you have a final graph you can use Dijkstra's algorithm to find the shortes paths between any two numbers (nodes).
The complexity of the basic implementation of Dijkstra's algorithm is O(N^2) where N - number of nodes. There are also more efficient implementations if you need it. In your case the basic solution should be enough because you will have only 151 - 66 = 85 nodes.
If you don't want to implement everything from the scratch you can use one of many graph libraries. Personally I like QuickGraph There is even a question how to use Dijkstra in this library.
I want to send this hex packet:
00 38 60 dc 00 00 04 33 30 3c 00 00 00 20 63 62
39 62 33 61 36 37 34 64 31 36 66 32 31 39 30 64
30 34 30 63 30 39 32 66 34 66 38 38 32 62 00 06
35 2e 31 33 2e 31 00 00 02 3c
so i build the string:
string packet = "003860dc0000" + textbox1.text+ "00000020" + textbox2.text+ "0006" + textbox3.text;
then "convert" it to ascii:
conn_str = HexString2Ascii(packet);
then i send the packet... but i have this:
00 38 60 **c3 9c** 00 00 04 33 30 3c 00 00 00 20 63
62 39 62 33 61 36 37 34 64 31 36 66 32 31 39 30
64 30 34 30 63 30 39 32 66 34 66 38 38 32 62 00
06 35 2e 31 33 2e 31 00 00 02 3c **0a**
why??
Thank you!
P.S.
the function is:
private string HexString2Ascii(string hexString)
{
byte[] tmp;
int j = 0;
int lenght;
lenght=hexString.Length-2;
tmp = new byte[(hexString.Length)/2];
for (int i = 0; i <= lenght; i += 2)
{
tmp[j] =(byte)Convert.ToChar(Int32.Parse(hexString.Substring(i, 2), System.Globalization.NumberStyles.HexNumber));
j++;
}
return Encoding.GetEncoding(1252).GetString(tmp);
}
EDIT:
if i convert directly in byte, the hex packet in coded as string:
00000000 30 30 33 38 36 30 64 63 30 30 30 30 30 34 33 33 003860dc 00000433
00000010 33 30 33 43 30 30 30 30 30 30 32 30 33 34 33 32 303C0000 00203432
00000020 36 33 36 33 33 35 33 39 33 32 33 34 36 36 33 39 63633539 32346639
00000030 36 33 33 39 33 31 33 39 33 30 33 36 33 33 36 35 63393139 30363365
00000040 33 35 36 33 36 35 36 35 36 35 33 31 33 39 33 38 35636565 65313938
00000050 36 33 33 31 36 34 33 34 36 33 33 30 30 30 30 36 63316434 63300006
00000060 33 35 32 65 33 31 33 33 32 65 33 31 30 30 30 30 352e3133 2e310000
00000070 30 32 33 43 023C
You cannot convert raw binary data to string data and expect things to just work. They are not the same. This is especially true when you mix up your character encodings.
C# characters are not ASCII characters. They are Unicode characters, represented by Unicode code points. When you then turn around and write those characters out, you need to specify what kind of data to write out. When you read your byte array into a string, using Encoding.GetEncoding(1252), you are getting the characters corresponding to code page 1252, in which 0xdc is a Ü.
But when your string is being converted back into bytes to send over the network, it is being written out as UTF-8. In UTF-8, UTF-00DC cannot be encoded as a single byte, since that byte value is used to indicate the start of a multi-byte sequence. Instead, it's encoded as the multi-byte sequence 0xc3 0x9c. As far as C# is concerned, those two values are the same character. (I don't know where that extra 0x0a is coming from, but my guess is an errant line feed from one of your text boxes and/or some other part of your process).
Its not clear what exactly you're trying to do, but I suspect you are converting way too many times for it to work out correctly. If you know the byte sequence you want to send, why not just encode that as a byte[] directly? For example, use a MemoryStream and write the constant bytes you need into it.
To get the values out of your text boxes, your original code to "convert" the string of hex digits into a string of ASCII characters had the right idea. You just need to stop at the point where you have a byte array, since ultimately the byte array is what you want.
public byte[] GetBytesFrom(string hex)
{
var length = hex.Length / 2;
var result = new byte[length];
for (var i = 0; i < length; i++)
{
result[i] = byte.Parse(hex.Substring(i, 2), NumberStyles.HexNumber);
}
return result;
}
// Variable portions of packet structure.
var byte[] segment2 = GetBytesFrom(textbox1.Text);
var byte[] segment4 = GetBytesFrom(textbox2.Text);
var byte[] segment6 = GetBytesFrom(textbox3.Text);
MemoryStream output = new MemoryStream();
output.Write(new[] { 0x00, 0x38, 0x60, 0xdc, 0x00, 0x00 }, 0, 6);
output.Write(segment2, 0, segment2.Length);
output.Write(new[] { 0x00, 0x00, 0x00, 0x20 }, 0, 4);
output.Write(segment4, 0, segment4.Length);
output.Write(new[] { 0x00, 0x06 }, 0, 2);
output.Write(segment6, 0, segment6.Length);
From here, you could use MemoryStream.CopyTo() to copy it to another stream, or MemoryStream.Read() to read the entire packet into a new byte array, or MemoryStream.GetBuffer() to get the underlying buffer (though that last one is rarely what you want -- it includes unused padding bytes)
I want to make an identical sha512 in C# and mysql.
c#
System.Security.Cryptography.SHA512.Create().ComputeHash(System.Text.ASCIIEncoding.ASCII.GetBytes("test"));
C# results Length 64
238 38 176 221 74 247 231 73 170 26 142 227 193 10 233 146 63 97 137 128 119 46 71 63 136 25 165 212 148 14 13 178 122 193 133 248 160 225 213 248 79 136 188 136 127 214 123 20 55 50 195 4 204 95 169 173 142 111 87 245 0 40 168 255
mysql
INSERT INTO users_table (password) VALUES ( SHA2("test", 512));
// password is a BINARY(128)
mysql results Length 128
101 101 50 54 98 48 100 100 52 97 102 55 101 55 52 57 97 97 49 97 56 101 101 51 99 49 48 97 101 57 57 50 51 102 54 49 56 57 56 48 55 55 50 101 52 55 51 102 56 56 49 57 97 53 100 52 57 52 48 101 48 100 98 50 55 97 99 49 56 53 102 56 97 48 101 49 100 53 102 56 52 102 56 56 98 99 56 56 55 102 100 54 55 98 49 52 51 55 51 50 99 51 48 52 99 99 53 102 97 57 97 100 56 101 54 102 53 55 102 53 48 48 50 56 97 56 102 102
I compare them in bytes from,
but the results are different from each other : (
thankful for help!
best regards johan
MySql is returning a hex-encoded string:
As of MySQL 5.5.6, the return value is a nonbinary string in the
connection character set. Before 5.5.6, the return value is a binary
string; see the notes at the beginning of this section about using the
value as a nonbinary string.
You can tell this is happening because C#'s first byte is 238, which is 0xee, and the ASCII code for e is 101 -- MySql's return begins with 101 101. So you just need to turn the hex-encoded string into a binary string again with UNHEX (but only on MySql > 5.5.6).
So to receive identical results in your case:
INSERT INTO users_table (password) VALUES (UNHEX(SHA2("test", 512)));
And using MySql conditional comments (oh the ugliness!) to be portable across versions:
INSERT INTO users_table (password) VALUES (/*!50506 UNHEX(*/SHA2("test",512)/*!50506 )*/);
Hey, been working at Project Euler, and this one is giving me some problems
By starting at the top of the triangle below and moving to adjacent numbers on the row below, the maximum total from top to bottom is 23.
3
7 4
2 4 6
8 5 9 3
That is, 3 + 7 + 4 + 9 = 23.
Find the maximum total from top to bottom of the triangle below:
...
NOTE: As there are only 16384 routes, it is possible to solve this problem by trying every route. However, Problem 67, is the same challenge with a triangle containing one-hundred rows; it cannot be solved by brute force, and requires a clever method! ;o)
here's the algorithm I've used to solve it
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Problem18
{
class Program
{
static void Main(string[] args)
{
string triangle = #"75
95 64
17 47 82
18 35 87 10
20 04 82 47 65
19 01 23 75 03 34
88 02 77 73 07 63 67
99 65 04 28 06 16 70 92
41 41 26 56 83 40 80 70 33
41 48 72 33 47 32 37 16 94 29
53 71 44 65 25 43 91 52 97 51 14
70 11 33 28 77 73 17 78 39 68 17 57
91 71 52 38 17 14 91 43 58 50 27 29 48
63 66 04 68 89 53 67 30 73 16 69 87 40 31
04 62 98 27 23 09 70 98 73 93 38 53 60 04 23";
string[] rows = triangle.Split('\n');
int currindex = 1;
int total = int.Parse(rows[0]);
Console.WriteLine(rows[0]);
for (int i = 1; i < rows.Length; i++)
{
string[] array1 = rows[i].Split(' ');
if (array1.Length > 1)
{
if (int.Parse(array1[currindex - 1]) > int.Parse(array1[currindex]))
{
Console.WriteLine(array1[currindex - 1]);
total += int.Parse(array1[currindex - 1]);
}
else
{
Console.WriteLine(array1[currindex]);
total += int.Parse(array1[currindex]);
currindex++;
}
}
}
Console.WriteLine("Total: " + total);
Console.ReadKey();
}
}
}
now whenever i run it, it comes up with 1064, only 10 less then the solution -- 1074
i haven't found any problems with the algorithm and I did the problem by hand and also came up with 1064, anyone know if the solution is wrong, i'm interpreting the question wrong, or if there's just a flaw in the algorithm?
Here is a graphical description:
Here's what the bottom up method belisarius describes--using the trivial triangle given in problem 18--looks like, just in case the image in his post is confusing to anyone else.
03
07 04
02 04 06
08 05 09 03
03
07 04
02 04 06
08 05 09 03
^^^^^^
03
07 04
10 04 06
08 05 09 03
^^^^^^
03
07 04
10 13 06
08 05 09 03
^^^^^^
03
07 04
10 13 15
^^^^^^
08 05 09 03
03
20 04
10 13 15
^^^^^^
08 05 09 03
03
20 04
10 13 15
^^^^^^
08 05 09 03
03
20 19
^^^^^^
10 13 15
08 05 09 03
23
^^
20 19
10 13 15
08 05 09 03
Your problem is that your algorithm is a greedy algorithm, always finding local maxima. Unfortunately that causes it to miss higher numbers down below because they are directly below lower numbers. For example, if the triangle were only 3 levels, your algorithm would pick 75 + 95 + 47 = 217, while the correct answer is 75 + 64 + 82 = 221.
The correct algorithm will either try every path and choose the one with the highest total, or compute paths from the bottom up (which allows you to avoid trying every one, thus being much faster). I should add that working from the bottom-up is not only much faster (O(n^2) instead of O(2^n)!), but also much easier to write (I did it in about 3 lines of code).
You've written a greedy algorithm, which I don't think fits the requirements here. Here's a quick example to demonstrate that point:
1
2 1
1 1 100
Using your algorithm you'll reach a sum of 4, although the optimal solution is 102.
It is a good question based on dynamic programming. You need to create a 2d data structure(like vector in c++) then follow the bottom to up approach of dp.
The formula is dp[i][j] += max(dp[i + 1][j], dp[i + 1][j + 1]). Try coding on your own then if you are stuck at some point see my solution.
vector< vector<int> > dp(n); // n is the number of rows
for (int i = 0 ; i < n; i++){
for (int j = 0; j <= i; j++){
cin >> val;
dp[i].push_back(val);
}
}
for (int i = n - 2 ; i >= 0; i--)
{
for (int j = 0; j <= i; j++)
dp[i][j] += max(dp[i + 1][j], dp[i + 1][j + 1]);
}
cout << dp[0][0] << endl;
return 0;
}
input: 3
2
4 5
6 8 9
output: 16
Recursive (not necessarily the best) approach:
static int q18(){
int matrix[][] = // BIG MATRIX;
return getMaxPath(matrix, 0, 0, 0);
}
static int getMaxPath(int matrix[][], int sum, int row, int col){
if(row==matrix.length) return sum;
return Math.max(getMaxPath(matrix, sum+matrix[row][col], row+1, col),
getMaxPath(matrix, sum+matrix[row][col], row+1, col+1));
}