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
Consider the following series sk=s(k-1)(!s(k-1))(!s(k-1))s(k-1). Take s1=1221 and !s1=2112, therefore s2=s1(!s1)(!s1)s1, which means s2=1221211221121221.
My current goal is to generate n elements of this series and then to determine the n-th element. I'm sorry if I made the question sound complicated.
I tried making this work but the code just doesn't show the correct answer or it doesn't work at all. I'd hope to see another perspective on this problem.
This is my code:
#include <iostream>
using namespace std;
int main()
{
int n,k=1,a,b,s=1,p=1,i=1;
a=1;b=2;
cin>>n;
while(i<=n){
if(s==1){
if(k==1){
cout<<a;
i++;
k++;
}
else{if(k==2||k==3){
cout<<b;
i++;
k++;
}
if(k==4){
cout<<a<<" ";
i++;
k=1;p++;
if(p==2) {s=2;p=0;}
}
}}
if(i<=n){
if(s==2){
if(k==1){
cout<<b;
i++;
k++;
}
else{if(k==2||k==3){
cout<<a;
i++;
k++;
}
if(k==4){
cout<<b<<" ";
i++;
k=1;p++;
if(p==2) {s=1;p=0;}
}
}
}
}}
return 0;
}
Here is one possible solution for your sequence generator.
#include <iostream>
#include <string>
// generate inverted term s -> !s
std::string invert(const std::string& s) {
const char a = '1';
const char b = '2';
std::string out;
for (int i = 0; i < s.size(); i++) {
// here one can check for invalid input as well, eg 0
out += s[i] == a ? b : a;
}
return out;
}
// sk = s(k - 1)(!s(k - 1))(!s(k - 1))s(k - 1)
std::string nextSeq(const std::string& s) {
std::string s_inv = invert(s);
return s + s_inv + s_inv + s;
}
// generate nth term of the sequence.
std::string nthSeq(const std::string& s, const int n) {
std::string out = s;
for (int i = 1; i < n; i++) {
out = nextSeq(out);
}
return out;
}
int main() {
std::string s1 = "1221";
// this will 2nd term 1221211221121221
std::cout << nthSeq(s1, 2) << std::endl;
return 0;
}
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
When I pass only Sum as Parameter in My coin Exchange Problem to find no of ways to find the amount Sum..
Example
if the coins are {2,3,5} and the desired sum is 9 i get correct output as 8.
This code :
#include <bits/stdc++.h>
using namespace std;
int n, sum, a[105];
int fun(int sum) {
int S = 0;
if(sum == 0) return 1;
if(sum < 0) return 0;
for(int i=1;i<=n;i++) {
S += fun(sum - a[i]);
}
return S;
}
int main()
{
cin >> n;
cin >> sum;
for(int i=1;i<=n;i++ ) {
cin >> a[i];
}
cout << fun(sum);
return 0;
}
But when I also give Current Index as a parameter it gives me output as 3 which is wrong.
This code :
#include <bits/stdc++.h>
using namespace std;
int n, sum, a[105];
int fun(int sum, int idx) {
int S = 0;
if(sum == 0) return 1;
if(idx > n) return 0;
if(sum < 0) return 0;
for(int i=idx;i<=n;i++) {
S += fun(sum - a[i], i);
}
return S;
}
int main()
{
cin >> n;
cin >> sum;
for(int i=1;i<=n;i++ ) {
cin >> a[i];
}
cout << fun(sum,1);
return 0;
}
But WHY??
Is my parameter passing in this case is wrong?
You don't have the same result between your 2 solutions because in the first, at each "function call", you will iterate from 1 to N and in your second solution, you will iterate from Index to N. So the number of loop will be different.
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 2 years ago.
Improve this question
I'm getting WA(Wrong Answer) in "Compress the List" problem Code(CMPRSS) of CodeChef, here is the link of the problem: https://www.codechef.com/problems/CMPRSS
I've checked the sample output given in the problem and also some self made test cases and It's working properly.I'm not getting what's wrong in my code
here is my approach:
#include <bits/stdc++.h>
using namespace std;
vector<int> number;
int main() {
int t;
cin>>t;
while(t--){
int n;
cin>>n;
for(int i=0;i<n;i++){
int temp;
cin>>temp;
number.push_back(temp);
}
int i;
for(i=0;i<n-1;i++){
int count = 0;
int temp = i;
while (number[temp+1]-number[temp]==1){
temp++;
count++;
}
if(count>1){
if(i+count==n-1){
cout<<number[i]<<"..."<<number[i+count]<<"\n";
} else{
cout<<number[i]<<"..."<<number[i+count]<<",";
}
i = i + count;
}
else{
cout<<number[i]<<",";
}
}
if(i!=n){
cout<<number[n-1]<<"\n";
}
number.clear();
}
return 0;
}
You forgot to check if number[temp+1] exists in the part
while (number[temp+1]-number[temp]==1){
temp++;
count++;
}
Therefore, it may read beyond the array and produce wrong output.
Try this case:
2
5
1 2 3 4 5
3
1 2 3
The part should be like this:
while (temp+1 < static_cast<int>(number.size()) && number[temp+1]-number[temp]==1){
temp++;
count++;
}
Your logic is partially correct because you forgot to check the upper limit index of vector: Please check the missing code in the bold letter or between ** code **:
#include <bits/stdc++.h>
using namespace std;
vector number;
int main() {
int t;
cin>>t;
while(t--){
int n;
cin>>n;
for(int i=0;i<n;i++){
int temp;
cin>>temp;
number.push_back(temp);
}
int i;
for(i=0;i<n-1;i++){
int count = 0;
int temp = i;
while (**temp+1<n &&** number[temp+1]-number[temp]==1){
temp++;
count++;
}
if(count>1){
if(i+count==n-1){
cout<<number[i]<<"..."<<number[i+count]<<"\n";
} else{
cout<<number[i]<<"..."<<number[i+count]<<",";
}
i = i + count;
}
else{
cout<<number[i]<<",";
}
}
if(i!=n){
cout<<number[n-1]<<"\n";
}
number.clear();
}
return 0;
}
Check my solution.
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 8 years ago.
Improve this question
i want to say if the value of the last element in the vector = 4 cout<<"Yes" how can i write the code write if(v.size()==y) where y is a number but it dosen't work i begin recently at writing codes
#include <iostream>
#include <vector>
#include <stdio.h>
using namespace std;
int main()
{
int n,x,y;
cin >> n >> x >> y;
vector<int> v(n);
for(int i = 0; i < n; i++)
{
cin >> v[i];
}
for(int i = 0; i < n; i++)
{
if(v[0] == x)
{cout<<"EASY";
return 0;
}
if(v[0] == x && v.back() == y)
{
cout << "BOTH";
return 0;
}
if(v.back()==y)
{
cout << "HARD";
return 0;
}
if(v[0] != x && v.back() != y)
{
cout << "OKAY";
return 0;
}
}}
v.size() will returns the number of the elements in v, not the last element in the vector.
To get the last value of a vector, you can simply call std::vector::back().
Of course, you have to make sure the vector is not empty before that by checking std::vector::empty.
if (!v.empty() && v.back()==y)
{
...
}
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 8 years ago.
Improve this question
I need to create a program that searches how many comments (// symbols) are in .txt file.
This is my code:
#include <fstream>
using namespace std;
const char read[] = "read.txt";
const char result[] = "result.txt";
const int CMax = 256;
void Skaityti (char E[], int& n);
int main() {
char E[CMax];
int n,k;
Skaityti(E,n);
ofstream rs(result);
rs << k;
return 0;
}
void Skaityti (char E[], int & n)
{
ifstream fd(read);
int k;
char sim = '/';
for (n = 0; !fd.eof() && n < n+1; n++)
fd.get(E[n]);
for(int i = 0; i < n;i++) {
if(sim == E[n])
k++; }
fd.close();
}
Program read fine, but I can't get symbol from massive.
I'm confused by your question... but I think you're asking to see how many times "//" appears in a file. I threw this together:
#include <fstream>
using namespace std;
int Skaityti()
{
ifstream fd("test.txt");
int count = 0;
while(fd.good())
{
char c = fd.get();
if(fd.good())
if(c == '/')
{
c = fd.get();
if(fd.good())
if(c == '/')
count++; // At this point we have two comments in a row
}
}
fd.close();
return count;
}
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 8 years ago.
Improve this question
How can I get the dValues[] in the line double dValues[] = {what should i input here?}?Because I'm using an array. The goal is to get the mode.
#include <iostream>
#include <vector>
#include <numeric>
using namespace std;
double GetMode(double daArray[], int iSize) {
// Allocate an int array of the same size to hold the
// repetition count
int* ipRepetition = new int[iSize];
for (int i = 0; i < iSize; ++i) {
ipRepetition[i] = 0;
int j = 0;
bool bFound = false;
while ((j < i) && (daArray[i] != daArray[j])) {
if (daArray[i] != daArray[j]) {
++j;
}
}
++(ipRepetition[j]);
}
int iMaxRepeat = 0;
for (int i = 1; i < iSize; ++i) {
if (ipRepetition[i] > ipRepetition[iMaxRepeat]) {
iMaxRepeat = i;
}
}
delete [] ipRepetition;
return daArray[iMaxRepeat];
}
int main()
{
int count, minusElements;
float newcount, twocount;
cout << "Enter Elements:";
std::cin >> count;
std::vector<float> number(count);
cout << "Enter " << count << " number:\n";
for(int i=0; i< count ;i++)
{
std::cin >> number[i];
}
double dValues[] = {};
int iArraySize = count;
std::cout << "Mode = "
<< GetMode(dValues, iArraySize) << std::endl;
You already have all the values in your number vector, but if you wanted to copy those values into a new array called dValues, you have to allocate it on the heap (since you don't know the size at compile-time), copy the elements from the vector, and later free up that memory:
double *dValues = new double[number.size()];
for (size_t i = 0; i < number.size(); i++)
{
dValues[i] = number[i];
}
// whatever you need to do with dValues
delete [] dValues;
You're also not checking that you're within the bounds of your vector in the for loop. A safer implementation would use the push_back() method on the vector rather than assigning the values by index.
If I understood you correctly, you wish to copy the elements from the vector to array. If yes -
float *dValues = new float[count] ; // Need to delete[] when done
std::copy( number.begin(), number.end(), dValues );
std::copy is in algorithms header. But why do you want to use/create raw array for this task. You already have the vector number and just pass it to GetMode(..)