For problem statement, please see this.
(I have omitted libraries.)
My code works fine (when online judge tests the code) except for this case:
aaaaaaaaaaaaaaaaaaaaaaaaaaaaa
bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb
I would love to know why output it expects to be 104 when intersection is zero?
std::string string_intersection;
int number_needed(string a, string b) {
int y = a.length();
int z = b.length();
sort(a.begin(), a.end());
sort(b.begin(), b.end());
std::set_intersection(a.begin(), a.end(), b.begin(), b.end(), std::back_inserter(string_intersection));
int num, x = string_intersection.length();
if(x==0)
num = 0;
else
num = y + z - 2*x;
return num;
}
int main(){
string a;
cin >> a;
string b;
cin >> b;
cout << number_needed(a, b) << endl;
return 0;
}
Just change
if(x==0)
num = y+z; //as everything needs to be deleted
else
num = y + z - 2*x;
Related
I need help solving this issue, I am expecting a number to come out but get this error instead
Line 65: Char 5: error: conflicting types for 'main' int main(int argc, char *argv[]) { ^ Line 47: Char 5: note: previous definition is here int main() ^ 1 error generated.
here is some of my code
class Solution {
public:
int value(char r){
if (r == 'I')
return 1;
if (r == 'V')
return 5;
if (r == 'X')
return 10;
if (r == 'L')
return 50;
if (r == 'C')
return 100;
if (r == 'D')
return 500;
if (r == 'M')
return 1000;
return -1;
}
int romanToInt(string& s) {
int ret = 0;
for (int i = 0; i < s.length(); i++) {
int s1 = value(s[i]);
if (i + 1 < s.length()) {
int s2 = value(s[i + 1]);
if (s1 >= s2) {
ret = ret + s1;
}
else {
ret = ret + s2 - s1;
i++;
}
}
else {
ret = ret + s1;
}
}
return ret;
}
};
int main()
{
Solution m;
string str = "III";
cout << "Integer form of Roman Numeral is " << m.romanToInt(str) << endl;
return 0;
}
I am trying to use a pointer array where it reads the line letter by letter and recognizing the value of the letter in the function value(), I think I understand that my main needs to be formatted differently in order to do this task but I am a little stuck on how to do so.
You have probably defined int main twice. Considering you have an error on line 65 while your code is less than 60 lines long I would assume there is more code than what was copied here.
I just want to know that for finding the Krishnamurthy number, we have to first find the factorial of the digits, then the addition of those numbers. (like, 1!+4!+5! = 145).
So, below is my code, and I have applied a factorial function over there. But the output is not coming in favor (145 is not a Kri...).
#include <stdio.h>
#include <stdlib.h>
void main()
{
int digit,factorial = 1, temp, input, sum = 0;
printf("Enter a Number:\n");
scanf("%d",&input);
int Factorial(int digit){
factorial = factorial*digit;
return 0;
}
temp = input;
while(temp>0){
digit = temp%10;
temp = temp/10;
sum = sum + Factorial(digit);
}
if(sum==input){
printf("%d is a Krishnamurthy Number",input);
}
else{
printf("%d is not a Krishnamurthy Number",input);
}
}
Have I done anything wrong in logic, or function declaration or definition? Please help.
your factorial function is not performing correctly. factorial means, multiplication of all digits starting from n downto 1 -
(n-1) * (n-2) * ... * (n)
but your function is not giving the desired result you want.
int Factorial(int digit){
factorial = factorial*digit;
return 0;
}
you need to change that function to get the factorial value of a number, you can either iterate a loop downto one or use a recursive approach to get the factorial.
int Factorial(int digit){
int result = 1;
for(int i=n; i>=1; i--){
result *= i;
}
return result;
}
or
int Factorial(int digit) {
if(n <= 1) return digit;
return digit * Factorial(digit-1);
}
you can follow the thread to understand the depth of recursive function mentioned above.
#include<stdio.h>
int main(int argc, char* argv[], char* envp[])
{
int sum = 0,
int a,
int p = 0,
int d,
int i,
int fact;
//code
printf("Enter a number: ");
scanf("%d", &a);
p = a;
while (a > 0)
{
fact = 1;
d = a % 10;
a /= 10;
for (i = d; i >= 1; i--)
{
fact *= i;
}
sum += fact;
}
if (sum == p)
printf("It is a Krishnamurthy number.\n");
else
printf("It is not a Krishnamurthy number.\n");
printf("\n\n");
return(0);
}
The problem is fairly simple and straight .However i cannot solve it exclusively using dp knapsack style . I have solution for that but since the number of coins in each denomination here is limited (in this question it's )it's creating a problem . I want to arrive at a 2d recurrence that i can use to implement the knapsack .
My actual solution which runs fine goes below :
#include<iostream>
using namespace std ;
int main ()
{ int flag ;
int N,i ;
int sum ;
int counter ;
while (cin >> N >>sum )
{
counter = 0;
flag= 0 ;
int count =0;
for( i = N ; i>=1;i--){
count += i ;
counter++;
if (count >sum){
count-=i ;
counter -- ;
}
if(count == sum){
flag = 1 ;
break ;
}
}
if (flag==1)
cout << counter<<"\n" ;
else cout <<-1<<"\n" ;
}
return 0 ;
}
Dynamic programming solution is not required for the problem as the constraint are quite high.A simple greedy approach would work fine.
The algorithm works as follows:
If the sum of all the values form 1 to n is less than m then you cannot pay m coins because even after using all the n coins you have money remaining to be paid.
If the sum of all the values form 1 to n is greater than or equal to m then you can definitely pay because you have enough coins.
Since you have to minimize the number of coins to be given you consecutively pick coins that have the maximum value until your m become zero and you need to only keep track of number of coins that you have picked.
Below is the code that implements the above algorithm
#include <bits/stdc++.h>
using namespace std;
int main() {
int n, m;
cin >> n >> m;
if (m > (n * (n + 1) / 2)) {
//if sum of all coins form 1 to n is less than m
cout << "-1";
} else {
int cnt = 0;
while (m != 0) {
if (n >= m) {
m = 0;
} else {
m -= n;
--n;
}
++cnt;
}
cout << cnt;
}
return 0;
}
I can't see my output from this code I have written. I'm trying to calculate the mean and standard deviation of a set of numbers from a file. I'm lost as to what the problem is and I won't know if my code is right until I can see output. Here's what I have so far:
#include <iostream>
#include <cmath>
#include <fstream>
using namespace std;
int main()
{
// Declare Variables
int n;
int xi;
int sdv;
int sum;
int sum2;
int sum3;
int mean;
// Declare and open input files
ifstream inData;
inData.open("score.dat");
if (!inData) // Incorrect Files
{
cout << "Cannot open input file." << endl;
return 1;
}
// Initialize variables and output
inData >> xi;
n = 0;
sum = 0;
sum3 = 0;
sdv = 0;
mean = 0;
while (inData)
{
sum += xi;
sum2 = sum * sum;
sum3 += (xi * xi);
mean = sum / n;
sdv = (sum3 - sum2) / (n * (n - 1));
inData >> xi;
}
// Print commands
cout << "The Standard Deviation of the Tests is:" << sdv << endl;
cout << "The Mean of the Tests is: " << mean << endl;
inData.close();
system("pause");
return 0;
}
After running your code I found a few bugs. You probably don't get any output because the program crashes the first time through the while loop on the line:
mean = sum / n;
due to a divide by zero error.
The other bug is that you don't increase (increment) the value of n in your loop, so you always only have one number.
Adding an n++ at the beginning of your loop will fix that
while (inData)
{
n++;
sum += xi;
...
But you still get a divide by zero on the sdv when n == 1:
sdv = (sum3 - sum2) / (1 * (1 - 1));
if you add a condition before the division it will work:
if (n >= 2)
sdv = (sum3 - sum2) / (n * (n - 1));
Look into debugging tools like gdb to help catch things like this.
I want to know efficient approach for the New Lottery Game problem.
The Lottery is changing! The Lottery used to have a machine to generate a random winning number. But due to cheating problems, the Lottery has decided to add another machine. The new winning number will be the result of the bitwise-AND operation between the two random numbers generated by the two machines.
To find the bitwise-AND of X and Y, write them both in binary; then a bit in the result in binary has a 1 if the corresponding bits of X and Y were both 1, and a 0 otherwise. In most programming languages, the bitwise-AND of X and Y is written X&Y.
For example:
The old machine generates the number 7 = 0111.
The new machine generates the number 11 = 1011.
The winning number will be (7 AND 11) = (0111 AND 1011) = 0011 = 3.
With this measure, the Lottery expects to reduce the cases of fraudulent claims, but unfortunately an employee from the Lottery company has leaked the following information: the old machine will always generate a non-negative integer less than A and the new one will always generate a non-negative integer less than B.
Catalina wants to win this lottery and to give it a try she decided to buy all non-negative integers less than K.
Given A, B and K, Catalina would like to know in how many different ways the machines can generate a pair of numbers that will make her a winner.
For small input we can check all possible pairs but how to do it with large inputs. I guess we represent the binary number into string first and then check permutations which would give answer less than K. But I can't seem to figure out how to calculate possible permutations of 2 binary strings.
I used a general DP technique that I described in a lot of detail in another answer.
We want to count the pairs (a, b) such that a < A, b < B and a & b < K.
The first step is to convert the numbers to binary and to pad them to the same size by adding leading zeroes. I just padded them to a fixed size of 40. The idea is to build up the valid a and b bit by bit.
Let f(i, loA, loB, loK) be the number of valid suffix pairs of a and b of size 40 - i. If loA is true, it means that the prefix up to i is already strictly smaller than the corresponding prefix of A. In that case there is no restriction on the next possible bit for a. If loA ist false, A[i] is an upper bound on the next bit we can place at the end of the current prefix. loB and loK have an analogous meaning.
Now we have the following transition:
long long f(int i, bool loA, bool loB, bool loK) {
// TODO add memoization
if (i == 40)
return loA && loB && loK;
int hiA = loA ? 1: A[i]-'0'; // upper bound on the next bit in a
int hiB = loB ? 1: B[i]-'0'; // upper bound on the next bit in b
int hiK = loK ? 1: K[i]-'0'; // upper bound on the next bit in a & b
long long res = 0;
for (int a = 0; a <= hiA; ++a)
for (int b = 0; b <= hiB; ++b) {
int k = a & b;
if (k > hiK) continue;
res += f(i+1, loA || a < A[i]-'0',
loB || b < B[i]-'0',
loK || k < K[i]-'0');
}
return res;
}
The result is f(0, false, false, false).
The runtime is O(max(log A, log B)) if memoization is added to ensure that every subproblem is only solved once.
What I did was just to identify when the answer is A * B.
Otherwise, just brute force the rest, this code passed the large input.
// for each test cases
long count = 0;
if ((K > A) || (K > B)) {
count = A * B;
continue; // print count and go to the next test case
}
count = A * B - (A-K) * (B-K);
for (int i = K; i < A; i++) {
for (int j = K; j < B; j++) {
if ((i&j) < K) count++;
}
}
I hope this helps!
just as Niklas B. said.
the whole answer is.
#include <algorithm>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <iterator>
#include <map>
#include <sstream>
#include <string>
#include <vector>
using namespace std;
#define MAX_SIZE 32
int A, B, K;
int arr_a[MAX_SIZE];
int arr_b[MAX_SIZE];
int arr_k[MAX_SIZE];
bool flag [MAX_SIZE][2][2][2];
long long matrix[MAX_SIZE][2][2][2];
long long
get_result();
int main(int argc, char *argv[])
{
int case_amount = 0;
cin >> case_amount;
for (int i = 0; i < case_amount; ++i)
{
const long long result = get_result();
cout << "Case #" << 1 + i << ": " << result << endl;
}
return 0;
}
long long
dp(const int h,
const bool can_A_choose_1,
const bool can_B_choose_1,
const bool can_K_choose_1)
{
if (MAX_SIZE == h)
return can_A_choose_1 && can_B_choose_1 && can_K_choose_1;
if (flag[h][can_A_choose_1][can_B_choose_1][can_K_choose_1])
return matrix[h][can_A_choose_1][can_B_choose_1][can_K_choose_1];
int cnt_A_max = arr_a[h];
int cnt_B_max = arr_b[h];
int cnt_K_max = arr_k[h];
if (can_A_choose_1)
cnt_A_max = 1;
if (can_B_choose_1)
cnt_B_max = 1;
if (can_K_choose_1)
cnt_K_max = 1;
long long res = 0;
for (int i = 0; i <= cnt_A_max; ++i)
{
for (int j = 0; j <= cnt_B_max; ++j)
{
int k = i & j;
if (k > cnt_K_max)
continue;
res += dp(h + 1,
can_A_choose_1 || (i < cnt_A_max),
can_B_choose_1 || (j < cnt_B_max),
can_K_choose_1 || (k < cnt_K_max));
}
}
flag[h][can_A_choose_1][can_B_choose_1][can_K_choose_1] = true;
matrix[h][can_A_choose_1][can_B_choose_1][can_K_choose_1] = res;
return res;
}
long long
get_result()
{
cin >> A >> B >> K;
memset(arr_a, 0, sizeof(arr_a));
memset(arr_b, 0, sizeof(arr_b));
memset(arr_k, 0, sizeof(arr_k));
memset(flag, 0, sizeof(flag));
memset(matrix, 0, sizeof(matrix));
int i = 31;
while (i >= 1)
{
arr_a[i] = A % 2;
A /= 2;
arr_b[i] = B % 2;
B /= 2;
arr_k[i] = K % 2;
K /= 2;
i--;
}
return dp(1, 0, 0, 0);
}