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.
}
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'm trying to input a number in each structure but I keep getting numbers I didn't even input.
#include <stdio.h>
#include <stdlib.h>
struct reg {
int course_num;
};
int main() {
struct reg arr_reg[2];
int i;
for (i = 0; i < 2; i++) {
printf("Enter course number: ");
scanf("%d"), &arr_reg[i].course_num;
}
for (i = 0; i < 2; i++) {
printf("course number: %d\n ", arr_reg[i].course_num);
}
}
A simple fix, move the , &arr_reg[i].course_num into the parenthesis after the closing quotation mark. Check your IDE's error flags whenever they pop up.
Simple mistake ,change "scanf("%d"), &arr_reg[i].course_num;"
to "scanf("%d",&arr_reg[i].course_num);"
#include <stdio.h>
#include <stdlib.h>
struct reg{
int course_num;
};
int main()
{
struct reg arr_reg[2];
int i;
for(i = 0; i < 2; i++)
{
printf("Enter course number: ");
scanf("%d",&arr_reg[i].course_num);
}
for(i = 0; i < 2; i++)
{
printf("course number: %d\n ", arr_reg[i].course_num);
}
return 0;
}
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 3 years ago.
Improve this question
While solving the code for the problem Plus Multiply on CodeChef, I tried to solve it using binary search, but my solution gives a TLE.
I have written the following code:
#include <stdio.h>
int main()
{
int t;
scanf("%d",&t);
for(int a=0;a<t;a++)
{
int n;
scanf("%d",&n);
int arr[n];
for(int i=0;i<n;i++)
scanf("%d",&arr[i]);
int s=0,j,k,f,m,mm;
(n%2==0)?(mm=n/2+1):(mm=n/2+1);
for(j=0;j<mm;j++)
{
f=n-j-1;
for(k=j+1;k<n-1;k++)
{
m=n-k-1;
if((arr[j]*arr[k])==(arr[j]+arr[k]))
s++;
if((arr[f]*arr[m])==(arr[f]+arr[m]))
s++;
}
if((arr[j]*arr[k])==(arr[j]+arr[k]))
s++;
}
printf("%d\n",s);
}
return 0;
}
Kindly suggest me on how to improve the time complexity of the above code.
As per your solution, the code looks to me as of complexity O(n^2).
But, as per the given constraints, the question expects me to solve it in O(n) time complexity.
The property, ab = a+b is special and exclusive only to 0 and 2.
Therefore, just by finding the number of pairs of 0's and 2's.
And it can be calculated using combinations.
Have a look at the following code:
#include <bits/stdc++.h>
using namespace std;
typedef long long int LL;
int main(){
int t;
cin>>t;
while(t--){
int n;
cin>>n;
LL z = 0; //Zero count
LL t = 0; //One count
while(n--){
int x;
cin>>x;
if(x==0){
z++;
}
if(x==2){
t++;
}
}
cout<<((z)*(z-1LL)/2LL) + ((t)*(t-1LL)/2LL)<<endl;
}
return 0;
}
Time Complexity: O(N)
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 3 years ago.
Improve this question
I have an issue with a function I've created. It doesn't return 1. I tried using the code in main instead of in the function and it worked.
I don't know if I'm missing something or what so please check it out and tell me:
int happynum(int n); // THIS IS THE FUNCTION
int main()
{
int num,digito,i,dig,temp,sum=0, lol;
do {
printf("Escribe un numero positivo:\n");
scanf("%d", &num);
} while (num <= 0);
lol = happynum(num); // THIS IS WHAT THE FUNCTION RETURN
printf("%d ", lol);
while(num!=89 && num!=1) //THIS IS THE SAME FUNCTION BUT IN THE MAIN
{
sum=0;
while(num>0)
{
dig=num%10;
num=num/10;
sum=sum+(dig*dig);
}
num=sum;
}
printf("%d", num);
/*
if(num== 1)
{
printf("Happy Number\n");
}
else
printf("UnHappy Number\n"); */
return 0;
}
This is the function:
int happynum(int n) // THIS IS THE FUNCTION
{
int i,dig,num,sum=0;
while(num!=89 && num!=1)
{
sum=0;
while(num>0)
{
dig=num%10;
num=num/10;
sum=sum+(dig*dig);
}
num=sum;
}
return num;
}
You're using the num local to happynum instead of the parameter that you pass. Name the argument num and then use that, like so:
int happynum(int num) // THIS IS THE FUNCTION
{
int i,dig,sum=0;
while(num!=89 && num!=1)
{
sum=0;
while(num>0)
{
dig=num%10;
num=num/10;
sum=sum+(dig*dig);
}
num=sum;
}
return num;
}
You should then probably un-comment the block at the end of your main so you get the Un/Happy Number result.
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;
}
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I need to make an array that accepts each number just once and if the user tries to insert a number more than once,then he must enter an other number...can anyone help me please?
I have tried this so far:
#include<stdio.h>
#include<stdlib.h>
int main(void)
{
int a[5];
int i,j,num;
scanf("%d",&num);
a[0]=num;
for(i=1;i<5;++i){
again: scanf("%d",&num);
if(a[i]!=a[i-1])
a[i]=num;
else
goto again;
}
for(i=0;i<5;i++)
printf("%4d\n",a[i]);
system("pause");
return 0;
}
but the code just doesn't work and i don't know why
it is observed from your code that given array must be filled but it should not contain redundant values.
following code iterates until the array is filled with no redundant value, once the array is filled it terminates.
int a[5],i=1,k=0,p;
int num;
scanf("%d",&num);
a[0]=num;
while(i<5)
{
scanf("%d",&num);
for(p=0;p<=k;p++)
{
if(a[p]==num)
{
break;
}
if(p==(k))
{
a[i]=num;
k=i;
i++;
}
}
}
for(i=0;i<5;i++)
{
printf("%d",a[i]);
}
hope this could help you
You are just comparing the new entered value with the previous one in a[i]!=a[i-1].
You better create a function to test the new value with the entire array, like
int valueExists(int num, int a[], int len) {
int i;
for (i = 0; i < len; i++) {
if (a[i] == num) {
return 1;
}
}
return 0;
}
Make sure you adding the new value to the array only after testing the value is not there.
again: scanf("%d",&num);
if (valueExists(num, a, i)) {
goto again;
} else {
a[i] = num;
}
(The loop you created with the goto can be replaced by a do-while loop, but that is not the issue here)