why this code is declared like this by using 't' - java.util.scanner

why this code is reading the inputs using the variable t? three variable scanning with another variable
Scanner in = new Scanner(System.in);
int t=0;
int a=0;
int b=0;
int n=0;
t=in.nextInt();
for(int i=0;i<t;i++){
a = in.nextInt();
b = in.nextInt();
n = in.nextInt();
}

Explanation of this code:-
Scanner in = new Scanner(System.in);
int t=0;
int a=0;
int b=0;
int n=0;
t=in.nextInt();
for(int i=0;i<t;i++){
a = in.nextInt();
b = in.nextInt();
n = in.nextInt();
}
Lets understand this with an example
say you enter t =3;so for loop will run three times.
At very first iteration when i=0
a,b,n will get value using command line say a=1,b=2,n=9
At the second iteration again you need to assign values to a,b,n and the old values get overrides. say u assign a=3, b=0,n=5
At the last iteration again values assigned and loop exits and the iteration values will persist in a,b and n.
In the for loop a = in.nextInt() will led you to command line and you need to enter one integer value for a, similarly for b and n.so in this you enter values for all(a,b,n) of them t times.

Related

I'm not able to understand logic of coin changing problem in o(sum) space complexity

I'm facing difficulty in understanding O(sum) complexity solution of coin changing problem.
The problem statement is:
You are given a set of coins A. In how many ways can you make sum B assuming you have infinite amount of each coin in the set.
NOTE:
Coins in set A will be unique. Expected space complexity of this problem is O(B).
The solution is:
int count( int S[], int m, int n )
{
int table[n+1];
memset(table, 0, sizeof(table));
table[0] = 1;
for(int i=0; i<m; i++)
for(int j=S[i]; j<=n; j++)
table[j] += table[j-S[i]];
return table[n];
}
can someone explain me this code.?
First, let's identify the parameters and variables used in the function:
Parameters:
S contain the denomination of all m coins. i.e. Each element contain the value of each coin.
m represents the number of coin denominations. Essentially, it's the length of array S.
n represents the sum B to be achieved.
Variables:
table: Element i in array table contains the number of ways sum i can be achieved with the given coins. table[0] = 1 because there is a single way to achieve a sum of 0 (not using any coin).
i loops through each coin.
Logic:
The number of ways to achieve a sum j = sum of the following:
number of ways to achieve a sum of j - S[0]
number of ways to achieve a sum of j - S[1]
...
number of ways to achieve a sum of j - S[m-1] (S[m-1] is the value of the mth coin)
I did not completely decipher nor validate the rest of the code, but I hope this is a step in the right direction.
Added comments to code:
#include <stdio.h>
#include <string.h>
int count( int S[], int m, int n )
{
int table[n+1];
memset(table, 0, sizeof(table));
table[0] = 1;
for(int i=0; i<m; i++) // Loop through all of the coins
for(int j=S[i]; j<=n; j++) // Achieve sum j between the value of S[i] and n.
table[j] += table[j-S[i]]; // Add to the number of ways to achieve sum j the number of ways to achieve sum j - S[i]
return table[n];
}
int main() {
int S[] = {1, 2};
int m = 2;
int n = 3;
int c = count(S, m, n);
printf("%d\n", c);
}
Notes:
The code avoids repeats: 3 = 1+1+1, 1+2 (2 ways instead of 3 if 2+1 was considered.
No dependence on the order of the coins in term of value.

Assigning a new string crashes?

I'm trying to write my first real program with dynamic arrays, but I've come across a problem I cannot understand. Basically, I am trying to take a dynamic array, copy it into a temporary one, add one more address to the original array, then copy everything back to the original array. Now the original array has one more address than before. This worked perfectly when trying with ints, but strings crash my program. Here's an example of the code I'm struggling with:
void main()
{
int x = 3;
std::string *q;
q = new std::string[x];
q[0] = "1";
q[1] = "2";
q[2] = "3";
x++;
std::string *temp = q;
q = new std::string[x];
q = temp;
q[x-1] = "4";
for (int i = 0; i < 5; i++)
std::cout << q[i] << std::endl;
}
If I were to make q and temp into pointers to int instead of string then the program runs just fine. Any help would be greatly appreciated, I've been stuck on this for an hour or two.
q = temp performs only a shallow copy. You lose the original q and all of the strings it pointed to.
Since you reallocated q to have 4 elements, but then immediately reassigned temp (which was allocated with only 3 elements), accessing (and assigning) the element at x now is outside the bounds of the array.
If you have to do it this way for some reason, it should look like this:
auto temp = q;
q = new std::string[x];
for(int x = 0; x < 3; ++x)
q[x] = temp[x];
delete [] temp;
q[x] = 4;
However, this is obviously more complex and very much more prone to error than the idiomatic way of doing this in C++. Better to use std::vector<std::string> instead.

Faster way to convert integer array to string vector

So I have a vector with n one digit elements. I want to make m digit numbers using these elements. m and n will be given by user.
For example:
If the original vector is {0,1,2,3,4,5,6,7,8}, I want the new vector to be {012,345,678}.
So instead of 9 one digit elements, we now have 3 three-digit elements. Once again, the user gives the value of n and m.
Now I have been able to do this by converting the elements to a string and appending them. But the problem is that the array size needs to be very large (about 3 million long).
When I use my logic for this long value, it takes ages to compile. This is what I have currently.
vector<string> list_generator(int *array,int m, int combinations)
{
string ryan, temp;
stringstream aStream;
vector<string> lexicographic;
while (next_permutation(array, array + m))
{
for (int i = 0; i < m; i = i + m)
{
for (unsigned int j = i; j < m+ 1; j++)
aStream << array[i];
temp = aStream.str();
ryan.append(temp);
aStream.str("");
}
lexicographic.push_back(ryan);
ryan.clear();
}
return (lexicographic);
}
Is there a faster way to do this.

Exception in thread "main" java.util.NoSuchElementException Runtime error getting proper output

Given a number N, find the sum of all products x*y such that N/x = y (Integer Division).
Since, the sum can be very large, please output this modulo 1000000007.
Input
The first line of input file contains an integer T, the number of test cases to follow. Each of the next T lines contain an integer N.
Output
Output T lines containing answer to corresponding test case.
`
private static Scanner sc;
public static void main(String [] args ){
sc = new Scanner(System.in);long k;int s;
int t = sc.nextInt();
while(t>0){
k=0;
long n = sc.nextInt();
for(int i=1;i<=n;i++){
k=k+(n/i)*i;
}
s=(int) (k%1000000007);
System.out.println(s);
}t--;
}
I didn't get any such exception while executing the above program. But you should put t-- inside the while loop. Otherwise it will run infinitely.

c# : selecting a variable from several, randomly

I have several independant int variables in my program. Is there a way I can feed randomly the value of one of them into a new int variable or an int array ? Thanks in Advance.
EDIT:
here's a pseudocode to demonstrate:
int A1 = 1;
int A2 = 3;
int RESULT = 0;
Random rand = new Random();
Result = rand.Next(0, A1 || A2)]; //Result holds the value/variable name of A1 or A2
You could put all the ints you want to choose from in a new array and then select a random value from it. For example:
int value1 = 3;
int anotherValue = 5;
int value2 = 1;
int[] selectableInts = new int[3] { value1, anotherValue, value2 };
Random rand = new Random();
int randomValue = selectableInts[rand.Next(0, selectableInts.Length)];
How about this:
// create an array of your variables
int[] A = new int[] {1,3};
// Instantiate Random object.
Random rand = new Random();
// Get a value between 0 and the lenght of your array.
// This is equivalent to select one of the elements of the array.
int index = rand.Next(0,A.Length);
// Get the value from the array that was selected at random.
int Result = A[index];
I had some trouble myself and found this thread, but its code is for Ints only, so I was stuck for some time to make it work for other than ints.
I think #David gave me some idea how to make it work.
This is my version for using types other than ints.
Vector2 down = new Vector2(0, 1);
Vector2 left = new Vector2(-1, 0);
Vector2 right = new Vector2(1, 0);
List<Vector2> possibleDirections = new List<Vector2>()
{
down,
left,
right
};
Random random = new Random();
Vector2 selectedRandomDirection = possibleDirections[random.Next(0, possibleDirections.Count)];
// this is the result
Vector2 direction = selectedRandomDirection;

Resources