so,i'm trying to do a conversion(integer to string) and then add this string
with another.But it seems stringstream is not working..(it's normally working but the loop causes troubles)
I'm done with google
& almost tried everything but can't get this code to work..Anyone help me :( ?
#include <iostream>
#include <string>
#include <sstream>
using namespace std;
int main()
{
int n;
cin>>n;
string arr[n];
string a;
int i=0;
int s;
int c;
cin>>a;
arr[i] = a;
i++;
cout<<"OK"<<endl;
n--;
while(n--)
{
cin>>a;
s = 0;
for(int j=0;j<i;j++)
{
if(arr[j] == a)
{
s = 1;
break;
}
}
i++;
if(s == 0)
{
arr[i] = a;
cout<<"OK"<<endl;
}
else
{
c++;
stringstream ss;
ss<<c;
string m = ss.str();
a+=m;
arr[i] = a;
cout<<a<<endl;
ss.str("");
ss.clear();
}
}
return 0;
}
c is uninitialized, you should initialize it, and also s before using:
int s = 0;
int c = 0;
You can't use non-const variables for array initialization, consider writing:
constexpr int MAX_STRINGS = 5;
string arr[MAX_STRINGS];
The loop trouble is here:
i++
You're going over the boundaries at the last element. Just move i++ to the end of while loop.
Related
#include <stdio.h>
int main(){
int n, g, i;
scanf("%d\n", &n);
while(n--) {
int l = -1;
int c = 1;
scanf("%d", &g);
while(g--) {
scanf("%d", &i);
if (l == -1) l = i;
else if (i - 1 != l) break;
else l++;
c++;
}
fflush(stdin);
printf("%d\n", c);
}
}
I get all the outputs correctly, so I have no idea what could be wrong. On the other hand, Kattis accepts this code below that I found on GitHub, and the outputs are exactly the same as in my code. If anyone can explain to me what is wrong or why Kattis rejects my code I would appreciate it.
#include <bits/stdc++.h>
using namespace std;
int main()
{
//Initialize n and g, take in n
int n, g;
cin >> n;
//Iterate n times
while (n--)
{
//Take in g, initialize empty vector of size g
cin >> g;
vector<int> gnomes(g);
//Take in all the gnomes
for (int i = 0; i < g; i++) cin >> gnomes[i];
//Iterate through without the beginning or end since king won't be there
for (int i = 1; i < g-1; i++)
{
//Must break the order, and if you remove it the gnomes around it should be in order
if (gnomes[i] < gnomes[i-1] || gnomes[i] > gnomes[i+1] && gnomes[i-1] < gnomes[i+1])
{
//Output the 1 based index, so add 1
cout << i+1 << endl;
//And exit to the next group
break;
}
}
}
}
First of all, if you're using C++, use C++, not C!
As pointed out, fflush(stdin) is undefined behavior. Instead, you can use std::getline to chew up the rest of the line.
Other than that, your logic looks fine, even though it's a different approach than the overkill C++ solution. There's no need to check i + 1 or use a vector, as you seem to have deduced.
I suggest using clearer naming and whitespace conventions, though.
Here's a C solution, using scanf to read the rest of the line:
#include <stdio.h>
int main() {
int n;
scanf("%d\n", &n);
for (int i = 0; i < n; i++) {
int previous = -1;
int g;
scanf("%d", &g);
for (int j = 0; j < g; j++) {
int current;
scanf("%d", ¤t);
if (previous >= 0 && previous + 1 != current) {
printf("%d\n", ++j);
for (; j < g; j++) {
scanf("%d", ¤t);
}
break;
}
previous = current;
}
}
}
Here's a C++ solution that uses bits/stdc++.h and using namespace std;, which are common idioms in competitive programming but should never be used in any application code.
#include <bits/stdc++.h>
using namespace std;
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0); cout.tie(0);
int n;
cin >> n;
for (int i = 0; i < n; i++) {
int g;
cin >> g;
int previous = -1;
for (int j = 0; j < g; j++) {
int current;
cin >> current;
if (previous >= 0 && previous + 1 != current) {
cout << (1 + j) << "\n";
string s;
getline(cin, s);
break;
}
previous = current;
}
}
}
#include <iostream>
#include <string>
using namespace std;
int main() {
int T;
int count = 0;
cin>>T;
for(int i = 0; i<T; i++)
{
string str;
cin>>str;
count = 0;
This part is for checking how many occurrences of "xyz" are there in my code.
The test conditon j<=str.length() - 3 in for loop is causing error for some test case(s).
When I run the for loop test condition j<=str.length() it does not give error.
for(int j = 0;j<=str.length()-3; j++)
{
string x;
x = str.substr(j, 3);
if(x =="xyz")
{
count++;
}
}
if(count)
{
cout<<count<<endl;
}
else
{
cout<<"-1"<<endl;
}
}
return 0;
}
String lengths use unsigned math; 2-3 becomes 2^64-1. Try j<-3 || j+3<=str.length()
I want:
*!!
**!!!!
***!!!!!!
// And so on.
My attempt is below:
#include <iostream>
using namespace std;
int main()
{
int a;
int ex;
for (a = 1; a <= 5; a++)
{
cout<<"*";
for(ex =1; ex<= 2*a; ex++)
{
cout<<"!";
}
cout<<endl;
}
return 0;
}
I get this as the output:
*!!
*!!!!
*!!!!!!
//and so on...
It does what I need it to do for the second symbol but I don't know how to arrange the loops so that first symbol is outputted the desired number of times and not cut off by the second loop.
there is a small logical mistake in your code, you are only printing '*' once every loop. use the code below
#include <iostream>
using namespace std;
int main()
{
int a;
int ex;
for (a = 1; a <= 5; a++)
{
cout<<std::string((a),'*');
cout<<std::string((a*2),'!');
cout<<endl;
}
return 0;
}
You need to have the cout << '*' statement in a loop as well:
int main()
{
int a;
int ex;
for (a = 1; a <= 5; a++) // signifies the number of lines to print
{
auto i = 1;
while (i <= a) // prints * a times
{
cout<<"*";
++i;
}
for(ex =1; ex<= 2*a; ex++) // prints ! 2*a times
{
cout<<"!";
}
cout<<endl;
}
return 0;
}
You need another loop to print a-counted * symbols inside the main loop.
#include <iostream>
using namespace std;
int main()
{
int a;
int ex;
for (a = 1; a <= 5; a++)
{
for(int i = 0; i < a; ++i)
{
cout<<"*";
}
for(ex =1; ex<= 2*a; ex++)
{
cout<<"!";
}
cout<<endl;
}
return 0;
}
Another solution is:
#include <iostream>
using namespace std;
int main(){
int times = 5;
char simbol1 = '*', simbol2 = '!';
for(int i=1 ; i<=times ; i++){
for(int k=0; k<i; k++) cout << simbol1;
for(int j=0; j<i*2; j++) cout << simbol2;
cout << endl;
}
return 0;
}
I am unable to print the string after assigning every value of one string to another string. How to overcome this problem
#include <bits/stdc++.h>
using namespace std;
int main()
{
int n, k;
string s = "Nikhil", shiftedS;
n = s.length();
cin >> k;
for (int i = 0; i < n; i++)
{
int idx = (i + k) % n;
shiftedS[idx] = s[i];
}
shiftedS[n] = '\0';
for (int i = 0; i < n; i++)
cout << shiftedS[i] << " ";
cout << shiftedS; // I am unable to print when I try like this.
return 0;
}
You are getting unpredictable behavior because shiftedS is an empty string. If you initialize it like this
string shiftedS(n, ' '); // n is equal to length of "Nikhil"
and get rid of shiftedS[n] = '\0'; (C++ string object doesn't need this), it should work as expected. I tried it out with these changes and it worked for me.
why don't you try like this
#include<bits/stdc++.h>
using namespace std;
int main()
{
int n,k;
string s="Nikhil",shiftedS = "";
n=s.length();
cin>>k;
for(int i=0;i<n;i++)
{
int idx=(i+k)%n;
shiftedS+=s[i];
}
cout<<shiftedS;
return 0;
}
I have two arrays and I want to count how many elements are same between two arrays.
I try many times but the output is not correct. The correct should be 6 times
but the output is 4 times in the code.
Note: if s1 is "ss" and s2 is "ss", is the result 2
Here is my code:
#include <iostream>
#include <string>
using namespace std;
int main() {
char s1[] = "FOOBART";
char s2[] = "BFORATO";
int flag=0;
for(int i=0, j=0; i < sizeof(s1) && j < sizeof(s2); ) {
if(s1[i] == s2[j]) {
flag++;
i++;
j++;
} else if(s1[i] < s2[j]) {
i++;
} else {
j++;
}
}
cout << flag;
}
All elements of s1 are present in both strings so the output will be equal to the length of s1. Here is the correct code
#include <iostream>
using namespace std;
int main() {
char s1[] = "FOOBART";
char s2[] = "BFORATO";
int count=0;
for (int i=0; i<sizeof(s1)-1; i++) {
for (int j=0; j<sizeof(s2)-1; j++) {
if (s1[i]==s2[j]) {
count++;
break;
}
}
}
cout<<count<<endl;
}
Hope this will help you
solution using stl algorithms:
#include <iostream>
#include <algorithm>
#include <string>
int main()
{
const std::string s1 = "FOOBART";
std::string s2 = "BFORATO";
int count = 0;
auto beg = begin(s2);
for(auto& elm : s1)
{
auto x = find(beg, end(s2), elm);
if(x != end(s2))
{
*x = *beg;//get rid of elment and reduce the range of search.
++beg;
++count;
}
}
std::cout << count;
return 0;
}