Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 3 years ago.
Improve this question
I have a problem with Input and Output in my program.
I can’t make sure that the BFS response for each horse is separated and at the end the BFS response of all horses of a given number of examples is summarized.
Everything works well for this example:
1- number of examples
1 - the number of chess knights
5 5 - the starting point
5 6 - the final point
answer: 3
but not this example:
1- number of examples
2 - the number of chess knights
0 0 - the starting point of the first knight
1 0 - the starting point of the second knight
0 1 - the final point of the first knight
1 1 - the final point of the second knight
I need the answer(BFS) for the first horse and for the second horse to be summed up (for first = 2, for second = 2, for all horses = 4). But if you check this example using my code (below) then the answer is 3, the program considers only the first horse.
Here is my code:
int main()
{
int number_of_examples;
cin >> number_of_examples; //number of examples
for (int i = 1; i <= number_of_examples; i++) {
int number_of_horse;
cin >> number_of_horse; //number of horse
vector<Node> src;
vector<Node> dest;
int x, y;
for (int i = 1; i <= number_of_horse; i++)
cin >> x >> y;
src.push_back(Node(x, y));
for (int i = 1; i <= number_of_horse; i++)
cin >> x >> y;
dest.push_back(Node(x, y));
for (int i = 0; i < src.size(); i++)
{
for (int j = 0; j < dest.size(); j++)
{
cout << BFS(src[i], dest[j]);
}
}
}
return 0;
}
the program considers only the first horse
As you told it to do...
for (int i = 1; i <= number_of_horse; i++)
cin >> x >> y;
src.push_back(Node(x, y));
Only the first line following the for is repeated. If you want (I wager you do) repeat multiple statement, you need to enclose them in a block:
for (int i = 1; i <= number_of_horse; i++) {
cin >> x >> y;
src.push_back(Node(x, y));
}
You might want to read a bit more about the for syntax.
Related
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 days ago.
Improve this question
hi i'm trying to solve a problem, where
given a set of numbers, for example:
0 1 3 0 0 1 2 0 2 3
I want to find the minimum contiguous subset that contains at least once all different elements of the set
in this case the answer could be:
1 2 0 2 3 which is that part of the previous set : 0 1 3 0 0 [ 1 2 0 2 3 ]
is a minimum contiguous subset of the previous set that contains all distinct elements of the set
i create this solution but is too slow (for the time complexity i need a DP way)
#include <bits/stdc++.h>
using namespace std;
int main() {
// uncomment the following lines if you want to read/write from files
// ifstream cin("input.txt");
// ofstream cout("output.txt");
//N is the size of the set
//C is the size of the contigues subset
int N, C;
cin >> N >> C;
vector<int> L(N);
//i take a set in imput
// 0 1 3 0 0 1 2 0 2 3
for (int i = 0; i < N; i++) {
cin >> L[i];
}
// insert your code here
int min = INT_MAX;
//couter
int ct= 0;
unordered_set<int> container;
for (int i = 0; i < N; i++) {
for (int j = i; j < N; j++) {
ct++;
// cout<<ct<<"\n";
container.insert(L[j]);
if(container.size() == C){
break;
}
}
if(ct<min && container.size() == C){
min = ct;
}
container.clear();
ct = 0;
}
cout << min<< endl; // print the result
return 0;
}
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 4 months ago.
The community reviewed whether to reopen this question 4 months ago and left it closed:
Original close reason(s) were not resolved
Improve this question
I am a student in my first year.
Could someone help me with an assignment?
Simple calc C++
In the first line the number of tests n. In the next n lines operations Each operation contains the operation type (+ *) the number of k numbers to be processed, and k numbers
Output
Result
Example
Input
3
+ 3 3.1 5.2 -8.3
* 2 1 3.3
+ 1 3
Output
0
3.3
3
My code
int n, k;
char x;
cin >> n;
int tab[100];
for (int i = 0; i < n; i++)
{
cin >> x >> k;
for (int j = 0; j < k; j++)
{
cin >> tab[j];
if (x == '+')
{
tab[j] += tab[j + 1];
}
if (x == '*')
{
tab[j] *= tab[j + 1];
}
cout << tab[j];
}
}
return 0;
}
There are a few problems.
your tab is of type int so it can only store integers e.g.: 1,2,3,4,5 not floating point numbers like 1.3 2.5 3.3, you should consider changing tab to be of type float.
your loop logic and indexing is totally wrong, and you are accessing the wrong elements, you should have some sort of accumulator variable that will accumulate the results instead of storing them in an array. e.g.:
float accumulator = 0;
cin >> input_number;
accumulator += input_number;`
the best advice i can give you is to learn to use the debugger, the debugger is your friend, you should use it to walk through your code, line by line and understand what each lines does to each variable, and read the contents of each variable, i cannot recommend a certain source for learning to use a debugger as it depends on your IDE, but you should maybe check a video on using debugger with IDE X, it can be a good start for learning to use an IDE GUI.
The issues are many. Just some:
The purpose of tab is unclear. You do not need to store the operands, you can accumulate a result in a single value.
In any event tab[j + 1]; is not initialised at point of use.
If you want to output the results after all the input as indicated in the question then you do need somewhere to store the results for later output.
You do need to output the results.
The input and output types do need to be a floating-point type.
Greater clarity of thought might arise if you were to use clear variable names that reflected their purpose and perhaps some comments. If you have to explain what the code does (even to yourself) often you spot the errors. A good technique is to write the comments first, to explain what you need to code rather than explaining the code you have written and assume erroneously to be correct. Also you need to get out of that "all variables at the top" habit.
Example:
#include <iostream>
int main()
{
// Get number of calculations
int ncalc = 0 ;
std::cin >> ncalc ;
// Create array for results
double* result = new double[ncalc] ;
// For each calculation
for( int calc = 0; calc < ncalc; calc++ )
{
// Get the operator
char op = 0 ;
std::cin >> op ;
// Get the number of operands
int noperands = 0 ;
std::cin >> noperands ;
// Get initial operand in accumulator
double accumulator = 0 ;
std::cin >> accumulator ;
// For each remaining operand...
for( int i = 1; i < noperands; i++)
{
// Get the operand
double operand = 0 ;
std::cin >> operand ;
// Perform the operation
switch( op )
{
// For +, accumulate sum
case '+' : accumulator += operand ; break ;
// For * accumulate product
case '*' : accumulator *= operand ; break ;
}
}
// Store accumulated result
result[calc] = accumulator ;
}
// Output results
for( int calc = 0; calc < ncalc; calc++ )
{
std::cout << result[calc] << '\n' ;
}
delete[] result ;
return 0;
}
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
In case of normal integer, we use %10 for the remainder, and /10 to reduce the digit by one digit.
For a Binary Digit, I could use %2 to get the remainder, but how do I remove digits from the Binary number?
If there's a number 0000, when I perform /10, I need 000 and not 0.
while (bin > 0) {
int rem = bin % 2;
// action_block;
// How do I divide ?
}
Clearing the confusion, this is for a problem of a competitive coding site. The stdin will be an integer, which has a binary data. Example: 1100001. I want its bits in an array. arr[0] = 1, arr[1] = 1, arr[2] = 0, arr[3]=0 and so on..
Quite logically, use /2. It's the same with any base.
Right shift (>>) operator is used to shift the bits to right by position specified in the expression.
For example :
unsigned int num1 = 12; // 12 = 1100
int num2 = 0;
num2 = num1 >> 1; // 6 = 0110
int num3 = 0;
num3 = num1 >> 2; // 3 = 0011
Now, try achieving your goal using this.
Edit :
As problem described by OP in comment section of this answer.
std::string inp;
std:: cin >> inp.
for (int i = 0; i < inp.length(); i++) {
std::stoi(inp[i]); // This will be 0,1.
}
If you want to use an array then this is what you are looking for :
#include <iostream>
#include <string>
using namespace std;
int main()
{
string input="0101011010";
int arr[10];
for(int i=0; i<10; i++)
{
arr[i]=input[i]-'0';
}
for(int i=0; i<10; i++)
{
cout << arr[i] << endl;
}
}
I have written the following code for the problem, ideone doesnt give me runtime error but the compiler for codechef is giving me one. The compiler in codechef is gcc 4.9.2
Problem [Link : https://www.codechef.com/problems/MOVIEWKN]
Little Egor is a huge movie fan. He likes watching different kinds of movies: from drama movies to comedy movies, from teen movies to horror movies. He is planning to visit cinema this weekend, but he's not sure which movie he should watch.
There are n movies to watch during this weekend. Each movie can be characterized by two integers Li and Ri, denoting the length and the rating of the corresponding movie. Egor wants to watch exactly one movie with the maximal value of Li × Ri. If there are several such movies, he would pick a one with the maximal Ri among them. If there is still a tie, he would pick the one with the minimal index among them.
Your task is to help Egor to pick a movie to watch during this weekend.
Expected Input
The first line of the input contains an integer T denoting the number of test cases.
The first line of the test case description contains an integer n(number of films).
The second line of the test case description contains n integers L1, L2, ...,Ln (L is the Length of the movie). The following line contains n integers R1, R2, ..., Rn(R is the Rating of the movie).
My Input
2
2
1 2
2 1
4
2 1 4 1
2 4 1 4
Expected Output
For each test case, output a single integer i denoting the index of the movie. Note that 1-based indexing is followed.
My Output
1
2
Explanation
In the first example case, both films have the same value of L × R, but the first film has a better rating.
In the second example case, the second and the fourth movies are equally good, but the second movie has a smaller index
My Solution
#include<iostream>
#include<string>
#include<vector>
using namespace std;
int main() {
int T;
cin >> T;
cin.ignore();
if (T >= 1 && T <= 5) {
for (int q = 0; q < T; q++) {
int n;
size_t pos;
cin >> n;
cin.ignore();
if (n >= 1 && n <= 100) {
vector<int> L;
vector<int> R;
vector<int> C;
for (int u = 0; u < n; u++) {
int a;
cin >> a;
cin.ignore();
if (a >= 1 && a <= 100) {
L.push_back(a);
}
}
for (int u = 0; u < n; u++) {
int a;
cin >> a;
cin.ignore();
if (a >= 1 && a <= 100) {
R.push_back(a);
}
}
for (int u = 0; u < n; u++) {
int df = (L[u] * R[u]);
C.push_back(df);
}
for (size_t u = 0; u < C.size(); u++) {
int max = C[0];
if (max < C[u]) {
max = C[u];
pos = u+1;
}
if (max == C[u]) {
if (R[pos-1] < R[u]) { pos = u + 1; }
if (R[pos - 1] == R[u]) {
if (pos > u) { pos = u + 1; }
}
}
}
cout << pos << endl;
}
}
}
return 0;
}
After going through some of the questions on runtime error regarding codechef questions, i am guessing the problem is related to accessing an out of boundary element but i am not able to pinpoint the actual problem
In this line:
if (R[pos - 1] < R[u]) { pos = u + 1; }
The first time around the loop, pos is uninitialised, leading to undefined behaviour.
Simple fix:
add pos = 1 before the loop
I'm probably late but here is my simple solution:
t=int(input())
for j in range(0,t):
n=int(input())
a=[int(ele) for ele in input().split()]
b=[int(ele) for ele in input().split()]
c=[]
pos=0
max=-1
max2=-1
for i in range(0,n):
fl=0
c.append(a[i]*b[i])
if(max<c[i]):
max=c[i]
fl=1
if(fl==1):
pos=i+1
max2=b[i]
elif(max==c[i]):
if(max2<b[i]):
pos=i+1
max2=b[i]
print(pos)
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions concerning problems with code you've written must describe the specific problem — and include valid code to reproduce it — in the question itself. See SSCCE.org for guidance.
Closed 9 years ago.
Improve this question
I'm working on a C++ project and have run into an issue that is leaving me puzzled. I am to create a phone number generator that has the user enter the first 4 numbers, and then generate all possible phone numbers that follow these two rules:
The last 6 digits must equal 33.
The 4th and 5th digit cannot both be even or both be odd.
This is what I've come up with so far:
#include <iostream>
using namespace std;
int main()
{//begin main
srand(time(0));
const int MAX_DIGITS = 10;
int num[MAX_DIGITS] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
cout<<"enter the first digit: ";
cin>>num[0];
cout<<"Enter the second digit: ";
cin>>num[1];
cout<<"Enter the third digit: ";
cin>>num[2];
cout<<"Enter the fourth digit: ";
cin>>num[3];
for (int e=0;e<MAX_DIGITS;e++)
{
for(int f=0;f<MAX_DIGITS;f++)
{
for(int g=0;g<MAX_DIGITS;g++)
{
for(int h=0;h<MAX_DIGITS;h++)
{
for(int i=0; i<MAX_DIGITS;i++)
{
for(int j=0;j<MAX_DIGITS;j++)
{
if ((num[e]+num[f]+num[g]+num[h]+num[i]+num[j]) == 33 && (num[3]%2 != 0 && num[4]%2 != 0) )
{
cout<<num[0]<<num[1]<<num[2]<<num[3]<<num[e]<<num[f]<<num[g]<<num[h]<<num[i]<<num[j]<<endl;
}
}
}
}
}
}
}
It all makes sense to me so far, but the program is displaying some numbers multiple times, and I'm not entirely certain how to make sense of the even/odd rule.
I'm still a rookie to programming and I'm sure that there may be a more efficient way to do this, but I'm trying my best and this has left me puzzled. Any help would be appreciated.
Thanks in advance!
EDIT: My question is this, how do I get the generator to display the numbers with the even/odd rule applied? My best idea was to use the modulus operator (%) to see if the remainder of the numbers divided by two was zero, and if so, the numbers were even. This is where I stumble a bit though, because I'm not perfectly certain how to implement this. Sorry for not being more specific the first time.
1) You're not ever changing the values in the num array, so testing to see if it contains a valid number doesn't work because the initial values you set don't fit the rules.
2) The validation is checking to see if both numbers are odd, not one or the other.
Here's a version that seems to work. The changes I made are to actually change the num array and then use a helper function to validate the numbers in the array so you don't have a mess inside your loops. I removed the srand call since you aren't using random numbers and the input of the first 4 digits to make testing easier for me. You can add that back if you like.
#include <iostream>
const int MAX_DIGITS = 10;
bool IsValid(int num[MAX_DIGITS])
{
int sum = 0;
for(int z = 4; z < MAX_DIGITS; ++z)
{
sum += num[z];
}
if(sum != 33)
{
return false;
}
int numodd = 0;
for(int z = 3; z < 5; ++z)
{
numodd += (num[z] % 2);
}
if(numodd != 1)
{
return false;
}
return true;
}
int main()
{
int num[MAX_DIGITS];
num[0] = 5;
num[1] = 5;
num[2] = 5;
num[3] = 1;
for (int e=0;e<MAX_DIGITS;e++)
{
num[4] = e;
for(int f=0;f<MAX_DIGITS;f++)
{
num[5] = f;
for(int g=0;g<MAX_DIGITS;g++)
{
num[6] = g;
for(int h=0;h<MAX_DIGITS;h++)
{
num[7] = h;
for(int i=0; i<MAX_DIGITS;i++)
{
num[8] = i;
for(int j=0;j<MAX_DIGITS;j++)
{
num[9] = j;
if(IsValid(num))
{
for(int z = 0; z < MAX_DIGITS; ++z)
{
if(z == 3 || z == 6)
{
std::cout << '-';
}
std::cout << num[z];
}
std::cout << std::endl;
}
}
}
}
}
}
}
}