This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. For help making this question more broadly applicable, visit the help center.
Closed 9 years ago.
Mr. Little Z is looking at a piece of paper and unsuccessfully trying to find the square root of a number written on the paper.
Help Mr. Little Z find the number B which is the square root of BIG number A. The number A has 1000 digits at most and the square root of A will always be an integer.
INPUT:
The first line of the standard input contains the number 0
OUTPUT:
To the standard output write number M, where M represents the length of number B (where B is the square-root of the number A) and in the next M lines write the digits of the number B (from the most significant digit to the least significant).
Input:
3
6
2
5
Output:
2
2
5
Explanation:
The number written on the paper was 625, and its square-root is 25.
My Code :
#include <iostream>
#include <cmath>
using namespace std;
int main()
{
int n,m,k,i;
int niza[1001];
cin>>m;
for(int i=0;i<m;i++)
cin>>niza[i];
niza[i]=k;
cout<<sqrt(k);
return 0;
}
Most likely this is because you never set k, and the random value it contains at the start of the program is negative, so sqrt(k) crashes. Also, if m is entered as greater than 1000, you will go outside the array bounds.
Your program will crash at: niza[i] = k because i is past the end of the array.
k is not initialized. You are outputting the square root of an uninitialized variable.
Like #Vilx- said, you will have to check the value of m before your loop.
Also, the sqrt function does not work on an array of numbers, so you will have to come up with a different method to calculate square root based on digits.
You will need to check the status of cin after cin >> m and cin >> niza[i]. Users can and will enter anything, such as letters, which cause failure when a number is expected.
Also, try putting spaces between operators and function names. They don't add anything to the size or speed of an executable, but make reading and maintenance easier.
Related
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 2 years ago.
Improve this question
I have to find all prime numbers between two given numbers(given in ascending order i.e small, large) I made logic such that my program starts from the given least number till the given most numbers and find factors for each number in between, if factors count are 2 i.e 1 and itself(which is a condition for a prime number), hence it is printed as prime. However I am unable to print my desired output.. can't track why(P.S I am 19 years old newbie in Programming)
#include <iostream>
using namespace std;
int main(){
int start,end;
cin>>start,end;
for(int i=start+1;i<end;++i){
int count;
for(int j=1;j<=i;++j){
if(i%j==0 || i/2==0)count++;
}
if(count==2) cout<<i<<endl;
}
return 0;
}
Input: 1 10
Expected Output:
2
3
5
7
9
Output: (nothing)
Your program has several issues.
cin>>start,end; is not going to read in 2 numbers. You need cin >> start >> end;
You are not initializing count to anything, so you invoke undefined behavior when you do count++. You need to do int count = 0;
Also, when checking if n is prime, you don't need to check for divisibility by 1 or n since this is always true.
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.
This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. For help making this question more broadly applicable, visit the help center.
Closed 9 years ago.
Description:
Jack is growing some peanuts in his garden. The garden is a L * W rectangle and every unit area has its independent number of how much peanuts he will harvest. He wants to find out what the maximum amount of peanuts is in a given-sized region. This region has fixed size as a*b.
Input Format:
Line 1 : 2 integer length L and width W
Line 2 - L+1 : W integer per line, representing the amount of peanuts in unit area, A (0<=A<=10)
Line L+2 : 2 integer, a and b, representing the length and width of the selected region
Output Format:
One integer m, which is the maximum sum of the amount of peanuts in the selected a*b region
Sample input:
4 5
1 2 3 4 5
6 7 8 0 0
0 9 2 2 3
3 0 0 0 1
3 3
Sample output
38
Data satisfying: 1≤a≤L,1≤b≤W
I think this problem can be abstracted to:
Return the MAXIMUM SUM of numbers in a*b sub-array of a L*W array
I consider there are (L-a+1)*(W-b+1) possible sums and I have to find the maximum.
My code is here, which cannot give the correct result. My code always returns the sum of the upper-left region of the area, but actually the problem need to return the MAXIMUM SUM of the a*b region among all possible positions. Thanks a lot in advance!
#include <iostream>
#include <cstring>
using namespace std;
int L,W,a,b,i,j,x,y,p,q,r,s;
int main()
{
cin>>L>>W;
int peanut[L][W];
for (i=0;i<L;i++)
for (j=0;j<W;j++)
cin>>peanut[i][j];
cin>>a>>b;
int sumArray[L-a+1][W-b+1];
memset(sumArray,0,(L-a+1)*(W-b+1)*sizeof(int));
for (p=0;p<L-a+1;p++)
for (q=0;q<W-b+1;q++)
{
for (r=p;r<a;r++)
for (s=q;s<b;s++) sumArray[p][q]+=peanut[r][s];
}
int max = 0;
for (x=0;x<L-a+1;x++)
for (y=0;y<W-b+1;y++)
if (sumArray[x][y]>max) max=sumArray[x][y];
cout << max;
return 0;
}
The problem is that you did not index elements carefully to calculate the sumArray. I only changed two lines of your code to make it work.
for (r=0;r<a;r++)
for (s=0;s<b;s++) sumArray[p][q]+=peanut[p+r][q+s];
The following is the full code. Please fix it if there are other problems.
#include <iostream>
#include <cstring>
using namespace std;
int L,W,a,b,i,j,x,y,p,q,r,s;
int main()
{
cin>>L>>W;
int peanut[L][W];
for (i=0;i<L;i++)
for (j=0;j<W;j++)
cin>>peanut[i][j];
cin>>a>>b;
int sumArray[L-a+1][W-b+1];
memset(sumArray,0,(L-a+1)*(W-b+1)*sizeof(int));
for (p=0;p<L-a+1;p++)
for (q=0;q<W-b+1;q++)
{
for (r=0;r<a;r++)
for (s=0;s<b;s++) sumArray[p][q]+=peanut[p+r][q+s];
}
int max = 0;
for (x=0;x<L-a+1;x++)
for (y=0;y<W-b+1;y++)
if (sumArray[x][y]>max) max=sumArray[x][y];
cout << max;
return 0;
}
There are two issues. First of all, a and b are the test region's size.
for (r=p;r<a;r++)
for (s=q;s<b;s++)
sumArray[p][q]+=peanut[r][s];
Here, p and q represent initial indecies, however you are checking the index against the subreigion dimension limits. Instead, it should be:
for (r=p;r<p+a;r++)
for (s=q;s<q+b;s++)
sumArray[p][q]+=peanut[r][s];
The second issue is that 38 is the answer; the topleft-most region contains the maximum of all the subrectangles. Here is my solution that prints all of the values. It should be noted that I'm not familiar with this int arr[X][Y] business when X and Y are not const (couldn't get it to compile), so I changed it to something I'm familiar with.
#include <iostream>
#include <cstring>
using namespace std;
int L,W,a,b,i,j,x,y,p,q,r,s;
int main()
{
cin>>L>>W;
int** peanut=new int*[L]; //a new row pointer for each column element
for (i=0;i<L;i++){
peanut[i]=new int[W]; //the actual row for each column element
for (j=0;j<W;j++)
cin>>peanut[i][j];
}
cin>>a>>b;
//don't need to remember results; just compare maximum after each evaluation.
//int sumArray[L-a+1][W-b+1];
//memset(sumArray,0,(L-a+1)*(W-b+1)*sizeof(int));
int max = 0;
for (p=0;p<L-a+1;++p){
for (q=0;q<W-b+1;++q){
int sum=0;
for (r=p;r<p+a;++r) //r<p ==> r<p+a, prefix increment is always better
for (s=q;s<q+b;++s){ //s<q ==> s<q+b, prefix increment is always better
cout << "(" << r << "," << s << "," << peanut[r][s] << ") ";
sum+=peanut[r][s];
}
cout << sum << endl;
if(sum>max) max=sum;
}
}
/* Merged in previous loop
int max = 0;
for (x=0;x<L-a+1;x++)
for (y=0;y<W-b+1;y++)
if (sumArray[x][y]>max) max=sumArray[x][y];
*/
cout << "Answer: " << max;
//Since I'm in Visual Studio, I need to pause and see the result before quitting...
int s;
cin >> s;
return 0;
}
The problem is this part:
for (r=p;r<a;r++)
for (s=q;s<b;s++) sumArray[p][q]+=peanut[r][s];
You are not iterating a and b times. Change it to:
for (r=p;r<p+a;r++)
for (s=q;s<q+b;s++) sumArray[p][q]+=peanut[r][s];
This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. For help making this question more broadly applicable, visit the help center.
Closed 10 years ago.
The assignment is to write a computer program that will add 1/3 to itself a large number of times and to compare the result to multiplying 1/3 by the number of times 1/3 was added to itself. It is also to do the same thing with ½.The program is to do this arithmetic twice, once using single precision (float) and once using double precision (double). Both of these will be in one program. Make certain you use a type for your counter that works with these large numbers.
Your program will do these additions 109 (1 billion) times.
#include<iostream>
#include<conio.h>
#include<math.h>
#include <limits>
using namespace std;
typedef std::numeric_limits< double > dbl;
int main()
{
long size=1000000000;
int count=0;
long N=10;
float nAdd=1;
float nMul=1;
cout.precision(dbl::digits10);
cout<<"Iterration #\t\tAdd\t\t\tMul"<<endl;
for(long i=1; i<=size; i++)
{
nAdd+=1.0/3.0;
nMul*=1.0/3.0;
count++;
if(count%N==0 && count!=0)
{
N*=10;
cout<<i<<"\t\t"<<fixed <<nAdd<<"\t\t"<<fixed <<nMul<<endl;
}
if(count==size)
{
cout<<"Difference : "<<fixed <<nAdd<<" - "<<fixed <<nMul<<" = "<<fixed <<nAdd-nMul<<endl;
}
}
getch();
return 0;
}
so for i have done this
i don't get it properly
what number i have to use which will be multiply by 1/3 or 1/3 will be added into it
can you guyz explain me this a lil
thanks alot
add 1/3 to itself a large number of times
This means that you should add ⅓ a bunch of times: ⅓ + ⅓ + ⅓ + ⅓ + ...
Then you're supposed to compare the result of that calculation with the result of multiplying ⅓ by the number of times you added it. So for example if you add it together four times (⅓ + ⅓ + ⅓ + ⅓) then you compare that result with the result of ⅓ × 4.
Mathematically the results should be the same, but the purpose of this assignment is to teach you something about how the computer performs the calculation.
This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. For help making this question more broadly applicable, visit the help center.
Closed 10 years ago.
Question: input an int number ex: ABCD, verify that is ABCD = AB*CD or not
(note that we don't how many digits this number got, just know it is an positive integer
and yes, it the number of digits is odd we can conclude result is No, immediately by... eye lol)
for example:
Enter a number: 88
Output: No
Enter a number: 12600
Output: No
Enter a number: 116725
Output: Yes, 116 * 725 = 116725 (**this is just example,
not actual result, just help to understand how output look like**)
Problem is that, you can't use array, jump and bitwise in order to solve this. Yes, if we can use array then not much to say, put input number in an array, check first half multiply other half....bla..bla... I need help with IDEA without using array to solve this problem, right now I'm stuck! Thank you guy very much!
Your program can safely output No for every input. Proof:
You are looking for integers A and B such that A*B = A*10^k + B, with A and B > 0 and B < 10^k.
If A*B = A*10^k + B, then B = 10^k + B/A > 10^k. But B had to be less than 10^k, so this is a contradiction. Therefore no such A and B exist.
A longer proof:
You are looking for integers A and B such that A*B = A*10^k + B, with A and B > 0 and B < 10^k.
Subtract B from both sides to get (A-1)*B = A*10^k.
Since A is a factor in the right hand side it is also a factor in the left hand side. But A and A-1 are coprime, so A must divide B. So, B = n*A for some integer n.
Now we have A*B = A*10^k + n*A, or A*B = (10^k + n)*A. Since A > 0 we can divide both sides by A to get B = 10^k+n. But this is impossible since B was supposed to be less than 10^k!
A little hint to your 6 digits number:
to get last 3 digits use % 1000
to get first 3 digits use (int) X/1000.
Notice that 1000 == 10^3.
Write a program that asks for the input and then prints "No".
Done.
It's not too clear what you're trying to do. ABCD == AB*CD suggests
four digit numbers. For a four digit number x, a test for the above
would be x == (x / 100) * (x % 100). For a six digit number, replace
100 with 1000, and more generally, for an n digit number, where
n is even, use 10^(n/2). If n is odd, however, I'm not sure what you're looking for, and the last example you give doesn't meet the criteria you mention; if you can permutate the digits in each half, the problem then becomes more complex.
You can read a string, split it in the middle and convert the both parts to an int.
You could also read an int, calculate the number of digits (writing your own loop or using the log function) and split the int after the calculated number of digits.
You can also write a loop that is taking an int ABCD, splitting it into ABC and D and moving the digits from ABC to D while the both have not the same number of digits (you do not need to calculate the number of digits here, there is a quite easy comparison you can do).
To get the number of digits, count how many times you had to divide the number by 10 until it gets smaller than 1.
For 116725, you would need to divide by 10 six times. After that you can print no if the number is odd or calculate the result like James Kanze and ProblemFactory described.
Here's a complete "C/C++" solution:
#include <stdio.h>
int main(void) {
while (fgetc(stdin) != '\n');
return puts("No.");
}