Spoj LastDigit Wrong answer [closed] - c

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 5 years ago.
Improve this question
Link to the problem
Spoj LastDigit problem
I have tested my code on my machine but in SPOJ it is showing wrong answer.
Here is my code-
#include<stdio.h>
int main()
{
int num=0;
scanf("%d",&num);
while(num--)
{
int a=0;
unsigned long int b;
scanf("%d\t%lu",&a,&b);
a%=10;
b%=100;
if(a==0||a==1)
printf("%d\n",a);
else if(b==0)
printf("1\n");
else if(a==5)
printf("5\n");
else
{
int d=b%4,e=1;
while(d--)
e*=a;
printf("%d\n",e%10);
}
}
return 0;
}

Your program is buggy. You are assuming cycle of length 4 for all digits except 0,1,5. That's incorrect
For instance, consider the input1
2 4
Your program outputs 1 whereas the answer should be last digit of Power(2,4) = last digit of 16 = 6

In SPOJ you get a WA because your program is used to calculate the last digit of not so big number. In the problem, it is clearly mentioned that the value of 'b' lies between '0' and '2,147,483,000' inclusive.
The correct way to solve the problem is by using Modular Exponentiation.

# Wasim Thabraze I used the modular approach but my solution is not accepted because
my solution is taking more than than 700bytes
#include<iostream>
using namespace std;
int lastdigit(int a,long int b);
int main()
{
int t,a;
long int b;
cin>>t;
while(t--)
{
cin>>a>>b;
cout<<lastdigit(a,b)<<"\n";
}
return 0;
}
int lastdigit(int a,long int b)
{ if(b==0){return 1;}
if(a%10==0){return 0;}
if(a%10==1){return 1;}
if(a%10==5){return 5;}
if(a%10==6){return 6;}
int ret,mod=b%4;
if(a%10==2||a%10==8||a%10==4)
{
if(a%10==8){if(mod==1){mod=3;}else if(mod==3){mod=1;}}
if(a%10==4){if(mod==1||mod==3){mod=2;}else if(mod==2){mod=0;}}
if(mod==1){ret= 2;}
else if(mod==2){ret =4;}
else if(mod==3){ret =8;}
else if(mod==0){ret= 6;}
}
else if(a%10==3||a%10==7||a%10==9)
{
if(a%10==7){if(mod==1){mod=3;}else if(mod==3){mod=1;}}
if(a%10==9){if(mod==1||mod==3){mod=2;}else if(mod==2){mod=0;}}
if(mod==1){ret= 3;}
else if(mod==2){ret= 9;}
else if(mod==3){ret= 7;}
else if(mod==0){ret= 1;}
}
return ret;
}

Related

I am making a prime number checker: when I try to run it, it stopped working [closed]

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 3 years ago.
Improve this question
#include <stdio.h>
int main() {
int x, i, counter = 0;
printf("Input number!\t");
scanf("%d", &x);
for (i = 0; i <= x; i++) {
if (x % i == 0) {
counter++;
}
}
if (counter <= 2) {
printf("%d is a prime number.", x);
} else {
printf("%d is not a prime number.", x);
}
return 0;
}
It seems the loop part is the problem but I don't know why. I'm very new to programming so please bear with it if its a silly mistake.
Try this Code.
Its going to Infinity when divide by ZERO after giving input. Make
sure loop start with 1, when division inside loop
#include <stdio.h>
int main()
{
int x,i,counter=0;
printf("Input number!\t");
scanf("%d",&x);
for(i=1;i<=x;i++)
{
if(x%i==0)
{
counter++;
}
}
if(counter<=2)
{
printf("%d is a prime number.",x);
}
else
{
printf("%d is not a prime number.",x);
}
return 0;
}
As #Some programmer dude mentioned, you can't do the x%0 because of division by 0 - this occured on first iteration.
So change your loop to starting from 1 like below:
for(i=1;i<=x;i++)
{
if(x%i==0)
{
counter++;
}
}

me on with my code on codechef elementary [closed]

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 years ago.
Improve this question
I face problems with my code when I enter 1 3 4 . although I couldn't find any error as it works perfectly with other numbers / ps. the was built as a solution to the codechef problem POTATOES
Problem summary: Write a program that inputs an integer T followed by T lines containing two space-separated positive integers. For each of these lines, output the smallest number (>1) that, when added to the sum of these two numbers, results in a sum that is a prime number.
and my code is
#include<stdio.h>
#include<math.h>
int prime(int a,int b);
int main() {
int c;
scanf("%d",&c);
int a,b,d[c];
for(a=0; a<c; a++) {
int x,y;
scanf("%d %d",&x,&y);
b=(x+y);
if(prime(x,y)-b!=0)
d[a]=prime(x,y)-b;
else d[a]=prime((x+1),y)-b;
}
for(a=0; a<c; a++)printf("%d\n",d[a]);
return 0;
}
int prime(int a,int b) {
int c,e;
for(c=2; c<(a+b); c++) {
if((a+b)%c==0) {
b++;
continue;
}
return(a+b);
}
}
Your code is wrong. In your function prime() due to the statement b++; , (a+b) is changing in (a+b)%c but the incremented c is never went back.So for bigger prime numbers your code will fail
Eg:- (89,1) (79,1) etc
Also You don't need that d[c] in your code. You don't need to store every output.When you compute one output just print it .That is ok with code-chef. Also You can divide the problem into to functions.
Try this simplified code :-
#include <stdio.h>
#include <math.h>
int prime(int n);
int make_prime(int a);
int main()
{
int c;
scanf("%d", &c);
int a, b;
for (a = 0; a < c; a++)
{
int x, y;
scanf("%d %d", &x, &y);
b = (x + y);
printf("%d\n", make_prime(b));
}
return 0;
}
int make_prime(int a)
{
int c=1;
while(prime(a+c)==0){
c++;
}
return c;
}
int prime(int n){ // simple prime function
int i,flag=1;
for (i = 2; i <= (n)/2; i++)
{
if ((n) % i == 0)
{
flag=0;
break;
}
}
return flag;
}

Why is my program not giving correct output? [closed]

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
Where is it logically wrong? I don't find it incorrect but the output it
gives is just 1. It should give all the Armstrong number from 1 to 500.
#include<stdio.h>
#include<conio.h>
void main()
{
clrscr();
int a,b,c=0 ,d,i=1;
while(i<=500)
{
b=i;
while(b>0)
{
a=b%10;
c=(a*a*a)+c;
b=b/10;
}
if(c==i)
` printf("%d",i);
i++;
}
getch();
}
You need to initialize c before the inner loop:
while(i<=500)
{
b=i;
c=0; /* reset 'c' */
while(b>0)
{
a=b%10;
c=(a*a*a)+c;
b=b/10;
}
}
You are using non-standard signature for main(). See: What should main() return in C and C++?
if you run the following code
you will see why there is only one output.
Note: correct declaration of main()
Note: using common functions rather than the proprietary conio.h
Note: uses simple 'for' statement rather than 'while' and increment of 'i'
#include <stdio.h>
#include <stdlib.h>
//#include<conio.h>
int main()
{
//clrscr();
int a;
int b;
int c=0;
int i=1;
for( ; i<=500; i++ )
{
b=i;
while(b>0)
{
a=b%10;
c=(a*a*a)+c;
b=b/10;
}
printf( "a=%d. b=%d, c=%d\n", a,b,c);
if(c==i)
printf("%d\n",i);
} // end for
//getch();
getchar();
return(0);
} // end function main

Stone codechef: What is wrong with the code? [closed]

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 new to coding and finding my way!! I am solving a problem at codechef http://www.codechef.com/problems/RRSTONE.
All tests cases are passed at my side but the answer is still wrong. Any Help???
//codechef stones
#include<stdio.h>
int main(int argc, char *argv[])
{
unsigned int size; //N=size
unsigned long int k; //k = K
scanf("%u %lu",&size,&k);
long int a[size], max;
int i,j;
for(i=0;i<size;i++)
scanf("%lu",&a[i]);
i=j=0;
if(k==0)
goto printing;
if(k%2==1)
k=1;
else
k=2;
for(j=0;j<k;j++)
{
max=a[0];
for(i=0;i<size-1;i++)
{
if(max<a[i])
max=a[i];
}
for(i=0;i<size;i++)
a[i]=max-a[i];
}
printing: //removed this comment
for(i=0;i<size-1;i++)
printf("%d ",a[i]);
printf("%d",a[i]);
return 0;
}
I suggest you should look at the criteria properly.
Constraints
1 <= N <= 105
0 <= K <= 109
Ai does not exceed 2 * 10^9 by it's absolute value.
Here is modified code try with this.
And one more thing: Since you are learning understand the code first.
#include<stdio.h>
int main(int argc, char *argv[])
{
unsigned int size; //N=size
unsigned long int k; //k = K
scanf("%u %lu",&size,&k);
long long int a[size], max; //CHANGE HERE!!!! and so forth for long long
int i,j;
for(i=0;i<size;i++)
scanf("%lld",&a[i]);
i=j=0;
if(k!=0)
{
if(k%2==1)
k=1;
else
k=2;
for(j=0;j<k;j++)
{
max=a[0];
for(i=1;i<size;i++)
{
if(max<a[i])
max=a[i];
}
for(i=0;i<size;i++)
a[i]=max-a[i];
}
}
for(i=0;i<size;i++)
printf("%lld ",a[i]);
return 0;
}
EDIT: Sorry I forgot to put this note. There is issue sometimes with goto when you are using online compilers, so better to try with a clean approach and make sure you are selecting the correct option for source language (C-gcc or C99 whatever you're using) . Earlier I have experienced the same.

SPOJ Addrev answer not accepted [closed]

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 8 years ago.
Improve this question
I am trying to submit the solution but I am getting wrong answer, but according to me it works fine. There is also the correct code.
My Code
#include<stdio.h>
int rev(int num){
int rev = 0;
while(num>=10){
rev = rev + num%10;
rev=rev*10;
num = num/10;
}
rev = rev + num;
}
int main(){
int T;
int num1,num2;
scanf("%d",&T);
while(T--){
scanf("%d",&num1);
scanf("%d",&num2);
printf("%d\n",rev(rev(num1)+rev(num2)));
}
return 0;
}
While code that got accepted is
#include<stdio.h>
int rev(int num)
{
int temp=0;
while(num)
{
temp=(temp*10)+(num%10);
num/=10;
}
return temp;
}
int main()
{
int t;
scanf("%d",&t);
while(t--)
{ int num1,num2;
scanf("%d%d",&num1,&num2);
printf("%d\n",rev(rev(num1)+rev(num2)));
}
return 0;
}
the code was take from http://codegeeksblog.wordpress.com/2013/05/30/spoj-addrev/
I cant find the reason why my code is not accepted! I get wrong answer message from judge.
You should return this num to make the whole function work as hinted by msandiford.
Proceed like this in your last step...
int rev(int num){
int rev = 0;
while(num>=10){
rev = rev + num%10;
rev=rev*10;
num = num/10;
}
rev = rev + num;
return rev; // added a return statement.
}

Resources