I am trying to solve this question, but on codechef its showing wrong answer,link to the queston is http://www.codechef.com/problems/STATUES/ .On system ,its showing correct answer, after trying a lot, i couldnt find the bug My code is,
#include<stdio.h>
//#include<conio.h>
int main()
{
int a[150];
int n;
int i;
int sum;
int avg;
int count=0;
scanf("%d",&n);
int ans;
int diff;
while(n!=0)
{
sum=0;
ans=0;
count++;
for(i=0;i<n;i++)
{ scanf("%d",&a[i]);
sum=sum+a[i];
}
avg=sum/n;
for(i=0;i<n;i++)
{ diff=avg-a[i];
if(diff>0)
ans=ans+diff;
}
printf("Set#%d\nThe minimum number of moves is %d.\n",count,ans);
scanf("%d",&n);
}
return 0;
}
Read the condition in the question:
Output a blank line after each test case.
Be careful about the case and punctuation in the above strings. Not adhering to the output format strictly will lead to the Wrong Answer verdict.
There is no blank line after your each output. Also one more for loop is needed to met the condition for exact output format. Add this snippet after while.
for(i=0;i<n;i++)
{
printf("\nSet #%d\nThe minimum number of moves is %d.\n",i,ans);
}
Another problem is
int a[150];
a should not exceed from 50. Change it to
int a[50];
But I woul advice you to use variable length array in this case to save memory.
Related
I typed the following code to sort the components of an int array. It does not show any error but does stops working abruptly. The error is generally after entering 7-8 inputs which shows that program.exe has stopped working. Does it has anything related to the code ?
#include <stdio.h>
#include <stdlib.h>
int main()
{
int n,a[n],i,j,temp;
printf("Enter number of inputs.\n");
scanf("%d",&n);
printf("Enter inputs\n");
for(i=0;i<n;i++)
{
scanf("%d",&a[i]);
}
for(i=0;i<n;i++)
{
for(j=i+1;j<n;j++)
{
if(a[i]<a[j])
{
temp=a[i];
a[i]=a[j];
a[j]=temp;
}
}
}
printf("Numbers in descending order are:\n");
for(i=0;i<n;i++)
{
printf("%d\n",a[i]);
}
return 0;
}
The problem is here:
int n, a[n], i, j, temp;
Declarations are done sequentially. If you write this in a slighly more readably (but equivalent form) you'd have this:
int n;
int a[n]; // here the variable n has not yet been initialized
// it contains an indeterminate value, and therefore the a array
// will have an indeterminate size and the program will have
// so called "undefined behaviour " (google that)
int i;
...
You should write the beginning of your program like this:
int main()
{
int n,i,j,temp;
printf("Enter number of inputs.\n");
scanf("%d",&n);
int a[n]; // now n has a determinate value
printf("Enter inputs\n");
Disclaimer: no error checking is done for brevity.
Always compile with warnings enabled and listen to them. Many of them are actually errors. Especially the warning variable 'somevar' is uninitialized when used here is always an error.
it asks two questions at once,why?
here is my code:
#include <stdio.h>
int main()
{
int i,j;
int n;
int adjmatrix[n][n];
char ans;
printf("How many vertices?");
scanf("%d",&n);
for (i=0;i<n;i++)
{
for (j=0;j<n;j++)
{
printf("Are vertex %d and %d adjacent?",i,j);
scanf("%c",&ans);
if (ans=='Y' || ans=='y')
{
adjmatrix[i][j]=1;
}
else adjmatrix[i][j]=0;
}
}
return 0;
}
Thanks in advanceļ¼
You're right. The output is weird.
Your problem is scanf. Use scanf("%2c", ans);. Helped me.
But even then, i had the problem that the loops are not iterated
correctly.
Explanation:
The reason was the matrix. The matrix is not allocating any memory because its size if variable.
Therefore by setting an entry of the matrix to 1, this influences the variable j.
Solution:
You need to solve the problem to dynamically allocate memory.
Have a look at:
https://www.eskimo.com/~scs/cclass/int/sx9b.html
https://www.geeksforgeeks.org/dynamically-allocate-2d-array-c/
I wrote this program which takes n and k as input and then takes an array a[n] as input. The program must give the output as the total no of distinct integers in array a that are less than k and odd. But this program with every input is producing 0 as output.
#include<stdio.h>
int main()
{
long long int n,i,j,k,temp=-1;
scanf("%lld %lld",&n,&k);
long long int a[n];
for(i=0;i<n;i++)
scanf("%d",&a[i]);
long long int cnt=0;
for(i=0;i<n;i++)
{
if(a[i]<k)
{
if((a[i]%2)==1)
cnt++;}
}
for(i=0;i<(n-1);i++)
{
for(j=i+1;j<n;j++)
{
if(a[i]==a[j])
{
cnt--;
a[j]=temp;
--temp;
}
}
}
printf("%lld",cnt);
return 0;
}
scanf("%d", &a[i]);
Must be:
scanf("%lld", &a[i]);
check out the inner for loop, maybe you need to declare a variable and increment that and write and if statement that says, if variable==1 then print the desired value.
Not sure but might work.
According to the problem we have to find whether an element occurs more than n/2 times or not and then print Yes or No accordingly.
The numbers can vary from 10^-3 to 10^3.
I took an array count[2005] and then adding 1000 to each input to make 10^-3 equal to 0 i.e, -1000+1000=0 and then storing no.of occurrences of -1000 in count[0] and same for the rest elements. Therefore:
lower limit= -1000+1000=0;
higher limit= 1000+1000=2000;
But still I am getting memory access violation. Here is the link to the original problem: http://www.spoj.com/problems/MAJOR/
#include<stdio.h>
int main()
{
int t,n,a,count[2005],max,check,temp;
scanf("%d",&t);
while(t--)
{
check=0;
scanf("%d",&n);
for(int i=0;i<2005;i++)
count[i]=0;
for(int i=0;i<n;i++)
{
scanf("%d",&a);
temp=a+1000;
count[temp]++;
if(count[temp]>(n/2))
{
check=1;
max=temp-1000;
break;
}
}
if(check==1)
printf("YES %d\n",max);
else
printf("NO\n");
}
return 0;
}
Problem to your code is you are not reading input fully - SPOJ demands that your program should read input fully, not break in middle.
Solution to your problem: Just take an arr of size (10^6+1) and first read all inputs and then apply algorithm. Remember this if you break while reading input too, it will always be SIGSEGV. So, always read input fully on every programming website.
Here is your modified code and it is ready to get AC:
#include<stdio.h>
int arr[1000002]; // Array of Size (10^6+2)
int main()
{
int t,n,a,count[2005],max,check,temp,i;
scanf("%d",&t);
while(t--)
{
check=0;
scanf("%d",&n);
for(int i=0;i<2005;i++)
count[i]=0;
for(i=0;i<n;i++)
scanf("%d",&arr[i]); // READING INPUT FULLY IN AN ARRAY
for(i=0;i<n;i++)
{
a=arr[i]; // Now, a=arr[i] and previous algorithm applies now
temp=a+1000;
count[temp]++;
if(count[temp]>(n/2))
{
check=1;
max=temp-1000;
break;
}
}
if(check==1)
printf("YES %d\n",max);
else
printf("NO\n");
}
return 0;
}
I know this has been posted before but I tried to do on my way
I tried to code but which is showing erroneous result! obviously there is error in my logic..can anyone please explain me where am I having error?
here we are assuming that arrays are sorted in descending order!
int kthlargestsum(int a[], int b[],int k)
{
int aIndex=0;
int bIndex=0;
int sum=0;
int i;
for(i=0;i<k;++i)
{
if(a[aIndex]>b[bIndex])
{
sum+=a[aIndex];
++aIndex;
}
else
{
sum+=a[bIndex];
++bIndex;
}
}
printf("the output is %d",sum);
}
main()
{
int a[]={10,9,6,4,2};
int b[]={11,9,7,1};
int k;
printf("enter the value of k \n");
scanf("%d",&k);
kthlargestsum(a,b,k);
}
First off, a[bIndex] seems suspect to me. Perhaps you want b[bIndex]?
For anything else, you'll have to be more clear on what the problem you're trying to solve actually is (examples are always nice:). What you code appears to do is to add up k entries from the two arrays. This is now how I would interpret "Find kth largest of sum of elements in 2 array".