The question is:
There is a strip of tickets with numbers of 8 digits. The first ticket has number M , the last - N . Magnitude M and N meet the following relationship: 10000000 ≤ M < N ≤ 99999999. You are required to determine the number of "lucky" ticket between the given numbers. A ticket is considered "lucky" if the sum of the first four digits equals the sum of the last four digits.
And here is my code:
#include <iostream>
#include <fstream>
#include <iomanip>
#include <stdlib.h>
#include <stdio.h>
using namespace std;
int calcSumDigits(int n)
{
int sum=0;
while (n!=0)
{
sum+=n%10;
n/=10;
}
return sum;
}
int main(void)
{
int a,b,cnt=0,x,y;
cin>>a>>b;
for (int i=a;i<=b;i++)
{
x=i%10000;
y=(i-x)/10000;
if (calcSumDigits(x)==calcSumDigits(y)) cnt++;
}
cout<<cnt;
return 0;
}
The results are right but it takes a little bit long time from the program to give the result. For ex when i try from 10000000 to 99999999 the result shows 4379055 but it takes more than 6 seconds
You just need to compare the two sets of sums generated by all the permutations of each half of your numbers - to simplify I rounded the numbers off a bit:
#include <iostream>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
using namespace std;
int calcSumDigits(int n)
{
int sum=0;
while (n!=0)
{
sum+=n%10;
n/=10;
}
return sum;
}
int SlowVersion(int a, int b) {
int cnt=0,x,y;
for (int i=a;i<=b;i++)
{
x=i%10000;
y=(i-x)/10000;
if (calcSumDigits(x)==calcSumDigits(y)) cnt++;
}
return cnt;
}
int main()
{
int lower;
int upper;
int original_lower;
int original_upper;
cout<<"enter lower:";
cin>>original_lower;
cout<<"enter upper:";
cin>>original_upper;
lower = original_lower - (original_lower%10000);
upper = original_upper + (9999 - (original_upper%10000));
cout<<"to simplify the calculations the lower was changed to:" << lower << endl;
cout<<"to simplify the calculations the upper was changed to:" << upper << endl;
int cnt=0;
const int b=lower%10000;
const int a=(lower-b)/10000;
const int b_top=upper%10000;
const int a_top=(upper-b_top)/10000;
int a_sums[a_top-a];
int b_sums[b_top-b];
int counter = 0;
for (int i=a;i<=a_top;i++)
{
a_sums[counter] = calcSumDigits(i);
counter++;
}
counter = 0;
for (int x=b;x<=b_top;x++)
{
b_sums[counter] = calcSumDigits(x);
counter++;
}
int countera = 0;
int counterb = 0;
for (int i=a;i<=a_top;i++)
{
counterb = 0;
for (int x=b;x<=b_top;x++)
{
if (a_sums[countera]==b_sums[counterb]) cnt++;
counterb++;
}
countera++;
}
cnt = cnt - SlowVersion(lower,original_lower-1);
cnt = cnt - SlowVersion(original_upper+1,upper);
cout << "The total \"lucky numbers\" are " << cnt << endl;
cout << "a is " << a << endl;
cout << "b is " << b << endl;
cout << "a_top is " << a_top << endl;
cout << "b_top is " << b_top << endl;
system("PAUSE");
return 0;
}
which with your inputs results in 4379055 (the same result you got) and runs extremely quicker.
Related
I have a code that generates 10 random numbers and I need to calculate the sum and product of the non-zero numbers and display which numbers have been multiplied. I already have most of the code but I have no idea how to multiply numbers that are nonzero and then display them. Can someone help me?
#include <iostream>
#include <cstdlib>
#include <ctime>
#include <windows.h>
using namespace std;
int main()
{
cout << "draws 10 numbers:" << endl;
Sleep(1000);
cout << endl;
srand(time(0));
int sum=0;
int product=1;
for(int i=0, value=0; i<10; i++, sum+=value, product*=value)
{
value = rand()%10+0;
Sleep(1000);
cout << value << endl;
}
Sleep(1000);
cout << "the sum is " << sum <<endl;
cout << "the product is " << product <<endl;
return 0;
}
To start, it is easier to read and understand if we move the addition and multiplication out of the for loop header.
int sum = 0;
int value = 1;
for(int i = 0; i < 10; i++)
{
int value = rand() % 10;
sum += value;
product *= value;
std::cout << value << std::::endl;
}
Next, we only want to do the multiplication if the value is not equal 0.
int sum = 0;
int value = 1;
for(int i = 0; i < 10; i++)
{
int value = rand() % 10;
sum += value;
if(value != 0)
{
product *= value;
}
std::cout << value << std::::endl;
}
So the whole program looks like this.
#include <iostream>
#include <cstdlib>
#include <ctime>
int main()
{
std::cout << "draws 10 numbers:" << std::endl;
srand(time(0));
int sum = 0;
int product = 1;
int multiplied = 0;
for(int i = 0; i < 10; i++)
{
int value = rand() % 10;
sum += value;
if(value != 0)
{
product *= value;
multiplied += 1;
}
std::cout << value << " ";
}
std::cout << std::endl;
std::cout << "the sum is " << sum << std::endl;
std::cout << "the product is " << product << std::endl;
std::cout << "numbers multiplied is " << multiplied << std::endl;
return EXIT_SUCCESS;
}
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 6 years ago.
Improve this question
Hi i'm trying to Write a program in C++ to, generate and print 20 random numbers, between 0 to 999, and do the following operations without using inbuilt functions, find and print the: min value, max value, average, median, standard deviation, variance. Do a binary search on the 15th element. Please help me with the code.
So far i've done this much
#include
#include
#include
using namespace std;
void minimum(int[], int);
void maximum (int[], int);
void average(int[], int);
void median(int[], int);
void mean(int[], int);
void sort(int[], int);
int ra()
{
int r = rand() % 1000;
return r;
}
int main ()
{
srand(time(NULL));
ra();
int array[20];
int num=20;
for (unsigned int i = 0; i < num; i++)
{
array[i] = ra();
cout << "Index: " << i << ", random number: " << array[i] << endl;
}
minimum();
new_array[20];
num=20;
for (unsigned int i = 0; i < num; i++)
{
new_array[i] = new_array();
cout << "Index: " << i << ", random number: " << minimum << endl;
}
return 0;
}
void minimum(int new_array[], int num)
{
for (unsigned int i = 0; i < num; i++)
if (new_array[i] minimum)
minimum = new_array[i];
cout << "Maximum value: " << minimum << endl;
}
void maximum (int new_array[], int num)
{
for (unsigned int i = 0; i < num; i++)
if (new_array[i] > maximum)
maximum = new_array[i];
cout << "Maximum value: " << maximum << endl;
return 0;
}
void median(int new_array[], int num)
{
//CALCULATE THE MEDIAN (middle number)
if(num % 2 != 0){// is the # of elements odd?
int temp = ((num+1)/2)-1;
cout << "The median is " << new_array[temp] << endl;
}
else{// then it's even! :)
cout << "The median is "<< new_array[(num/2)-1]<<new_array[num/2]< endl;
}
mean(new_array, num);
}
void sort(int new_array[], int num)
{
//ARRANGE VALUES
for(int x=0; x<num; x++){
for(int y=0; y<num-1; y++){
if(new_array[y]>new_array[y+1]){
int temp = new_array[y+1];
new_array[y+1] = new_array[y];
new_array[y] = temp;
}
}
}
cout << "List: ";
for(int i =0; i<num; i++){
cout << new_array[i] << " ";
}
cout << "\n";
median(new_array, num);
}
void average_(int new_array[], int nums)
{
float sum;
for (unsigned int i = 0; i < 20; ++i)
{
sum+=num;
}
cout << "Average value: " << average_/num << endl;
}
Please tell the necessary corrections
You have a ways to go, your code does not do any of the things you want yet. However, you mentioned that you are a beginner so I fixed your code and set up a basic structure of how to get going. I left comments on what I changed and what you need to do. That being said, I don't know what you mean by "Do a binary search on the 15th element"
#include <iostream>
#include <cstdlib>
#include <time.h>
using namespace std;
int ra()
{
// You wanted a number between 0 and 999 inclusive so do not add 1
// Instead do a modulus of 1000
int r = rand() % 1000;
return r;
}
int main ()
{
// Do this to get different random numbers each time you run your program
srand(time(NULL));
// You have to call ra as a function. Do this by writing: ra()
// Here I am storing 20 random numbers in an array
int nums[20];
for (unsigned int i = 0; i < 20; ++i)
{
nums[i] = ra();
cout << "Index: " << i << ", random number: " << nums[i] << endl;
}
// Iterate to find the minimum number
int minimum = nums[0];
for (unsigned int i = 1; i < 20; ++i)
if (nums[i] < minimum)
minimum = nums[i];
cout << "Minimum value: " << minimum << endl;
// TODO: Find the maximum in basically the same way
// TODO: Find the average by summing all numbers then dividing by 20
// TODO: Find the median by sorting nums and taking the average of the two center elements
// TODO: etc.
return 0;
}
#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;
int r;
int ra;
int i=0;
int ra(){
r = (rand() % 999) + 1;
return r;
}
int main ()
{
int random_;
srand((int)time(0));
while (i++ < 20)
{
random_ = r;
cout<< random_<<endl;
}
return 0;
}
This is supposed to be a code for user entering integers in an array and then calling a function that shows if they are even or odd. It then counts how many are even or odd (which I didn't get to yet because I am stuck here and I keep getting this error).
invalid operands of types 'int [20]' and 'int' to binary 'operator%'
#include <iostream>
#include <iomanip>
#include <math.h>
using namespace std;
void getValue(int[], int);//function prototype
int main()
{
const int ARRAY_SIZE = 20;
int numbers[ARRAY_SIZE];
cout<<fixed<<showpoint<<setprecision(1);
//get ints from user
getValue(numbers, ARRAY_SIZE);
if ( numbers % 2== 0 )
// if the integer when divided by 2 has no remainder then it's even.
cout << numbers << " is even "<<endl;
else
// the integer is odd
cout << numbers << " is odd "<<endl;
return 0;
}
void getValue(int numbers[], int ARRAY_SIZE)
{
//loop counter
int index;
//get each value
for(index = 0; index <= ARRAY_SIZE - 1; index++)
{
cout<< "Enter an integer " << (index + 1)<< " : ";
cin>> numbers[index];
}
}
#include <iostream>
#include <iomanip>
#include <math.h>
using namespace std;
void getValue(int [], int);//function prototype
int main()
{
const int ARRAY_SIZE = 20;
int numbers[ARRAY_SIZE];
cout << fixed << showpoint << setprecision(1);
//get ints from user
getValue(numbers, ARRAY_SIZE);
//I added this FOR loop.
for (int i = 0; i < ARRAY_SIZE - 1; i++)
{
//I also added the [i] so that the number is outputted as well
if (numbers[i] % 2 == 0)
// if the integer when divided by 2 has no remainder then it's even.
// and here as well
cout << numbers[i] << " is even "
<< endl;
else
// the integer is odd
cout << numbers[i] << " is odd "
<< endl;
}
return 0;
}
void getValue(int numbers[], int ARRAY_SIZE)
{
//loop counter
int index;
//get each value
for (index = 0; index <= ARRAY_SIZE - 1; index++)
{
cout << "Enter an integer " << (index + 1) << " : ";
cin >> numbers[index];
}
}
I added a for loop and the array brackets on the output.
You need to loop through your array, just add a loop and check if (numbers[index] % 2 == 0)
Above answers should give you right results.
You can use a range for loop for fixed-size arrays:
for (int value : numbers)
{
if (value % 2 == 0)
{
cout << value << " is even.\n";
}
else
{
cout << value << " is odd.\n";
}
}
First, this is illegal in C++:
const int ARRAY_SIZE = 20;
int numbers[ARRAY_SIZE];
You need to declare
int numbers[20];
instead.
Second,
if ( numbers % 2== 0 )
numbers is an array of 20 values. What exactly is it meant to be using the % operator on? You probably want a loop that will check each int in the array.
for (int i = 0; i < 20; ++i)
{
if ( numbers[i] % 2== 0 )
...
else
...
}
Looking for some advice here on what I'm getting wrong. Everything in my main should be fine and left unchanged. My problem is in my reverse function. It's printing the reversed number right before the cout statement of "The number is" instead down below where it should be. I spent awhile trying to fix but can't come up with a solution.
#include <iostream>
#include <iomanip>
#include <cstdlib>
#include <ctime>
using namespace std;
const int NUM_VALS = 10; //the maximum number of values to use
int reverse(int num);
bool isPrime(int num);
int main()
{
int number, //Holds the random number that is manipulated and tested
loopCnt; //Controls the loop
//set the seed value for the random number generator
//Note: a value of 1 will generate the same sequence of "random" numbers every
// time the program is executed
srand(1);
//Generate 10 random numbers to be manipulated and tested
for( loopCnt = 1; loopCnt <= NUM_VALS; loopCnt++ )
{
//Get a random number
number = rand();
//Display the sum of adding up the digits in the random number, the reversed
//random number, and whether or not the number is palindromic or a prime number
cout << "The number is " << number << endl
<< "----------------------------------------" << endl
// << "Adding the digits result" << setw(16) << sumDigits( number ) << endl
<< "Reversing the digits result" << setw(13) << reverse(number) << endl
// << "Is the number a palindrome?" << setw(13) << (isPalindrome(number)? "Yes" : "No") << endl
// << "Is the number prime?" << setw(20) << (isPrime(number)? "Yes" : "No") << endl
<< endl << endl;
}
return 0;
}
int reverse(int num)
{
int quo, rem;
quo = num;
while (quo != 0)
{
rem = quo % 10;
cout << rem;
quo /= 10;
}
}
bool isPrime(int num)
{
int i;
if (num % 2 == 0)
return false;
for (i = 3; i*i <= num; i+=2)
{
if (num % i == 0)
return false;
}
return true;
}
You need to have your reverse function return the number as reversed, because the return value is used in main.
You can build the reversed number by multiplying a "reversed" value by 10, then adding in the remainder:
int reverse(int num)
{
int reversed = 0;
int quo, rem;
quo = num;
while (quo != 0)
{
rem = quo % 10;
reversed = reversed * 10 + rem;
quo /= 10;
}
return reversed;
}
You can also use this method to reverse a number by taking string input and then reverse it and convert it to int.
#include <iostream>
#include<string>
using namespace std;
int reverse_num(string a)
{
string s;
for(int i=a.length()-1;i>=0;i--)
{
s+=a[i];
}
int n;
n=stoi(s);
return n;
}
int main()
{
string a;
cin>> a;
cout<<reverse_num(a);
return 0;
}
I'm making a program that prints all digits from an array (entered as an integer) and it works, but the digits are printed backwards and I don't know how to reverse them. Can someone help?
#include <iostream>
#include <iomanip>
#include <cmath>
using namespace std;
void explode(int number,int array[])
{
while (number > 0) {
int digit = number % 10;
cout << digit << '\n';
number /= 10;
}
}
int main()
{
int digits[100];
int numdigits;
int n;
cout << "Enter number: ";
cin >> n;
// numdigits = explode(n,digits);
cout << "[";
while (n > 0) {
int digit = n % 10;
n /= 10;
digits[digit] = digit;
cout << digits[digit];
}
cout << "]" << endl;
}
You just have to reverse the array using reverse() from <algorithm>.
Code:
#include <iostream>
#include <iomanip>
#include <algorithm>
#include <cmath>
using namespace std;
int array_c = 0;
void explode(int number,int array[])
{
while (number > 0) {
int digit = number % 10;
number /= 10;
array[array_c++] = digit;
}
}
int main()
{
int digits[100];
int numdigits;
int n;
cout << "Enter number: ";
cin >> n;
explode(n,digits);
reverse(digits,digits+array_c);
cout << "[";
for(int i = 0; i < array_c; ++i)
cout<<digits[i];
cout << "]" << endl;
}
Your use of
digits[digit] = digit;
is not right. You probably meant to use
digits[numdigits] = digit;
You can fix your problem by dividing the work into two steps. In the first step, you store the digits. In the second step, you print the digits.
int numdigits = 0;
while (n > 0) {
cout << "n: " << n << endl;
int digit = n % 10;
n /= 10;
digits[numdigits++] = digit;
}
// Make sure to print them in reverse order.
cout << "[";
for ( ; numdigits > 0; )
{
cout << digits[--numdigits];
}
cout << "]" << endl;