Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 2 years ago.
Improve this question
My initial question was a bit vague, so here's the edited question.
The 3n+1 uVa problem is based on the Collatz Conjecture.
Consider any positive integer 'n'. If it is even, divide it by 2. If it is odd, multiply it by 3 and add 1. After 'x' such repeated operations, you will get 1. This has been proven for very large numbers by supercomputers.
UVa Online Judge is an online automated judge for programming problems hosted by University of Valladolid.
Here's a link to the problem statement:
uVa 3n+1
Please read the problem statement before looking at my code!
The uVa online judge runs your code against certain test-cases and tells you if your solution was right or wrong. It does not tell you why it failed (if your solution was wrong).
Here is the link again if you missed the first one:
The link to the problem description
I don't understand what I am missing out or skipping (since I am getting Wrong Answer) in my code logic. I have tried lots of test cases, and they seem to be working fine. I know the logic can be compressed to a large extent, but I just need to figure out where the flaw is for now.
Here is my code. I know bits/stdc++ shouldn't be used, but currently, it's not affecting my code. Please review it if you can and help me out.
#include<iostream>
#include<cstdlib>
#include<bits/stdc++.h>
using namespace std;
int main()
{
unsigned long int num;
int i,j,tempi, tempj,temp;
//Scanning until inputs don't stop
while(scanf("%d %d",&i,&j)!=EOF)
{
//Boolean variable to set if i is greater
bool iwasmore=false;
//Boolean variable for equal numbers
bool equalnums=false;
int cycles=1, maxcycles=0;
if(i>j)
{
//swapping for the for loop to follow
temp=i; i=j; j=temp;
iwasmore=true;
}
if(i==j)
{
equalnums=true;
}
tempi=i; tempj=j;
//Taking each number in the given range and running it in the algorithm.
//The maxcycles variable will have the value of the maximum number of cycles
//that one of the numbers in the range took (at the end of the for loop).
for(num=i;num<=j;num=(++tempi))
{
if(cycles>maxcycles)
{
maxcycles=cycles;
}
cycles=1;
//The actual algorithm
while(num!=1)
{
if(num%2==1)
{
num = (3*num)+1;
cycles++;
}
else
{
num=(num/2);
cycles++;
}
}
if(equalnums==true)
{
maxcycles=cycles;
equalnums=false;
}
//Resetting num
num=0;
}
if(!iwasmore)
cout<<i<<" "<<j<<" "<<maxcycles<<endl;
else
cout<<j<<" "<<i<<" "<<maxcycles<<endl;
}
return 0;
}
Here is the output I am getting for the following input:
Input:
1 1
10 1
210 201
113383 113383
999999 1
Output:
1 1 1
10 1 20
210 201 89
113383 113383 248
999999 1 525
Another testcase:
Input:
1 5
10 8
210 202
113383 113383
999999 999989
Output:
1 5 8
10 8 20
210 202 89
113383 113383 248
999999 999989 259
The cycle length of the last number checked is never compared against maximum.
For Input:
8 9
9 9
The program outputs
8 9 4
9 9 20
But the correct answer is
8 9 20
9 9 20
Put
if (cycles > maxcycles) {
maxcycles = cycles;
}
at the end of the for-loop, instead of at the beginning.
Related
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 1 year ago.
Improve this question
I am trying to do dfs traversal using recursive call, My Graph, and vector<int>visited are global variables. num_v variable corresponds to number of vertices,num_e corresponds to number of edges I tried calling two DFS calls in main, but my output matches to the correct DFS order for the first case. and it gives incorrect out for the other, kindly help where I'm getting wrong in the implementation of it.
sample input: first line is a number of vertices and number of edges. next, each line is directed nodes between two-point.
5 5
1 2
2 4
4 5
5 3
1 3
expected output:
1 2 4 5 3
2 4 5 3
my wrong output
1 2 4 5 3
2
code:
#include<iostream>
#include<vector>
#include<stack>
using namespace std;
int mx=1e5;
//int num_v;
vector<vector<int>>Graph(mx);
vector<int>visited;
void dfs(int v,vector<int>&vec){
visited[v]=1;
vec.push_back(v);
for(int neigh:Graph[v]){
if(visited[neigh]!=1){
dfs(neigh,vec);
}
}
}
int main(){
int num_v;int num_e;
cin>>num_v>>num_e;
visited.resize(num_v + 1);
for(int i=0;i<num_e;i++){
int u,v;
cin>>u>>v;
Graph[u].push_back(v);
}
vector<int>vec;
dfs(1,vec);
for(auto x:vec){
cout<<x<<" ";
}
visited.clear();
cout<<endl;
vector<int>tt;
dfs(2,tt);
for(auto y:tt){
cout<<y<<" ";
}
}
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 4 years ago.
Improve this question
I have to store the matrix
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
in a variable called "a".I am given the following code block
for(i=1;i<=5;i++)
for(j=1;j<=5;j++)
.................
which I have to finish.I think there should be some connection between the elements of the matrix,but I can't see any.
EDIT:I must not use any additional variables.
You have an array of 5 times 5 integral values; i obviously stands for a row index, whereas j indicates a column index. The respective value in each cell then is (i-1)*5 + j.
But, probably a pitfall introduced by your teacher, be aware that array indizes in c++ start from 0, not 1. And that's why I'm giving more hints than usual for a question without any attempt of solving it:
int arr[5][5];
for(i=1;i<=5;i++) {
for(j=1;j<=5;j++) {
val = // enter the expression here
rowIndex = i-1;
columnIndex = j-1;
// insert the one missing statement here :-)
}
}
i am working through an EDX course on computer programming. I have come to this problem and dont know how to work through it. im not looking for an answer but more a point in the right direction.
so the question gives you a 2D array. two columns and N amount of rows. the N is the number of students. each column is the grade of first test and then the second is the grade of the second test. I am asked to find the root mean square of two seperate kids and compare them and then return a number based off the comparison. The question gives you this formula
RMS = (0.5×(midsem_marks2 + endsem_marks2))0.5
I know how to get the appropriate marks using array[index 1(firsttest)] etc and then how to compare them. However, i am clueless on how to write that formula. any help would be great. Thanks in advance.
code I have
float RMSi1 = sqrt(.5*((marksarray[index1][0]*marksarray[index1][0])+(marksarray[index1][1])*(marksarray[index1][1])));
float RMSi2 = sqrt(.5*((marksarray[index2][0]*marksarray[index2][0])+(marksarray[index2][1])*(marksarray[index2][1])));
if(RSMi1>RSMi2){
return -1;
}
if(RSMi1<RSMi2){
return 1;
}
if(RSMi1==RSMi2){
return 0;
}
I'm getting an error that the RSMi1 and 2 are not declared in the if statements
Input marksarray:
1 2
1 60 20
2 60 20
3 30 40
4 10 90
5 90 30
6 0 100
7 60 20
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 7 years ago.
Improve this question
I am getting Time Limit Exceeded on submitting this question
Question:
Let's consider a triangle of numbers in which a number appears in the first line, two numbers appear in the second line, three in the third line, etc. Develop a program which will compute the largest of the sums of numbers that appear on the paths starting from the top towards the base, so that:
on each path the next number is located on the row below, more precisely either
directly below or below and one place to the right;
the number of rows is strictly positive, but less than 100
all numbers are positive integers between O and 99.
My Code:
#include<stdio.h>
#include<iostream>
#include<algorithm>
using namespace std;
int trian(int i,int j);
long long int n,a[100][100];
int main()
{
long long int t,i,j,v,k;
scanf("%lld",&t);
for(i=0;i<t;i++)
{
scanf("%lld",&n);
for(j=0;j<n;j++)
{
for(k=0;k<j+1;k++)
{
scanf("%lld",&a[j][k]);
}
}
v=trian(0,0);
printf("%lld\n",v);
}
}
int trian(int i,int j)
{
if(i>=n)
return 0;
else
return (a[i][j]+(std::max(trian(i+1,j),trian(i+1,j+1))));
}
Why I am getting Time Limit Exceeded?
Consider this triangle (ignore the numbers):
1
2 3
There are 2 possible paths to take here. Let's add a row:
1
2 3
4 5 6
The 4 can only be reached via a path that ends directly above, the 5 has two paths which can reach it, the 6 can only be reached from the path previously ending left above of it. We now have 4 possible paths. Another row:
1
2 3
4 5 6
7 8 9 0
That's 8 possible paths. Do you see a pattern? Let's describe the path straight down to 7, starting from 1:
D = DOWN
R = DOWN AND RIGHT
DDD
The (single) path to 0:
RRR
Since in each step you go down one row, you can only chose between the two possibilities number of rows - 1 times, thus giving you:
2^(number of rows - 1) possible paths
With 100 rows, that's alot. Your code tries to compute each of these paths separately. Assuming computing 1 path takes 1 nanosecond (which would be blazing fast) computing them all would take more than 2 * 10^16 years. Well, ...
Time Limit Exceeded
So you now know that you cannot just compute every possible path and take the maximum. The main issue is the following:
1
2 3
4 5 6
One path to the 5 is 1 + 3 + 5, the path to the 6 is 1 + 3 + 6. Your code computes each path separately, thus 1 + 3 will be computed twice. If you save that result, you'll get rid of most of the unnecessary computations.
How could you store such results? Well, 1 + 3 is the computation of the path arriving at 3, so store it there. What if a number (say 5) can be reached by multiple paths?
5 could be reached via 1 + 2 + 5 or 1 + 3 + 5.
Any path going through the 5 will give a higher result if it wen't through the 3 first, so only remember this path (and ignore the path through the 2, it's useless now).
So, as an algorithm:
For each row, starting at row 1 (not the first, but the second): For each entry: Calculate the maximum of the entries left above (if available) and directly above (if available) and store the result + the entry's value as new value for the entry. Then, find the maximum of the last row.
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 7 years ago.
Improve this question
Can u Guys Please give me tips on how to reduce the compilation time of my c or c++ programmes...
Some basic simple techniques will be helpful.
I was solving a question through a site(https://www.codechef.com/problems/TRISQ)
The Question was :-
What is the maximum number of squares of size 2x2 that can be fit in a right angled isosceles triangle of base B.One side of the square must be parallel to the base of the isosceles triangle.Base is the shortest side of the triangle.
First line contains T, the number of test cases.
Each of the following T lines contains 1 integer B.
Output exactly T lines, each line containing the required answer.
Sample Input
11
1
2
3
4
5
6
7
8
9
10
11
Sample Output
0
0
0
1
1
3
3
6
6
10
10
MY CODE
#include<iostream>
using namespace std;
int main()
{
int T,N,a,i,j;
cin>>T;
while(T--)
{
a=0;
cin>>N;
N=N/2;
N--;
j=N;
for(i=0;i<j;i++)
{
a+=N;
N--;
}
cout<<a<<endl;
}
}
So how do u guys think that this code (for eg) can be edited for better compilation time?
First profile.
Second, turn up optimizations levels on you compiler.
Thirdly, replace your for loop with multiplication / algebra. For example, the line
a+=N
is the fundamental basis for multiplication (repetitive addition), and the loop can be replaced by:
a += j * N; N -= j;
Replacing the loop will speed up your program (if your compiler hasn't already replaced the loop).
Printing the assembly language for the function will show how the compiler applied optimizations.
Edit 1:
Less code means a faster build time as well. I don't know if time difference in building is measurable.