A bug on bubble sorting
I want to sort a 2*n matrix, n is given in the input. Make a program to
output a matrix . Here is the requirment. (1)the first column MUST be
sorted in ASC, and (2)the second column in DESC if possible.
For Example, let n = 5, and the matrix is
3 4
1 2
3 5
1 6
7 3
The result should be
1 6
1 2
3 5
3 4
7 3
So I write down the code like this. First Line input the value n , and the
following lines like above.
#include <stdio.h>
#define TWO_16 65536
#define TWO_15 32768
int v[100000][2];
int z[100000];
int vec[100000];
int n;
int main()
{
int i, j;
scanf ("%d", &n); // give the value of n;
for (i = 1; i <= n; i++) // filling the matrix;
{
scanf ("%d%d", &v[i][0], &v[i][1]);
z[i] = TWO_16 * v[i][0] + TWO_15 - v[i][1];
vec[i] = i;
}
for (i = 1; i <= n; i++)
for (j = 1; j <= i; j++)
{
if (z[j] > z[i])
{
int t = vec[i];
vec[i] = vec[j];
vec[j] = t;
}
}
for (i = 1; i <= n; i++) // output the matrix
printf("%d %d\n",v[vec[i]][0],v[vec[i]][1]);
return 0;
}
But in gcc, the output is
1 6
3 5
3 4
1 2
7 3
What's more, when the first line is changed to "1 2" and the second is
changed to "3 4" in input, the result also changed.
What's the problem of my code?
Borer
Thursday, 3 October 2013
Wednesday, 2 October 2013
Use URI builder in an Android App
Use URI builder in an Android App
I'm developing an Android app. I need to build a URI for my app, to make
an API request. Unless there's another way to put a variable in a URL,
this is the easiest way I've found. I found that you need to use
Uri.Builder, but I'm not quite sure how to. My url is:
http://lapi.transitchicago.com/api/1.0/ttarrivals.aspx?key=[redacted]&mapid=`value`.
My scheme is http, authority lapi.transitchicago.com, path /api/1.0, path
segments ttarrivals.aspx, and query key=[redacted]&mapid=value.
I understand that I do uri.add, but how do I integrate it into the URI
builder? Would I add every thing, like URI.add(scheme),
URI.add(authority), and so on?
Thank you for your help.
I'm developing an Android app. I need to build a URI for my app, to make
an API request. Unless there's another way to put a variable in a URL,
this is the easiest way I've found. I found that you need to use
Uri.Builder, but I'm not quite sure how to. My url is:
http://lapi.transitchicago.com/api/1.0/ttarrivals.aspx?key=[redacted]&mapid=`value`.
My scheme is http, authority lapi.transitchicago.com, path /api/1.0, path
segments ttarrivals.aspx, and query key=[redacted]&mapid=value.
I understand that I do uri.add, but how do I integrate it into the URI
builder? Would I add every thing, like URI.add(scheme),
URI.add(authority), and so on?
Thank you for your help.
Submitting multiple values for one input field
Submitting multiple values for one input field
What I'm trying to do:
I'm trying to build a very simple visual layout builder. The idea is that,
the user selects a block, and it has some settings in it, those setting
values are stored in the hidden input, and when the user saves the page, i
store those values in the database.
Basic block is ok:
For example, user selects a 'text' block, it is added like this:
<div>
<input type="hidden" value="text" name="item_name[]">
<input type="hidden" value="" name="item_title[]">
<input type="hidden" value="sdsd" name="item_text[]">
</div>
Problem:
However, some of the blocks have more than one values for each field. For
example, 'gallery' block, which has multiple image urls, image titles etc.
I'm facing problem in finding a suitable way to put together the multiple
values and submit.
Right now I'm adding them to a string with jQuery, separated with __. I
can store the data and separate it, but the problem is that if I want to
remove any image from it, it is very difficult because I have just added
them in the string, so its hard to find it and remove it.
<div>
text item
<input type="hidden" value="gallery" name="item_name[]">
<input type="hidden" value="__http://img1.jpg__http://img2.jpg"
name="img_path[]">
<input type="hidden" value="__img1__img2" name="img_title[]">
<input type="hidden" value="" name="img_desc[]"></input>
</div>
Question:
What would be the suitable way to send multiple values for the above block
example, keeping in the mind that there will be multiple blocks having
multiple input values?
Thanks.
What I'm trying to do:
I'm trying to build a very simple visual layout builder. The idea is that,
the user selects a block, and it has some settings in it, those setting
values are stored in the hidden input, and when the user saves the page, i
store those values in the database.
Basic block is ok:
For example, user selects a 'text' block, it is added like this:
<div>
<input type="hidden" value="text" name="item_name[]">
<input type="hidden" value="" name="item_title[]">
<input type="hidden" value="sdsd" name="item_text[]">
</div>
Problem:
However, some of the blocks have more than one values for each field. For
example, 'gallery' block, which has multiple image urls, image titles etc.
I'm facing problem in finding a suitable way to put together the multiple
values and submit.
Right now I'm adding them to a string with jQuery, separated with __. I
can store the data and separate it, but the problem is that if I want to
remove any image from it, it is very difficult because I have just added
them in the string, so its hard to find it and remove it.
<div>
text item
<input type="hidden" value="gallery" name="item_name[]">
<input type="hidden" value="__http://img1.jpg__http://img2.jpg"
name="img_path[]">
<input type="hidden" value="__img1__img2" name="img_title[]">
<input type="hidden" value="" name="img_desc[]"></input>
</div>
Question:
What would be the suitable way to send multiple values for the above block
example, keeping in the mind that there will be multiple blocks having
multiple input values?
Thanks.
Check if IEnumerable is of a ValueType (at runtime)
Check if IEnumerable is of a ValueType (at runtime)
How do I check if an object I receive as a method result is not ValueType
and not IEnumerable<ValueType>?
Here is what I wrote:
MethodInfo selectedOverload = SelectOverload(selectedMethodOverloads);
object result = ExecuteAndShowResult(selectedOverload);
ExploreResult(result);
private static void ExploreResult(object result)
{
if (result != null &&
!(result is ValueType) &&
!((IEnumerable)result).GetType().GetProperty("Item").PropertyType) is
ValueType)
)
Console.WriteLine("explore");
}
Unfortunately type of PropertyType is Type, its content is the type I need
to check (e.g. int) but I don't know how to.
How do I check if an object I receive as a method result is not ValueType
and not IEnumerable<ValueType>?
Here is what I wrote:
MethodInfo selectedOverload = SelectOverload(selectedMethodOverloads);
object result = ExecuteAndShowResult(selectedOverload);
ExploreResult(result);
private static void ExploreResult(object result)
{
if (result != null &&
!(result is ValueType) &&
!((IEnumerable)result).GetType().GetProperty("Item").PropertyType) is
ValueType)
)
Console.WriteLine("explore");
}
Unfortunately type of PropertyType is Type, its content is the type I need
to check (e.g. int) but I don't know how to.
Saving Qlist to a file. Qt
Saving Qlist to a file. Qt
I have a qlist 'List' of objects of my own class. I want to save this to a
file for later use. I want to be able to open that file later, and use the
qlist to add more data (or modify,etc). I am using qt static build.
How should I go about this ? (little new at this)
I have a qlist 'List' of objects of my own class. I want to save this to a
file for later use. I want to be able to open that file later, and use the
qlist to add more data (or modify,etc). I am using qt static build.
How should I go about this ? (little new at this)
Tuesday, 1 October 2013
expectation calculation in probability and statistics
expectation calculation in probability and statistics
2 four-sided dice are rolled. X = number of odd dice Y = number of even
dice Z = number of dice showing 1 or 2 So each of X, Y, Z only takes on
the values 0, 1, 2. (a) joint p.m.f. of (X,Y)? joint p.m.f. of (X,Z). You
can give your answers in the form of 3 by 3 tables. (b) Are X and Y
independent? Are X and Z independent? (c) Compute E(XY ) and E(XZ).
WHAT I TRIED IS - The faces on a four-sided die are labeled with the
numbers 1, 2, 3, and 4. Upon throwing the dice, each one will land with
exactly one of its faces upwards. You will therefore see two numbers, one
for each die. Suppose, for the sake of example, that you see a 2 and a 4.
Both of these are even numbers. None of them are odd numbers. Let's count:
no dice show an odd number, two dice show even numbers, and one die shows
a number in the set {1,2}. Therefore, for this throw,, X=0, Y=2, and Z=1.
There are 16 combinations (1,1) (1,2)(1,3) (1,4)(2,1)(2,2)(2,3)(2,4) ...
(4,1 )(4,2 )(4,3 )(4,4 ) There are only 2 case when no dice show an odd
number, two dice show even numbers, and one die shows a number in the set
{1,2}. ---> when (2,2) (2,4 ) occurs. Here X= 0, Y =2, Z =1 Both odd and
one is in {1,2} is --> (1,1)(1,3)(3,1) Here, X= 2, Y =0, Z =1 Just as we
have to in the case with one discrete random variable, in order to find
the "joint probability distribution" of X and Y, we first need to define
the support of X and Y.
The support of X is: S1 = {2, 4} And, the support of Y is: S2 = {1, 3}
X, Y and Z combinations are - {(1,1), (1,2), (1,3), (1,4), (2,1), (2,2),
(2,3), (2,4), (3,1), (3,2), (3,3), (3,4), (4,1), (4,2), (4,3), (4,4)} Out
of this, the X, Y, Z values are respectively,
(2,0,1) , (1,1,2) ,
(2,0,1),(1,1,1),(1,1,2),(0,2,1),(1,1,1),(0,2,1),(2,0,1),(1,1,1),(2,0,0),(1,1,0),(1,1,1),(0,2,1),(1,1,0),(0,2,0).
b) The random variables X and Y are independent if and only if: P(X= x, Y
= y) = P(X = x) ~ P(Y = y) for allx¸S1,y¸S2.
if we again take a look back at the representation of our joint p.m.f. in
tabular form, you might notice that the following holds true: P(X=x,Y=y)
for allx¸S1,y¸S2. When this happens, we say that X and Y are
independent.
Similarly for X, Z, they are also independent.
c) I am not sure how to do
help please.
2 four-sided dice are rolled. X = number of odd dice Y = number of even
dice Z = number of dice showing 1 or 2 So each of X, Y, Z only takes on
the values 0, 1, 2. (a) joint p.m.f. of (X,Y)? joint p.m.f. of (X,Z). You
can give your answers in the form of 3 by 3 tables. (b) Are X and Y
independent? Are X and Z independent? (c) Compute E(XY ) and E(XZ).
WHAT I TRIED IS - The faces on a four-sided die are labeled with the
numbers 1, 2, 3, and 4. Upon throwing the dice, each one will land with
exactly one of its faces upwards. You will therefore see two numbers, one
for each die. Suppose, for the sake of example, that you see a 2 and a 4.
Both of these are even numbers. None of them are odd numbers. Let's count:
no dice show an odd number, two dice show even numbers, and one die shows
a number in the set {1,2}. Therefore, for this throw,, X=0, Y=2, and Z=1.
There are 16 combinations (1,1) (1,2)(1,3) (1,4)(2,1)(2,2)(2,3)(2,4) ...
(4,1 )(4,2 )(4,3 )(4,4 ) There are only 2 case when no dice show an odd
number, two dice show even numbers, and one die shows a number in the set
{1,2}. ---> when (2,2) (2,4 ) occurs. Here X= 0, Y =2, Z =1 Both odd and
one is in {1,2} is --> (1,1)(1,3)(3,1) Here, X= 2, Y =0, Z =1 Just as we
have to in the case with one discrete random variable, in order to find
the "joint probability distribution" of X and Y, we first need to define
the support of X and Y.
The support of X is: S1 = {2, 4} And, the support of Y is: S2 = {1, 3}
X, Y and Z combinations are - {(1,1), (1,2), (1,3), (1,4), (2,1), (2,2),
(2,3), (2,4), (3,1), (3,2), (3,3), (3,4), (4,1), (4,2), (4,3), (4,4)} Out
of this, the X, Y, Z values are respectively,
(2,0,1) , (1,1,2) ,
(2,0,1),(1,1,1),(1,1,2),(0,2,1),(1,1,1),(0,2,1),(2,0,1),(1,1,1),(2,0,0),(1,1,0),(1,1,1),(0,2,1),(1,1,0),(0,2,0).
b) The random variables X and Y are independent if and only if: P(X= x, Y
= y) = P(X = x) ~ P(Y = y) for allx¸S1,y¸S2.
if we again take a look back at the representation of our joint p.m.f. in
tabular form, you might notice that the following holds true: P(X=x,Y=y)
for allx¸S1,y¸S2. When this happens, we say that X and Y are
independent.
Similarly for X, Z, they are also independent.
c) I am not sure how to do
help please.
ToRequestContext method missing in servicestack
ToRequestContext method missing in servicestack
Using some examples I am attempting to get SS authentication working in an
asp.net MVC application. I am using this line:
authService.RequestContext =
System.Web.HttpContext.Current.ToRequestContext();
No matter what I do, I cannot find "ToRequestContext". I believe I've
added the proper using:
using ServiceStack.WebHost.Endpoints.Extensions;
Any suggestions?
Using some examples I am attempting to get SS authentication working in an
asp.net MVC application. I am using this line:
authService.RequestContext =
System.Web.HttpContext.Current.ToRequestContext();
No matter what I do, I cannot find "ToRequestContext". I believe I've
added the proper using:
using ServiceStack.WebHost.Endpoints.Extensions;
Any suggestions?
Subscribe to:
Comments (Atom)