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.
Related
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 1 year ago.
Improve this question
Program adds 2 fractions and displays their sum in a reduced form (n times). Could someone help me optimize my solution (According to SPOJ, the time limit has been exceeded)
My solution:
#include <iostream>
using namespace std;
int main()
{
int n, a, b, c, d, gcd;
long long num, den;
cin >> n;
for(int i = 0; i < n; i++)
{
cin >> a >> b >> c >> d;
num = (a*d)+(b*c);
den = b*d;
for(int j = 1; j <= num && j <= den; ++j)
{
if(num % j == 0 && den % j == 0)
{
gcd = j;
}
}
cout << num/gcd << " " << den/gcd << endl;
}
}
The key step in your code is computing the greatest common divisor of num and den. The code does this by testing every number less than or equal to either of them to find the largest that divides both. While this is a correct algorithm, it is very slow, requiring time proportional to whichever is smaller.
To make this faster, use the Euclidean algorithm, which takes time proportional to the logarithm of the smaller number, which means it is much faster. The essence of this algorithm is to divide one number by the other and replace the larger with the resulting remainder until that remainder is zero. Concretely, copying and slightly reformatting the code from tutorialspoint.com:
int gcd(int a, int b)
{
if (b == 0)
return a;
return gcd(b, a % b);
}
Alternatively, as noted in a comment by #badfilms, C++17 contains std::gcd in the library.
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 3 years ago.
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.
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.
Improve this question
Error:
//Count(variable) not declared! error.
But I have declared it. This is a program to calculate the number of Armstrong digits in the interval given by the user. It will continue to run until there is at least one Armstrong number in the interval. I have used a Do-While loop for this purpose.
C++:
#include<iostream>
#include<cmath>
using namespace std;
//Function to check if the number is an Armstrong number
bool Armstrong(int n) {
int number, original, remainder, result = 0, k = 0;
original = n;
//Calculating the number of digits
while (original != 0) {
original /= 10;
++k;
}
original = n;
while (original != 0) {
remainder = original % 10;
result += pow(remainder, k);
original /= 10;
}
if (result == n)
return true;
else
return false;
}
//Checking Armstrong in the interval
int main() {
do {
int start, stop, n, i = 1;
std::cout << "Enter Starting Point: ";
std::cin >> start;
std::cout << "Enter Stop Point: ";
std::cin >> stop;
n = start;
int count = 0; //printing the numbers in the interval
for (; start <= stop; start++) {
if (Armstrong(start)) {
std::cout << "Armstrong Number " << i << " : " << start;
count++;
i++;
}
n--;
}
//It is showing the error here. "Count not Declared"
}
while (count == 0);
}
The problem is that you declare int count; inside the do-while loop, so you can't check for it in the loop condition. Move it to outside of the loop:
int count = 0;
do {
int start, stop, n, i = 1;
...
} while (count == 0);
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
I'm working on a hw assignment and I keep getting a crash but I don't get any errors It just crashes instead. I created 2 functions to try to calculate the max,min and avg and 1 to generate 3 random numbers and send it back to main. All my values from my functions need to be returned as pointers. Thanks for the help in advanced!
int * random_number()
{
static int num[3];
for(int i = 0; i < 3; i++){
num[i] = (10 + rand() % 90);
}
return num;
}
int * calculate(int *value)
{
static int ans[3];
int max = 0;
for (int i = 0; i < 3; i++)
{
ans[0] += value[i];
ans[0] = ans[0] / i;
if (value[i] > max)
{
ans[2] = max;
}
int min = value[0];
if (value[i] < min)
{
ans[3] = min;
}
}
return ans;
}
int main() {
srand(time(NULL));
int cnt = 0;
int *result;
int *find;
do{
result = random_number();
find = calculate(result);
cnt++;
}
while(cnt < 10);
if (cnt == 10){
printf("Result is %i, %i, %i.", result[0],result[1],result[2]);
printf("The avg is %i. The max is %i and the min is %i.", find[0], find[1], find[2]);
printf("Worked \n");
}
else
printf("Didn't work");
return 0;
}
Your calculate() function has a number of bugs -- mainly that you are trying to do some things within your for loop that should be done outside it. The crash you're getting is from the line:
ans[0] = ans[0] / i;
...when i is zero on the first loop pass (so you're dividing by zero). That division should actually be done after the loop, after all the values have been added (and will always be a divide by 3).
The line:
int min = value[0];
should be moved to before the loop as well; as it is, you're resetting the value of min on every loop pass, so you're actually ending up with the last value that's less than value[0], if there is one... and you don't change ans[3] at all when there isn't. ans[3] should actually be ans[2] anyway... ans[3] is out of bounds (would be the 4th element).
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.
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.