I tried my best to solve Spoj problem No Squares numbers but I'm getting (TLE). Please tell how to approach. I'm unable to find any proper approach. Here is my code:
#include <bits/stdc++.h>
using namespace std;
#define size 10000004
int mark[size+1];
void sieve()
{
for(int i=2,k;(k=i*i)<=size;++i)
{
for(int j=k;j<=size;j=j+k)
mark[j]=-1;
}
}
int fn(int a,int b,int c)
{
int i,j,k,cnt=0;
for(i=a;i<=b;++i)
{
j=i;
if(mark[j]!=-1)
{
while(j>0)
{
if(j%10==c)
{
cnt+=1;
break;
}
else
j=j/10;
}
}
}
return cnt;
}
int main()
{
sieve();
int t,a,b,c;
cin>>t;
for(int i=0;i<t;t++)
{
cin>>a>>b>>c;
cout<<fn(a,b,c)<<endl;
}
}
Related
I implemented binary search in two ways and wondering which is more efficient? please help me know which is more efficient and how can it further be optimized? is time complexity remains same in both approach? I am a beginner in programming.
approach 1;
#include<iostream>
using namespace std;
bool BinarySearch(int*a,int n,int s ){
if(n==1){
if(a[0]==s)
return true;
else
return false;
}
else{
if(s<a[n/2]){
int U[n/2];
for(int i=0;i<n/2;i++){
U[i]=a[i];
}
return BinarySearch(U,n/2,s);
}
else{
int V[n-n/2];
for(int i=0;i<n-n/2;i++){
V[i]=a[i+n/2];
}
return BinarySearch(V,n-n/2,s);
}
}
}
int main(){
int array[10]={2,4,6,8,10,12,14,16,18,22};
cout<<BinarySearch(array,10,9);
}
approach 2:
#include<iostream>
using namespace std;
bool Bsearch(int arr[],int s,int l,int x){
cout<<"calling bsearch with arguments "<<s<<' '<<l<<' '<<x<<endl;
if(l==1)
return arr[s]==x;
int h=l/2;
if(x<arr[s+h])
return Bsearch(arr,s,h,x);
else
return Bsearch(arr,s+h,l-h,x);
}
int main(){
int marks[11]={17,18,20,22,24,26,28,30,32,34,36};
cout<<Bsearch(marks,0,11,32);
}
Thanks in advance for the kind help.
Other posters are correct that variable-length arrays are not good C++. If you define your main() as:
int main() {
std::array<int> marks{17,18,20,22,24,26,28,30,32,34,36};
cout<<Bsearch(marks,0,32);
}
or:
int main() {
std::vector<int> marks{17,18,20,22,24,26,28,30,32,34,36};
cout<<Bsearch(marks,0,32);
}
then you can drop the pointer/length pair in the parameter list to your binary-search function. The function prototype becomes something like:
bool Bsearch(const std::array<int>& arr, int s, int x);
#include<iostream>
#include <bits/stdc++.h>
using namespace std;
class knapsack
{
public:
int profit[4]={280,100,120,120};
int weight[4]={40,10,20,24};
int total=60;
float fraction[];
int fractotal=0;
int profittotal=0;
int getprofit=0;
float temp[4]={0,0,0,0};
float temp1=0;
float temp2=0;
float temp3=0;
float temp4=0;
void displayvalues()
{
int i;
cout<<"The profit and weight of the item"<<"\n";
cout<<"The Profit"<<" ";
for(i=0;i<4;i++)
{
cout<<profit[i]<<" ";
}
cout<<"\n"<<"The weights"<<" ";
for(i=0;i<4;i++)
{
cout<<weight[i]<<" ";
}
}
float fractionalknapsack()
{
int i,j;
for(i=0;i<4;i++)
{
fraction[i]=profit[i]/weight[i];
}
cout<<"\n"<<"The values are"<<"\n";
for(i=0;i<4;i++)
{
profittotal=profittotal+profit[i];
}
for(i=0;i<4;i++)
{
cout<<fraction[i]<<"\n";
fractotal= fractotal+fraction[i];
}
if(fractotal<=total)
{
cout<<"The maximum possible value is"<<profittotal;
}
else if(fractotal>=total)
{
for(i=0;i<4;i++)
{
for(j=i+1;j<4;j++)
{
if(fraction[i]<fraction[j])
{
temp2=fraction[i];
fraction[i]=fraction[j];
fraction[j]=temp2;
temp3=profit[i];
profit[i]=profit[j];
profit[j]=temp2;
temp4=weight[i];
weight[i]=weight[j];
weight[j]=temp4;
}
}
}
for(i=0;i<4;i++)
{
while(getprofit<total)
{
if(getprofit+weight[i]<=total)
{
temp[i]=1;
getprofit+=weight[i];
}
else
{
temp[i]=(total-getprofit)/weight[i];
getprofit=total;
i+=1;
}
}
cout<<temp;
}
}
for(i=0;i<4;i++)
{
temp1+=profit[i]*temp[i];
}
cout<<temp1;
}
};
int main()
{
knapsack k;
k.displayvalues();
k.fractionalknapsack();
}
So here if you see i have am solving fractional knapsack question with my approach but there is a error in sort operation so can someone give me the solution for this. It would really helpfull. So in the function of fractional knapsack you will se a else if where I have to do sort if the next value to the previous value is greater i used sort operation but is not working. So someone can help me with this.
Here is my code for quick sort. I am a beginner kindly please help.
#include<iostream>
using namespace std;
class quick
{
private:
int n,left,right,i,j;
float a[55];
public:
void getdata();
void sort(float[],int,int);
void putdata();
};
void quick::getdata()
{
cout<<"Enter how many elements you want to enter:";
cin>>n;
for(int k=0;k<n;k++)
{
cout<<"Enter percentage of students:"<<k+1<<":";
cin>>a[k];
}
left=0;
right=n-1;
}
void quick::putdata()
{
for(int k=0;k<5;k++)
{
cout<<"\nSorted marks are:"<<a[k]<<endl;
}
}
void quick::sort(float a[],int left,int right)
{
if(left<right)
{
int i=left;
int j=right+1;
float pivot=a[left];
do{
do{
i++;
}while((a[i]<pivot)&& left<right);
do{
j--;
}while(a[j]>pivot);
if(i<j)
swap(a[i],a[j]);
}while(i<j);
a[left]=a[j];
a[j]=pivot;
sort(a,left,j-1);
sort(a,j+1,right);
}
}
int main()
{
quick obj;
obj.getdata();
obj.sort(a[],left,right);
obj.putdata();
return (0);
}
It is giving me error in int main() function:
a is not declared in this scope.
expected primary expression before ']'.
As the answer is given by #Shubham Khatri. Here is the corrected code.
#include<iostream>
using namespace std;
class quick
{
public: int n,left,right,i,j;
float a[55];
public:
void getdata();
void sort(float[],int,int);
void putdata();
};
void quick::getdata()
{
cout<<"Enter how many elements you want to enter:";
cin>>n;
for(int k=0;k<n;k++)
{
cout<<"Enter percentage of students:"<<k+1<<":";
cin>>a[k];
}
left=0;
right=n-1;
}
void quick::putdata()
{
for(int k=0;k<n;k++)
{
cout<<"\nSorted marks are:"<<a[k]<<endl;
}
}
void quick::sort(float a[],int left,int right)
{
if(left<right)
{
int i=left;
int j=right+1;
float pivot=a[left];
do{
do{
i++;
}while((a[i]<pivot)&& left<right);
do{
j--;
}while(a[j]>pivot);
if(i<j)
swap(a[i],a[j]);
}
while(i<j);
a[left]=a[j];
a[j]=pivot;
sort(a,left,j-1);
sort(a,j+1,right);
}
}
int main()
{
quick obj;
obj.getdata();
obj.sort(obj.a,obj.left,obj.right);
obj.putdata();
return (0);
}
As it mentions you have not declared a as a variable inside the int main(). Rather it is an object of quick. In a function you don't pass array like a[] rather as a only .since a, left, right are a private variable of a class you can't access it from the object directly. Declare it as public and use it as obj.a, obj.left, obj.right inside sort function.
Complete code:
#include<iostream>
using namespace std;
class quick
{
public:
int n,left,right,i,j;
float a[55];
void getdata();
void sort(float[],int,int);
void putdata();
};
void quick::getdata()
{
cout<<"Enter how many elements you want to enter:";
cin>>n;
for(int k=0;k<n;k++)
{
cout<<"Enter percentage of students:"<<k+1<<":";
cin>>a[k];
}
left=0;
right=n-1;
}
void quick::putdata()
{
for(int k=0;k<5;k++)
{
cout<<"\nSorted marks are:"<<a[k]<<endl;
}
}
void quick::sort(float a[],int left,int right)
{
if(left<right)
{
int i=left;
int j=right+1;
float pivot=a[left];
do{
do{
i++;
}while((a[i]<pivot)&& left<right);
do{
j--;
}while(a[j]>pivot);
if(i<j)
swap(a[i],a[j]);
}while(i<j);
a[left]=a[j];
a[j]=pivot;
sort(a,left,j-1);
sort(a,j+1,right);
}
}
int main()
{
quick obj;
obj.getdata();
obj.sort(obj.a,obj.left,obj.right);
obj.putdata();
return (0);
}
#include<iostream>
using namespace std;
int main() {
int a,b,c,d;
c=0;
for(a=100;a<1000;a++) {
for(b=a;b>0;b=b/10) {
d=b%10;
c=c+d*d*d;
}
if(c==a) {
cout<<c<<endl;
}
}
return 0;
}
You have to initialize c before each check.
#include<iostream>
using namespace std;
int main()
{
int a,b,c,d;
c=0;
for(a=100;a<1000;a++)
{
c=0; // add this
for(b=a;b>0;b=b/10)
{
d=b%10;
c=c+d*d*d;
}
if(c==a)
{
cout<<c<<endl;
}
}
return 0;
}
I am solving Critical links problem on UVA.The problem is about finding the bridges in the Graph.I used the same algorithm here.But I am continuously getting wrong answer.
Please suggest what's wrong with my code.
//Bridges in a Graphs
#include<cstdio>
#include<cstring>
#include<iostream>
#include<vector>
#include<stack>
#include<utility>
#include<cstdlib>
#include<algorithm>
using namespace std;
#define MAX 205
int parent[MAX],timer=0,low[MAX],disc[MAX];
bool vis[MAX];
vector<pair<int,int> >st;
vector<vector<int> >G(MAX);
bool cmp(const pair<int,int> a,pair<int,int> b)
{
if(a.first==a.second)
return a.second<b.second;
else
return a.first<b.first;
}
void reset()
{
st.clear();
memset(parent,-1,sizeof parent);
memset(vis,false,sizeof vis);
for(int i=0;i<=MAX;i++)
G[i].clear();
}
void dfs(int u)
{
vis[u]=true;
disc[u]=low[u]=timer++;
for(int i=0;i<G[u].size();i++)
{
int v=G[u][i];
if(!vis[v])
{
parent[v]=u;
dfs(v);
low[u]=min(low[u],low[v]);
if(low[v]>low[u])
st.push_back(make_pair(min(u,v),max(u,v)));
}
else if(v!=parent[u])
low[u]=min(low[u],disc[v]);
}
}
int main()
{
int n,t=0;
while(cin>>n)
{
reset();
if(t>0)
cout<<endl;
if(n==0)
{
cout<<"0"<<" critical links\n";break;
}
int node,count;
for(int j=0;j<n;j++)
{
scanf("%d (%d)",&node,&count);
for(int i=0;i<count;i++)
{
int x;
scanf("%d",&x);
G[node].push_back(x);
G[x].push_back(node);
}
}
for(int i=0;i<n;i++)
{
if(!vis[i])
dfs(i);
}
sort(st.begin(),st.end(),cmp);
cout<<st.size()<<" critical links\n";
for(int i=0;i<st.size();i++)
cout<<st[i].first<<" - "<<st[i].second<<endl;
t++;
}
}
Finally I got it,little bit flaw in std::sort cmp function and it is
bool cmp(const pair<int,int> a,pair<int,int> b)
{
if(a.first==b.first)
return a.second<b.second;
else
return a.first<b.first;
}
also in dfs function low[v]>disc[u] instead of low[v]>low[u].