I am stuck on this question getting WA.
I have seen many bottom up implementations of this question. My top-down implementation is not working with memoization and is working fine without it. How can i correct it ??
#include<cstdio>
#include<cstring>
#include<iostream>
#define INF 0x7FFFFFFF
using namespace std;
int o,n,num,ox[2000],nt[2000],wt[2000];
int dp[2000][2000];
int dive(int index,int oxygen,int nitrogen,int weight) {
if(dp[oxygen][nitrogen]!=-1) return dp[oxygen][nitrogen];
int &ret=dp[oxygen][nitrogen];
if(oxygen>=o&&nitrogen>=n) {
ret=weight;
return ret;
}
if(index==num) {
ret=INF;
return ret;
}
ret= min(dive(index+1,oxygen+ox[index],nitrogen+nt[index],weight+wt[index]),dive(index+1,oxygen,nitrogen,weight));
return ret;
}
main() {
int c;
scanf("%d",&c);
while(c--) {
memset(dp,-1,sizeof(dp));
scanf("%d%d",&o,&n);
scanf("%d",&num);
for(int i=0;i<num;++i) {
scanf("%d%d%d",&ox[i],&nt[i],&wt[i]);
}
printf("%d\n",dive(0,0,0,0));
}
return 0;
}
Try this test:
1
21 79
5
1 1 800
1 1 800
1 1 800
1 1 800
17 75 800
It seems to me the correct answer for it should be 800 * 5 = 4000 right? Your program outputs something else.
This gives wrong answer too :( could someone check pls
#include<iostream>
using namespace std;
int swap( int &a,int &b)
{
int temp=a;
a=b;
b=temp;
}
int min(int a, int b)
{
if(a<b)return a;else return b;
}
int max(int a,int b)
{
if(a>b)return a;else return b;
}
int minweight(int ans[][22][80],int arr[][3],int n,int oxy,int nit)
{
int prev=0,curr=1;
for(int i=1;i<=n;i++)
{
for(int j=0;j<=21;j++)
for(int k=0;k<=79;k++)
ans[curr][j][k]=min( ans[prev][j][k], arr[i][2]+ans[prev][max(0,j-arr[i][0])][max(0,k-arr[i][1])]);
swap(curr,prev);
}
return ans[n&1][oxy][nit];
}
int main()
{
int cases;
cin>>cases;
while(cases--)
{
int oxy,nit;
cin>>oxy>>nit;
int n;
cin>>n;
int arr[n+1][3];
for(int i=1;i<=n;i++)
{
cin>>arr[i][0]>>arr[i][1]>>arr[i][2];
}
int ans[2][22][80];
for(int j=0;j<oxy+1;j++)
for(int k=0;k<nit+1;k++)
ans[0][j][k]=9999;
ans[0][0][0]=0;
cout<<minweight(ans,arr,n,oxy,nit);
}
}
Related
I am trying to run this code on online compilers and it shows Runtime errors while it runs perfectly locally. The parts of code that are not being used have been commented out.
#include <iostream>
#include <string>
#include <cmath>
using namespace std;
int main() {
// your code goes here
ios_base::sync_with_stdio(0);
cin.tie(NULL);
cout.tie(NULL);
int t;
cin>>t;
while(t--)
{
string s;
cin>>s;
int x,y;
cin>>x>>y;
string ans;
int noOfApple=0,noOfBanana=0;
for(int i=0;i<s.size();i++)
{
if(s[i]=='a')
{
noOfApple++;
}
else
{
noOfBanana++;
}
}
int noOfABlock=ceil(noOfApple/x);
//int noOfBBlock=ceil(noOfBanana/y);
int length=floor(noOfBanana/(noOfABlock-1));
int remainderB=noOfBanana%(noOfABlock-1);
int counter=0;
//condition for '*'
if(noOfBanana<(noOfABlock-1))
{
int q=noOfBanana;
for(int i=0;i<(noOfABlock-1);i++)
{
for(int j=0;j<x;j++)
{
ans[counter++]='a';
}
if(q>0)
{
ans[counter++]='b';
q--;
}
else
{
ans[counter++]='*';
}
}
for(int i=0;i<x;i++)
{
ans[counter++]='a';
}
}
else{
for(int i =0;i<(noOfABlock-1);i++)
{
for(int j=0;j<x;j++)
{
ans[counter++]='a';
}
for(int j=0;j<length;j++)
{
ans[counter++]='b';
}
while(remainderB>0)
{
remainderB--;
ans[counter++]='b';
}
}
for(int i=0;i<x;i++)
{
ans[counter++]='a';
}
}
cout<<ans<<endl;
}
return 0;
}
Input for this program:-
6
ab
1 1
aab
1 1
aabb
1 2
aaaaab
2 1
aaaa
1 3
aaaaa
2 2
When Arrays are used we have to initialize the size of the arrays as they are static but as string are dynamic we do not need to initialize with a size.
ive been trying to add 2 large integer on c++ using array and the output is always 0000 and i cant identified the problem
what im trying to achieve is like 30000000000 + 50000000 = 30050000000.
and this is the code that i wrote
, can anyone help me trying to figure it out why the output is 0000?
#include <math.h>
using namespace std;
int array1[100];
int array2[100];
int hasil[100];
int func1(int n)
{
int b;
while(b>0)
b=sizeof(array1[100]);
array1[b]=n%10;
n=n/10;
b--;
}
int func2(int p)
{
int b;
while(b>0)
b=sizeof(array1[100]);
array2[b]=p%10;
p=p/10;
b--;
}
int func3(int i)
{ int satuan,puluhan,bil;
for(i=sizeof(array2[100]);i>0;i--)
{
int bil = array1[100]+array2[100];
satuan=bil%10;
hasil[i]=satuan+puluhan;
puluhan=bil/10;
}
}
int main ()
{ int n,p,i;
int func1(n);
int func2(p);
int func3(i);
int satuan=0;
int puluhan=0;
int x,y;
cout<<"masukan bilangan pertama = ";
cin>>n; cout<<endl;
cout<<"masukan bilangan ke dua = ";
cin>>p; cout<<endl;
for(x=sizeof(hasil[100]);x>0;x--)
{
y=0;
cout<<hasil[y];
y++;
}
} ```
created NOT,AND,OR and XOR functions(using NAND) then created the 'adder_prim' and 'adder_carry' functions for the primary output and the carry. Used the standard full adder circuit and put it in a loop(10 cycles so should be able to add til 2031).
entered the input in the code itself(X is 139,Y is 74) just to see if it's working properly or not.
instead of 216(correct ans) it's coming 196 and i have no clue why.
int NAND(int i,int j)
{
int A;
A=((i==1)&&(j==1))?0:1;
return A;
}
int NOT(int i)
{
int A=NAND(i,i);
return A;
}
int AND(int i,int j)
{
int A=NOT(NAND(i,j));
return A;
}
int OR(int i,int j)
{
int A=NAND((NAND(i,i)),NAND(j,j));
return A;
}
int XOR(int i,int j)
{
int A=OR(AND(i,NOT(j)),AND(NOT(i),j));
return A;
}
int adder_prim(int,int,int);
int adder_carry(int,int,int);
int _tmain(int argc, _TCHAR* argv[])
{
int Z[10];
int C=0;
int X[]={0,0,1,0,0,0,1,0,1,1};
int Y[]={0,0,0,1,0,0,1,0,1,0};
for(int i=0;i<10;i++)
{
Z[i]=adder_prim(X[i],Y[i],C);
C=adder_carry(X[i],Y[i],C);
}
for(int j=0;j<10;j++)
{
cout <<Z[j];
}
getch();
return 0;
}
int adder_prim(int a,int b,int c)
{
int O=XOR(XOR(a,b),c);
return O;
}
int adder_carry(int a,int b,int c)
{
int C=OR(AND(XOR(a,b),c),AND(a,b));
return C;
}
You need to add the least significant bit first, not the most significant bit:
for(int i=9;i>-1;i--)
{
Z[i]=adder_prim(X[i],Y[i],C);
C=adder_carry(X[i],Y[i],C);
}
# include <iostream>
using namespace std;
class mm
{
private:
int k[1000];
int n;
int i;
int a;
int b;
int f;
public:
mm ()
{
a=0;
b=1;
f=0;
i=0;
for(int i=0; i<n;i++)
k[i]=0;
};
~mm()
{
}
void fib(int n)
{
for (int i=0;i<n;i++)
{
if (i<=1)
f=i;
else
{
f=a+b;
a=b;
b=f;
}
k[i]=f;
}
for (int j=0;j<n;j++)
cout<<k[j]<<" ";
}
int se (int n, int i)
{
if (n==1)
return 1;
else
return 1/k[i] + se (n-1, i+1);
}
};
int main()
{
int n;
cout<<"Enter n:";
cin>>n;
mm p;
cout<<"fib: "<<endl;
p.fib(n);
cout<<endl;
cout<<"se: ";
cout<<p.se(n,0);
return 0;
}
Recursion function from main is not responding. Maybe the array k[i] is not working, but I cant find the reason. Can anyone help me?
k[0] is set to 0. When you then call se(n,0) in main, it computes 1/k[0] + se(n-1,1) which is a division by zero.
#include<iostream.h>
#include<conio.h>
#include<math.h>
double Det (double a[2][2], int size);
void form(double a[2][2], int c, int size);
double b[2][2];
double Det(double a[2][2], int size) {
if (size==1) {
return a[0][0];
}
else {
double ans = 0.0;
int c;
for (c=0;c<size;c++) {
form(a,c,size);
ans += pow(-1.0,(double)c)*Det(b,size-1);
}
return ans;
}
}
void form(double a[2][2], int c, int size) {
int i,j=0,k,l=0;
for (i=0;i<size;i++) {
for (j=0;j<size;j++) {
if ((i!=0)&&(j!=c)) {
if (l==size-1) {
k++;
l=0;
}
b[k][l]=a[i][j];
l++;
}
}
}
}
int main() {
double mat[2][2] = {{1.0,2.0},{3.0,7.0}};
cout << Det(mat,2);
getch();
return 0 ;
}
I am writing a program in C++ to calculate the determinant of a matrix using recursion. I know that there are many better algorithms to do the same. However, I have been asked to implement this one. As a test case, I have used the matrix specified in the code. When I this run this code, I get an answer of -4 instead of 1. I do not understand what is wrong with this code. Could someone please help? Thanks in advance.
you made several mistakes in your code, use the one below
#include<iostream>
#include<math.h>
using namespace std;
double Det (double a[2][2], int size);
void form(double a[2][2], int c, int size);
double b[2][2];
double Det(double a[2][2], int size)
{
if(size==1)
return a[0][0];
else
{
double ans=0.0;
int c;
for(c=0;c<size;c++)
{
form(a,c,size);
ans+=pow(-1.0,(double)c) * a[0][c] * Det(b,size-1);
}
return ans;
}
}
void form(double a[2][2], int c, int size)
{
int i,j=0,k = 0,l=0;
for(i=0;i<size;i++)
{
for(j=0;j<size;j++)
{
if((i!=0)&&(j!=c))
{
if(l==size-1)
{
k++;
l=0;
}
b[k][l]=a[i][j];
l++;
}
}
}
}
int main()
{
double mat[2][2]={{1.0,2.0},{3.0,7.0}};
cout<<Det(mat,2);
return 0 ;
}
This gives you 1. The mistakes, you forget to initialize k to 0, and you forget to multiply a[0][c].