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 8 years ago.
Improve this question
please someone help me finding the error in this code. i've its python counterpart which is working fine, but when i code this in c++ , giving values(N) >=5 gives me an infinite loop.
[in this problem, you have to find the no. of trailing zeros in the factorial of a given number]
[here T is no. of test cases, N is the number, and z is output]
#include <iostream>
using namespace std;
int main(){
long int T,N,Z;
cin>>T;
for(int x=0;x<T;x++){
Z=0;
cin>>N;
for(int y=1;y<=N;y++){
if(y%5==0){
while(y/5!=0 && y%5==0){
Z+=1;
y/=5;
}
}
}
cout<<Z<<endl;
}
}
my python code is (which is working fine)
T=int(raw_input())
for x in range(1,T+1):
Z=0
N=int(raw_input())
for y in range(1,N+1):
if y%5==0:
while y/5!=0 and y%5==0:
Z=Z+1
y=y/5
print Z
change this:
for(int y=1;y<=N;y++){
int temp=y;
if(temp%5==0){
while(temp/5!=0 && temp%5==0){
Z+=1;
temp/=5;
}
}
}
your logic is not correct. Look at this, which i correctly submitted , hope it helps:-
#include<iostream>
using namespace std;
int tailing_zero(int n){
int ans=0;
int DIV=5;
while(n/DIV >0){
ans=ans+n/DIV;
DIV=DIV*5;
}
return ans;
}
int main(){
int run_count;
cin>>run_count;
int input[run_count];
for(int i=0;i<run_count;i++){
cin>>input[i];
}
for(int i=0;i<run_count;i++){
cout<<tailing_zero(input[i])<<endl;
}
return 0;
}
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 2 years ago.
Improve this question
Here is the problem: https://codeforces.com/contest/1256/problem/D .I'm getting runtime error("out of bound") in test case 15 here is my submission: https://codeforces.com/contest/1256/submission/84865113
Where am I doing wrong?
my approach:
#include <iostream>
using namespace std;
int main() {
int t;
cin>>t;
while (t--){
int n,k;
cin>>n>>k;
string num;
cin>>num;
int start = 0;
for(int i=0;i<n;i++){
if(num[i]=='0' && k>0){
if((k-abs(i-start))>=0){
swap(num[start],num[i]);
k = k - abs(i-start);
start++;
}
else{
swap(num[i],num[i-k]);
k = 0;
}
}
}
cout<<num<<"\n";
}
return 0;
}
the range of k is out of int. You should use long long.
9428683473 is out of 2147483647(max value for type int), so you should not use int. And the var i and start should change too.
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
In the following program, I have attached code and its sample output I don't know why it is printing 0 as the most significant digit.
I have tried different test cases but it just adds 0 to most significant digit.
Is it a bug or I'm missing some concept.
#include<bits/stdc++.h>
using namespace std;
int kthLargest (vector<int> arr, int n, int k){
map<int,int> a;
cout<<a.size();
vector<int>::iterator i;
for(i=arr.begin();i!=arr.end();i++)
{
a[*i]=a[*i]+1;
}
map<int,int>::iterator j;
int sum=0;
for(j=a.begin();j!=a.end();j++)
{
sum+=j->second;
if(sum>(n-k))
{
sum=j->first;
break;
}
}
return sum;
}
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int n;
cin>>n;
vector<int> a(n);
for (int i = 0; i < n; ++i)
{
cin>>a[i];
}
int k;
cin>>k;
cout<<kthLargest(a,n,k);
return 0;
}
input
5
1 2 3 4 5
3
output
04
You are outputting 2 numbers, one immediately after the other:
cout<<a.size();
and
cout<<kthLargest(a,n,k);
The first is 0 (the map is empty), the second is the final result of your function call. The formatting is behaving as expected, and no leading 0 is being output anywhere.
You probably forgot about the cout<<a.size();, so just remove it and your code will be fine.
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 5 years ago.
Improve this question
4 of the samples shows ok.. Others are not running !
Problem Link: https://www.hackerearth.com/challenge/competitive/programming-indiahacks-2017/algorithm/hacker-with-prime-bebe28ac/
#include <iostream>
using namespace std;
int main()
{
long N,A[100000],Q,X[10000],R,p;
cin>>N>>Q;
for (int i=0;i<N;i++){
cin>>A[i];
}
for (int i=0;i<Q;i++){
cin>>X[i];
}
for (int i=0;i<Q;i++){
R=0;
bool isprime=false;
for (int j=0;j<N;j++){
R = (X[i]/A[j])*A[j];
if (R==X[i])
{
for (int k=2;k<=R/2;k++)
{
if(R%k==0){
R=0;
};
};
if(R==0){
cout<<"YES"<<endl;
isprime=true;
break;
}
else {
isprime=false;
}
}
}
if(isprime==false){
cout<<"NO"<<endl;
}
}
return 0;
}
My psychic powers suggest you have input that will trigger your code to attempt to divide by zero.
For example:
R = (X[i]/A[j])*A[j];
if(R%k==0){
If A[j] is zero in the first line, or if k is zero in the second line... that might be the cause....
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
I was debugging this, and debugger skipped the last 'if' even 'sum' was equal to 'n' and jump straight to 'else', I don't know why. Please help.
P/s: Can I use dynamic array to increase the mobility of my program?
#include <iostream>
#include <math.h>
using namespace std;
int exponent_of_10(); // set position for digits
int exponent_of_10(int a, int b){
for(int j = b; j>0;j--)
{
a *= 10;
}
return a;
}
main() //check if the number was palindromic
{
int n;
int a[6]={0,0,0,0,0,0};
int i = 0;
int temp;
int S;
cout<< "Input n (maximum of 6 digits): ";
cin>> n;
do
{
if(n<1)
{break;}
temp=n%10;
a[i]=temp;
n=(n-temp)/10;
i++;
}
while (n!=0);
int sum = 0;
for(int j=0; j<=5; j++)
{
exponent_of_10(a[j],j);
S = exponent_of_10(a[j],j);
if (S==0)
{break;}
sum +=S;
}
if(sum==n)
{
cout<< "Congratz, this is PALIDROMIC NUMBER !!";
}
else
cout<< "Sad life, this is NOT palidromic number";
return 0;
}
When the code exits the do ... while() loop, n is 0. For the test in the if to be meaningful, the code should save the original value of n somewhere and compare sum to that original value.
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 6 years ago.
Improve this question
#include<stdio.h>
#include<conio.h>
int main()
{
int ar1[3][3] = {{1,0,0},{0,1,0},{0,0,1}};
int ar2[3][3] = {{1,2,3},{4,5,6},{7,8,9}};
int ar3[3][3];
int i,j,k;
for(i=0;i<3;i++)
{
ar3[i][j] = 0;
for(j=0;j<3;j++)
{
for(k=0;k<3;k++)
{
ar3[i][j] = ar3[i][j]+(ar1[i][k]*ar2[k][j]);
}
}
}
for(i=0;i<3;i++)
{
for(j=0;j<3;j++);
printf("%d\t",ar3[i][j]);
}
getch();
return 0;
}
When I compile the code in Dev C++ it does not give any error but fails to run and the application stops working. Whats wrong with it?
On the line
ar3[i][j] = 0;
j is still garbage so you get undefined behavior, which may lead to different kinds of faults - erroneous results, segfaults, on some rare cases it may even work as expected.
Move that line inside the second loop.
you should initialize arr3[i][j] to 0 as int arr3[i][j]={0};. Also while displaying the multiplication matrix you closed the second loop with j. Don't close that otherwise you won't get desired output. I corrected it. Hope this one solves your problem.
#include<stdio.h>
#include<conio.h>
int main()
{
int ar1[3][3] = {{1,0,0},{0,1,0},{0,0,1}};
int ar2[3][3] = {{1,2,3},{4,5,6},{7,8,9}};
int ar3[3][3] = {0}; // here goes initialization
int i,j,k;
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
for(k=0;k<3;k++)
{
ar3[i][j] = ar3[i][j]+(ar1[i][k]*ar2[k][j]);
}
}
}
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
printf("%d\t",ar3[i][j]);
}
printf("\n");
}
getch();
return 0;
}