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 5 years ago.
Improve this question
Can someone tell me why this programm gives only the cout text?
#include "iostream"
#include "stdafx.h"
#include <iostream>
using namespace std;
int main()
{
float x;
int y[50];
int n;
cout << "Dati notele despartindule prin Enter";
for ( n = 0; n == 5; n++) {
cin >> y[n];
if (n >= 1) {
y[n] = y[n - 1] + y[n];
}
}
x = y[n] / (n + 1);
cout << x;
return 0;
}
for ( n = 0; n == 5; n++)
Is not what you want. A for-loop is nothing but a while-loop using different syntax:
n = 0;
while(n == 5)
{
// you loop body here
n++;
}
As you can see, it executes while your condition is true. In this case... not at all because it's not true to begin with.
You probably meant
for ( n = 0; n < 5; n++)
n == 5 is the problem. When came to for function and makes the test to see if the block needs to run it get false because you initialize with n = 0; better than n == 5 use n!=5 but if you skip to make n = 5 you get an infinite loop.
For the best case use n < 5.
Related
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
#include <iostream>
#include <cmath>
using namespace std ;
int main ()
{
int n, sum=0 ;
cout<< "Input number of terms: " ;
cin>> n ;
for (int i=1; i<=n; i++)
{
int k = pow(10, i) - 1 ;
cout << k ;
if(i < n)
{
cout<< " + " ;
}
sum += k ;
}
cout << "\nThe sum of the series = " << sum ;
{int m;cin>>m;}
return 0 ;
}
every time I run this code it gives me weird output like
9 + 98 + 999 + 9998 + ...
it subtracts some Ks from 2 !!
The rule is mathematically right and there is no syntax errors.
Is it the way of declaring k inside the loop or it's an compiler error ?
So, what's wrong here?
Here is the for loop without using the pow function:
int power = 10;
for (int i = 1; i < n-1; ++i)
{
const int k = power - 1 ;
cout << k;
if(i < n)
{
cout<< " + " ;
}
sum += k;
power *= 10; // This is important.
}
This should be more accurate than using pow because there is no conversions between integer and floating point.
Also, you may want to try using the series from 0 .. (n-1).
I think the best thing to do is to create your own power function because pow() is not working that way before when I used it. Kinda like this one
int pow_num(int base_num, int exp_num)
{
int result = 1;
for(int i = 0; exp_num > i; ++i)
{
result = result * base_num;
}
return (result);
}
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 4 years ago.
Improve this question
This is a problem on codeforces
and I have submitted a solution and I got TLE.
How to remove TLE
#include<bits/stdc++.h>
using namespace std;
int main()
{
ios::sync_with_stdio(false);
unsigned int y, k, n;
set <int> s;
cin >> y >> k >> n;
if (y >= n)
{
cout << -1; return 0;
}
for (int i = y + 1; i <= n; i++)
{
if (i%k == 0)
{
s.insert(i - y);
}
}
if (s.begin() == s.end())
{
cout << -1; return 0;
}
for (auto x : a)
cout << x << " ";
}
The problem seems at the algorithmic level. Instead to generate all candidate i values and then test if they are divisible by k, you can directly generate these values in a loop with an increment equal to k.
The minimum value iminis equal to k*((y+1)/k) or k*((y+1)/k) + k, depending if y+1 is divisible by kor not.
You have two gains: you consider k less candidates, and you avoid the costly % operation.
Moreover, when you find a value, you can directly print it, no need to memorize it.
Edit: here is the code
#include <iostream>
int main()
{
std::ios::sync_with_stdio(false);
unsigned int y, k, n;
std::cin >> y >> k >> n;
unsigned int imin = k*((y+1)/k);
if (imin < y+1) imin += k;
if (imin > n) {
std::cout << -1;
return 0;
}
for (unsigned int i = imin; i <= n; i+=k)
{
std::cout << i-y << " ";
}
return 0;
}
Edit 2: the last i-y calculation can be avoided by changing the bounds of the loop
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 8 years ago.
Improve this question
I am trying to come up with the following algorithm:
The input is unsigned integer number.
The output is the size of the array of unordered pairs of unsigned integers, which, when multiplied, give a number less then or equal to the input.
I have one naive implementation working, but it is way too slow for my purpose (compl. O(n^2), please correct me if I am wrong). My question is: how to make it faster?
#include <iostream>
using namespace std;
bool notInYet(int t[][1], int mi, int ma, int m) {
bool val = true;
for(int i = 0; i < m; i++)
if(t[i][0] == mi && t[i][1] == ma)
val = false;
return val;
}
int main() {
int n, m;
int t[100000][1];
cin >> n;
m = 0;
for(int i = 1; i <= n; i++) {
for(int j = 1; j*i <= n && j <= i; j++) {
if(notInYet(t, j, i, m)) {
t[m][0] = j;
t[m][1] = i;
//cout << "t[" << m << "] = (" << t[m][0] << ", " << t[m][1] << ")" << endl;
m++;
}
}
}
cout << m << endl;
return 0;
}
I think it should be something like that - pseudocode:
int counter = 0;
for int i = 1 to sqrt(input), i++ {
if (input % i == 0) counter++;
}
counter is an answer if you need unique pairs, otherwise you need to multiply it by 2 (and sub 1 if input % sqrt(input) == 0)
If I'm reading correctly #jauser's algorithm doesn't get what you want.
If the target is 5, then the pairs are (1,1)(1,2)(1,3)(1,4)(1,5)(2,2). So the answer is 6. His algorithm will produce 1 because 5 mod 1 == 0, but not mod 2.
In general, if the target is n, then you know (1,k) is a counted pair for all k from 1 to n. There are n - 1 + 1 = n of these. Now you have (2,k) for k from 2 to floor(n/2) (skip 1 because your pairs are unordered). There are n/2-2+1 of these. Continue this through (j,k) for j= floor(sqrt(n)). Putting this is pseudocode
count = 0;
for j in 1 .. floor(sqrt(n))
count += floor(n / j) - j + 1;
Maybe there is even some clever series solution that gets this to a constant time calculation.
Am I missing something in the problem?
Well, you are spending a lot of time effectively calculating the following per i:
j= n/i;
So if you just do that you reduce the complexity to O(n). You can halve it also since the list will contain both (i, j) and (j, i) when i!=j, but that won't reduce the overall complexity.
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 9 years ago.
Improve this question
I have to make this C++ code in Dart, but I find it really difficult. I tryed watching Darts video and searching on the web, but with no success.Could someone be able to give me a hand?
This is the code:
#include <iostream>
using namespace std;
int main()
{
int n;
cin >> n;
if (n < 0) return 1;
int sum = 0;
int i = 0;
while (i <= n) sum += i*i;
cout << sum;
return 0;
}
something like
library x;
import 'dart:io';
void main(List<String> args) {
int n;
print('input a number');
String input = stdin.readLineSync();
n = int.parse(input);
print('n: $n');
if(n < 0) {
exit(1);
}
int sum = 0;
int i = 0;
while(i <= n) {
print(sum);
sum += i * i;
}
print(sum);
}
But don't expect to much.
When reaching the while loop sum and i are 0.
This way you have produced a nice endless loop to busy your computer ;-)
You could do the calculation bit (sum of squares of all numbers from 1 to n inclusive) with a recursive function like:
int recur(int n) => (n > 0) ? (n * n) + recur(n - 1) : 0;
Then it's a simple matter of figuring out how to enter n and output recur(n). That can be done with stdin.readLineSync and print. That would be along the following lines:
int recur(int n) => (n > 0) ? (n * n) + recur(n - 1) : 0;
void main( List<String> args ) {
int inNum;
String input = stdin.readLineSync();
inNum = int.parse( input );
if (inNum < 0) {
exit( 1 );
}
print( recur( sum ) );
}
Just be careful with large input values, I'm not sure whether Dart is smart enough to do tail end recursion optimisation. If not, stack space may be an issue.
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
Problem : http://www.spoj.com/problems/EGYPIZZA/
I've been trying to solve this 'PIZZA' problem for quite some time and I have tried many, many
inputs and it seems to be working fine on my machine but the online judge keeps refusing to accept mu code saying its the wrong answer!!
Please help me out...
Here's my code:
#include<iostream>
#include<cstring>
using namespace std;
int main()
{
char str[4],b[3][4]= {"1/4","1/2","3/4"};
unsigned int n, s = 1, count1 = 0, count2 = 0, count3 = 0;
scanf("%u",&n);
while(n--)
{
cin>>str;
if(strcmp(str,b[0])==0)
count2++;
else if(strcmp(str,b[1])==0)
count1++;
else if(strcmp(str,b[2])==0)
count3++;
}
while(count3!=0 && count2!=0)
{
count2--; count3--; s++;
}
if(count1%2!=0)
if(count2/2!=0)
{
count2-=2; count1--; s++;
}
s = s + (count1/2) + (count1%2) + (count2/4) + (count2%4) + count3 ;
printf("%u\n",s);
return 0;
}
EDIT :
I have updated my code after your suggestions please check it out guys!!
Still giving wrong answer..
Accepted Solution:
#include<iostream>
using namespace std;
int main()
{
string s;
int n, sum = 1, count1 = 0, count2 = 0, count3 = 0, extra;
cin >> n;
for (int i=0; i < n; i++)
{
cin >> s;
if (s == "1/2")count1 ++;
if (s == "1/4")count2 ++;
if (s == "3/4")count3 ++;
}
sum += count3 + count1/2.0 + 0.5;
extra = count3 + (count1%2)*2;
if (count2 >= extra)
{
count2 -= extra;
sum += count2 / 4.0 + 0.75;
}
cout << sum << endl;
return 0;
}
This problem would be more interesting if aboTrika don't insist on having his pizza one piece as the others. :)
Your program has numerous problems, such as using vectors where simple counters will suffice, and using floating point where integer arithmetic is appropriate. Perhaps the most serious problem is the statement s = s + q/4 + h/2 which in effect satisfies most requests for 1/4 pizza by grouping quarter-pizzas together and grouping half-pizzas together. Instead, requests for 1/4 pizza should be used first to complement as many 3/4-pizza requests as possible, then to fill up a 1/2-pizza request if the requested number of halves is odd, and only then used together.