How can I add a changing integer variable to string? - c++

There is something wrong in the if condition but it's the 7th day I have no clue. It prints the taken integer like this
mike .. mike1 .. mike12 .. mike123
But actually I need it to be like this
mike .. mike1 .. mike2 .. mike3
Can anyone help? this is my code :
#include <bits/stdc++.h>
using namespace std;
string str(int o){
stringstream ss;
ss<<o;
return ss.str();}
int main(){
int n;
cin>>n;
int z[n];
for(int p=0;p<n;p++)
{
z[p]=-50;
}
string x[n];
int k;
for(int i=0;i<n;i++){
k=1;
cin>>x[i];
for(int j=0;j<i;j++){
if(x[j]== x[i]){
x[i] = x[j] + str(k) ;
k++;
z[i]=0;
}
}
}
for(int q=0;q<n;q++){
if(z[q]==0)
cout<<x[q]<<endl;
else
cout<<"OK"<<endl;
}
return 0;
}

Remove the second for loop and get k outside the loop like this:
k=1;
for(int i=0;i<n;i++){
cin>>x[i];
x[i] = x[i] + str(k) ;
k++;
z[i]=0;
}

You can simplify all your code with C++11
#include <bits/stdc++.h>
using namespace std;
int main() {
int n, k = 1;
cin >> n;
vector<bool> z(n, false);
vector<string> x(n);
for (int i = 0; i < n; ++i) {
cin >> x[i];
x[i] += to_string(k);
++k;
z[i] = true;
}
for (int i = 0; i < n; ++i)
cout << (z[i] ? x[i] : "OK") << endl;
return 0;
}

Related

the program is stuck while giving output

I dont know why the program is failed to give output, This is the code to find number of zero's
in a given array
#include<iostream>
using namespace std;
int main()
{
int n;
cin>>n;
int arr[n];
for(int i=0; i<n; i++)
{
cin>>arr[i];
}
int b=0 , a;
for(int j=0; j<n; j++)
{
a=arr[j];
while(a==0)
{
b=b++;
}
}
cout<<b;
}
Try changing it to
if (a==0) //you had a while here
{
b=b++;
}
Since you are not changing the value of 'a' inside the loop, it gets stuck inside the while and this leads to an infinite loop!
#include <iostream>
using namespace std;
int main()
{
int n;
cin >> n;
int arr[n];
for (int i = 0; i < n; i++)
{
cin >> arr[i];
}
int b = 0, a = 0;
for (int j = 0; j < n; j++)
{
a = arr[j];
if (a == 0) //here you used while use if as you are checking the condition every iteration of loop.
{
b++; //here instead of using b=b++ use this.
}
}
cout << b;
return 0; //return is optional.
}
just do this in the j for loop. you don't need any a variable or anything
and also runtime variable arrays are not allowed in C++ use dynamic arrays or vectors
for(int j=0; j<n; j++)
{
if(arr[j]==0){
b++;
}
}

How to count inversions faster

I am trying to make my inversion counting program run faster since I am continuously getting TLE.
I used C++ and this is my code
#include <iostream>
#include <stdio.h>
using namespace std;
int main()
{
int n;
int count = 0;
cin >> n;
int arr[n];
for(int i = 0; i < n; i++)
{
cin >> arr[i];
}
for(int i = 0; i < n - 1; i++)
{
for(int j = i + 1; j < n; j++)
if(arr[i] > arr[j])
{
count++;
}
}
cout << count;
}
Help please!

I want to print the following pattern

1
3 2
6 5 4
10 9 8 7
I want to print the following pattern. I have tried very hard but couldn't make the code for it. I have tried everything which came up to my mind.
#include<iostream>
using namespace std;
int main() {
int i, j, n;
cin >> n;
int k = 0;
for (i = 1;i <= n; i++) {
for (j = 1; j <= i; j++) {
k++;
printf("%d ", k);
}
printf("\n");
}
}
the other code which i tried is this.
#include<iostream>
using namespace std;
int main() {
int i, j, n;
cin >> n;
int k = 0;
for (i = 1; i <= n; i++) {
for (j = i; j >= 1; j--) {
k++;
printf("%d ",j);
}
printf("\n");
}
}
#include <iostream>
#include <stack>
using namespace std;
int main()
{
int previousRow = 0;
for(int row = 1; row <= 4; row++)
{
int rowTracker = row;
for(int col = 0; col < row; col++)
{
cout<<rowTracker - col + previousRow<<" ";
}
previousRow += row;
cout<<endl;
}
return 0;
}
#include<iostream>
void printPattern(unsigned numlevels)
{
unsigned last_num = 1;
for(unsigned i = 0; i < numlevels; ++i)
{
unsigned next_num = i + last_num;
for(unsigned j = next_num; j >= last_num; --j)
{
std::cout << j << ' ';
}
std::cout << '\n';
last_num = next_num + 1;
}
}
int main()
{
unsigned n;
std::cin >> n;
printPattern(n);
return 0;
}
You may also use a stack to implement this. Here is a working answer:
#include <iostream>
#include <stack>
using namespace std;
int main() {
int i, j, n;
stack<int> st;
cin >> n;
int k = 0;
for(i = 1;i <= n; i++) {
for(j = 1; j <= i; j++) {
k++;
st.push(k);
}
while(!st.empty()){
printf("%d ", st.top());
st.pop();
}
printf("\n");
}
}
Hope it helps!
Before reading the code blow, you should really try to do it yourself. This problem is obviously for practice and to develop the programming muscle. Just getting the answer is not going to help.
The issue with your code is that for each row, the range you want to print is not being determined correctly. You should first find the range and then print the numbers. Ther can be multiple approaches to it. Below is one of them.
for(i=1;i<=n;i++){
int max = i*(i+1)/2;
int min = i*(i-1)/2 + 1;
for(j=max;j>=min;j--){
printf("%d ",j);
}
printf("\n");
}
Here is a simple method
int main(int argc, char* argv[])
{
int n = 4; // suppose print 4 lines
for (int i = 1; i <= n; ++i)
{
int i0 = (i + 1) * i / 2; // first number of line i
for (int j = 0; j < i; j++)
cout << i0 - j << " ";
cout << endl;
}
return 0;
}
thanx everyone for your responses. I was able to do it on my own. Below is what I did. if there is any correction let me know
#include<iostream>
using namespace std;
int main(){
int i,j,n,temp;
cin>>n;
int k=0;
for(i=1;i<=n;i++){
k=k+i,temp=k;
for(j=1;j<=i;j++){
cout<<temp<<+" ";
temp--;
}
cout<<("\n");
}
}

print stars as much as the values in the array

I'm trying to make a C++ program start creating an array and takes the values from the user , then print every value + star as much the value is .. Example : the user had entered 5 then the output must be like this
5*****
Input
1
2
3
4
5
6
output
1*
2**
3***
4****
and so on
.. help :(
#include <iostream>
using namespace std;
void main()
{
int arr[10];
for (int i = 0; i < 10; i++)
{
cin >> arr[i];
int x = arr[i];
for (int j = 0; x <= arr[i]; j++)
{
cout<< "*";
}
}
}
And another help please can you give me some useful link to practice on programming to be professional
Your code is wrong. Use the following code:
#include <iostream>
using namespace std;
int main() {
int arr[10];
for (int i = 0; i < 10; i++)
{
cin >> arr[i];
int x = arr[i];
for (int j = 0; j < x; j++){ // your condition was wrong
cout<< "*";
}
cout<<endl; // for better formatting
}
return 0;
}
For edited question
int main() {
int arr[10];
for (int i = 0; i < 10; i++)
{
cin >> arr[i];
}
for (int i = 0; i < 10; i++)
{
int x = arr[i];
cout << x;
for (int j = 0; j < x; j++){ // your condition was wrong
cout << "*";
}
cout << endl;
}
return 0;
}
#include <iostream>
using namespace std;
void main()
{
int nbValues = 10;
int arr[nbValues];
// First recover the values
for (int i = 0; i < nbValues; i++)
{
cin >> arr[i];
}
// Then print the output
for (int i = 0; i < nbValues; i++)
{
int x = arr[i];
cout << x;// Print the number
for (int j = 0; j < x; j++)
{
cout<< "*";// Then print the stars
}
cout << endl;// Then new line
}
}

Google Code Jam Minimum Scalar Product

I was solving a problem from Google Code Jam and I am not able to solve the problem: http://code.google.com/codejam/contest/32016/dashboard#s=p0 (Minimum Scalar Product, Problem A 2008)
The strategy I used was:
Accept v1 and v2 from the user
Sort both v1 and v2
Reverse v2 i.e. sort v2 in descending order
Multiply straight-out corresponding v1[i] * v2[i] and store the result in product
Sum up all such products and print the answer
I did some research and indeed it appears that's the only permutation that's possible to obtain. However, my code does not produce the correct output:
#include <stdio.h>
#include <algorithm>
#include <iostream>
using namespace std;
int main()
{
int T;
int cases;
FILE *fin = fopen ("A-small-practice.in", "r"); // open input file
FILE *fout = fopen ("output.out", "w");
fscanf(fin, "%d", &T);
for(cases = 1; cases <= T; cases++)
{
int v1[1000], v2[1000];
int i,j; int n;
int product =0;
fscanf(fin, "%d", &n);
for(i=0; i < n; i++)
{
fscanf(fin, "%d",&v1[i]);
fscanf(fin, "%d", &v2[i]);
}
sort(v1,v1+n);
sort(v2,v2+n);
reverse(v1, v1+n);
int k;
for(k = 0; k < n; k++)
{
product += v1[k] * v2[k];
}
fprintf(fout, "Case #%d: %d\n", cases, product);
}
return 0;
}
You should use long long.
This worked for me:
#include <iostream>
#include <algorithm>
using namespace std;
int main() {
long long T,n,v1[1000],v2[1000];
cin >> T;
for (int t = 1; t <= T; t++) {
cin >> n;
for (int i = 0; i < n; i++)
cin >> v1[i];
for (int i = 0; i < n; i++)
cin >> v2[i];
sort(v1,v1+n);
sort(v2,v2+n);
long long p = 0;
for (int i = 0; i < n; i++)
p += v1[i]*v2[n-i-1];
cout << "Case #" << t << ": " << p << endl;
}
return 0;
}
After hours of troubleshooting, I found that the problem was in the way input was taken, as I had used only one loop for both v1 & v2:
for(i=0; i < n; i++)
{
fscanf(fin, "%d",&v1[i]);
fscanf(fin, "%d", &v2[i]);
}
And it should have been done this way:
for(i=0; i<n; i++)
fscanf(fin, "%d",&v1[i]);
for(i=0; i<n; i++)
fscanf(fin, "%d", &v2[i]);
Thank you lukas1994 and pkacprzak
This worked for me:
#include <algorithm>
#include <iostream>
#include <vector>
#include <numeric>
using std::vector;
long long min_dot_product(size_t n, vector<int> a, vector<int> b) {
long long result = 0;
if (n != 0)
{
std::sort(a.begin(), a.end());
std::sort(b.begin(), b.end());
std::reverse(a.begin(), a.end());
/*for (size_t i = 0; i < n; i++) {
result += a[i] * b[n - 1 - i];
}*/
result = std::inner_product(a.begin(), a.end(), b.begin(), 0);
}
return result;
}
int main() {
size_t n;
std::cin >> n;
vector<int> a(n), b(n);
for (size_t i = 0; i < n; i++) {
std::cin >> a[i];
}
for (size_t i = 0; i < n; i++) {
std::cin >> b[i];
}
std::cout << min_dot_product(n, a, b) << std::endl;
}
There is a specific case to be considered.
When one vector is all negative and the other all positive, the algorithm does not produce the minimum result.
if x = [-1,-2] and x = [1,2]
the minimum according to the algorithm output is :
(-1*2) + (-2*1) = -4
However, if you instead use x1y1 + x2y2... xnyn for this case you get:
(-1 * 1) + ( -2*2) = -1-4 = -5