Sample Input #2
8
5
12
12
15
23
15
18
12
4
Sample Output #2
helloworld
8:8-16-24-32-40-48-56-64
5:5-10-15-20-25
12:12-24-36-48-60-72-84-96-108-120-132-144
12:12-24-36-48-60-72-84-96-108-120-132-144
15:15-30-45-60-75-90-105-120-135-150-165-180-195-210-225
23:23-46-69-92-115-138-161-184-207-230-253-276-299-322-345-368-391-414-437-460-483-506-529
15:15-30-45-60-75-90-105-120-135-150-165-180-195-210-225
18:18-36-54-72-90-108-126-144-162-180-198-216-234-252-270-288-306-324
12:12-24-36-48-60-72-84-96-108-120-132-144
4:4-8-12-16
#include <iostream>
#include <iomanip>
using namespace std;
char secretCode(char number)
{
if(number >= 1 && number <= 26)
{
return static_cast<char>('a' - 1 + number);
}
else if (number >= 27 && number <= 52)
{
return static_cast<char>('a' - 27 + number);
}
else if (number >= 53 && number <= 104)
{
return static_cast<char>('a' - 53 + number);
}
}
void printSequence(int number[10])
{
for (int i = 0; i < 10; i++)
{
cout << secretCode(number[i]);
}
}
int main()
{
int number[10];
for (int x = 0; x < 10; ++x)
{
cin >> number[x];
printSequence(number);
for (int y = number[x]; y <= number[x]; ++y)
{
for (int z = 1; z <= number[x]; ++z)
{
if (z > 1)
{
cout << "-";
}
if (z < 1)
{
cout << number[x] * -1;
}
else
cout << number[x] * z ;
}
}
}
}
I think I got the answer but I'm doing something wrong in my code, I'm pretty new to programming still and have been doing fine so far till I encounter the loop and functions..
#include<iostream>
#include<iomanip>
#include <iterator>
using namespace std;
char secretCode(char number)
{
if(number >= 1 && number <= 26)
{
return static_cast<char>('a' - 1 + number);
}
else if (number >= 27 && number <= 52)
{
return static_cast<char>('a' - 27 + number);
}
else if (number >= 53 && number <= 104)
{
return static_cast<char>('a' - 53 + number);
}
}
void printSequence(int number[10])
{
for (int i=0; i<10; i++)
{
cout << secretCode(number[i]);
}
cout << endl;
int len = 0;
while(len < 10){
cout << number[len] << ":";
for(int z=1; z<=number[len]; ++z){
if (z>1)
{
cout << "-";
}
if (z<1)
{
cout <<number[len];
}
else
cout << number[len]*z ;
}
cout << endl;
len += 1;
}
}
int main()
{
int number[10];
for(int x=0; x<10; ++x)
{
cin >> number[x];
}
printSequence(number);
}
Related
the code is for reversing the input decimal whether negative or positive .if the value of decimal exceeds as max size of int in 32 bit system then return 0.why my code is not working...help me finding the bug .i have attached the code.
#include<iostream>
#include<stdlib.h>
#include<math.h>
using namespace std;
float reverseint(int x)
{
int digit[10];
int i = 0;
float revnum = 0;
//store digits of number x
while (x != 0)
{
digit[i] = x % 10;
x = x / 10;
i++;
}
i--;
//reversing number x
while (i >= 0)
{
int j = 0;
revnum = (digit[i] * pow(10, j)) + revnum;
j++;
i--;
}
return revnum;
}
int main()
{
int n;
cin >> n;
if (n < 0)
{
if (n < -pow(2, 31))
cout << 0 << endl;
cout << -reverseint(-n);
}
if (n > 0)
{
if (n > (pow(2, 31) - 1))
cout << 0 << endl;
cout << (reverseint(n)) << endl;
}
return 0;
}
You are initializing j variable on every while loop on reverseint function.
float reverseint(int x)
{
int digit[10];
int i = 0;
float revnum = 0;
//store digits of number x
while (x != 0)
{
digit[i] = x % 10;
x = x / 10;
i++;
}
i--;
//reversing number x
int j = 0; /// Correct place.
while (i >= 0)
{
//int j = 0; /// Wrong. j will be always 0 on every loop. revnum will be sum of digits on variable x.
revnum = (digit[i] * pow(10, j)) + revnum;
j++;
i--;
}
return revnum;
}
I'm trying to make a C++ program to find numbers from 1 to n (and their count) such that they add up to s, I've hit time limits so I was trying to optimise it by calculating sum only once and then mutating it. In the for loop in the removeStuff function, i becomes a random number even though length isn't.
#include <iostream>
using namespace std;
#define ll long long
//ll sumArray(ll arr[], ll length) {
// ll sum = 0;
// for (ll i = 0; i < length; i++) {
// sum += arr[i];
// }
// return sum;
//}
void printArray(ll arr[], ll length) {
string w;
ll count = 0;
for (ll i = 0; i < length; i++) {
if (arr[i] != 0) {
count += 1;
w += to_string(arr[i]) + " ";
}
}
cout <<count<<" " << w<< endl;
}
void removeStuff(long long arr[], long long length, long long target, ll sum) {
if (sum == target) {
printArray(arr, length);
return;
}
for (long long i = length - 1; i >= 0; i--) {
if (sum - arr[i] > target && arr[i] != 0) {
sum = sum - arr[i];
arr[i] = 0;
if (sum == target) {
printArray(arr, length);
return;
}
continue;
}
}
}
ll sum(ll n) {
return (n * (n + 1)) / 2;
}
int main() {
ios::sync_with_stdio(false);
int testCases;
cin >> testCases;
while (testCases--) {
ll n, s;
cin >> n >> s;
if (sum(n) < s) {
cout << -1 << endl;
continue;
} else if (sum(n) == s) {
string w;
for (ll i = 1; i <= n; i++) {
w += " " + to_string(i);
}
cout << n << " " << w << endl;
continue;
} else {
if (n >= s) {
cout << "1 " << s << endl;
continue;
} else if (s % n < n && n+n > s) {
cout << "2 " << n << " " << s - n << endl;
continue;
} else {
ll arr[n];
ll sum = 0;
for (ll i = 0; i < n; i++) {
arr[i] = i + 1;
sum = sum + i + 1;
}
removeStuff(arr, n, s, sum);
}
}
}
}
The problem occurs when I use the input 1 4 9.
For reference, here is a screenshot of my debugger results
I've come across this problem where I have to transform a number from the decimal system to the binary and compare if the numbers are equal
Example:
7 is becoming 111
The output is false
and where 5 is 101
The output is true
I've figured out how to transform the numbers with an array
But have no idea how to compare them
#include<iostream>
using namespace std;
int main()
{
int num;
cin >> num;
int arr[8] = {};
if ((num >= 0) && (num <= 255))
{
while (num != 0)
{
for (int i = 0; i < 8; i++)
{
if (num % 2 == 0)
arr[i] = 0;
else
{
arr[i] = 1;
}
num = num / 2;
}
}
for (int i = 7; i >= 0; i--)
cout << arr[i];
cout << endl;
}
else
{
cout << "error" << endl;
return 1;
}
system("pause");
return 0;
}
this is the problem
enter link description here
the problem in function prime but it think it is true , and i can not find solution
i submit it in codeforces but it give me .Wrong answer on test 5
:-
the input :
39
81 46 4 5 2 71 66 97 51 84 50 64 68 99 58 45 64 86 14 44 7 49 45 72 94 19 33 68 83 12 89 88 39 36 51 11 57 9 54
the wrong !!
#include <iostream>
#include <math.h>
using namespace std;
int maximum(int arr[], int n)
{
int max = INT_MIN;
for (int i = 0; i < n; i++)
{
if (max < arr[i]) { max = arr[i]; }
}
return max;
}
int minimum(int arr[], int n)
{
int min = INT_MAX;
for (int i = 0; i < n; i++)
{
if (min > arr[i]) { min = arr[i]; }
}
return min;
}
int prime(int arr[], int n)
{
int con = 0;
bool flag = true;
for (int i = 0; i < n; i++)
{
if (arr[i] == 2)
{
con++;
}
else if (arr[i] > 2)
{
for (int j = 2; j < n; j++)
{
if (arr[i] % j == 0)
{
flag = false;
break;
}
else
{
flag = true;
}
}
if (flag == true)
con++;
}
}
return con;
}
int palindrome(int arr[], int n)
{
int i = 0, con = 0;
while (n--)
{
int temp;
temp = arr[i];
int reverseNumber = 0, rightDigit;
while (temp != 0)
{
rightDigit = temp % 10;
reverseNumber = (reverseNumber * 10) + rightDigit;
temp = temp / 10;
}
if (reverseNumber == arr[i]) {
con++;
}
i++;
}
return con;
}
int divisors(int arr[], int n)
{
int max = 0;
int con = 0;
int x = arr[0];
for (int i = 0; i < n; i++)
{
int temp = arr[i];
for (int j = 1; j <= arr[i]; j++)
{
if (arr[i] % j == 0)
{
con++;
}
}
if (max < con)
{
max = con;
x = arr[i];
}
else if (max == con)
{
if (x < arr[i])
{
x = arr[i];
}
}
con = 0;
}
return x;
}
int main()
{
int n; cin >> n;
int arr[1001];
for (int i = 0; i < n; i++)
cin >> arr[i];
cout << "The maximum number : " << maximum(arr, n) << endl;
cout << "The minimum number : " << minimum(arr, n) << endl;
cout << "The number of prime numbers : " << prime(arr, n) << endl;
cout << "The number of palindrome numbers : " << palindrome(arr, n) << endl;
cout << "The number that has the maximum number of divisors : " << divisors(arr, n) << endl;
divisors(arr, n);
return 0;
}
This for loop
for (int j = 2; j < n; j++)
is incorrect. It seems you mean
for (int j = 2; j < arr[i]; j++)
Also you should declare the variable flag within the else statement where it is used. For example
for (int i = 0; i < n; i++)
{
if (arr[i] == 2)
{
con++;
}
else if (arr[i] > 2)
{
bool flag = false;
//...
So far I have gotten most of my functions working. I am now stuck on my final step. I need to take in an array from my main, to my "histo" function. I need to take repeating values within certain ranges, and put them each into the same array element and count them. Once this is done, I am to print out a histogram with asterisks. the ranges are------bin 0. if score < 10.----bin 1. if score >= 10 but < 20......ect. Then the output; 9| ** 8|* 7| *** and so on until 0. The asterisks need to resemble the amount of numbers that fit within the specified range and stored in an array element. I am stuck on this part and we can't use vectors, or any #includes other than , , and using namespace std; Any advice is great. I have my cout in the main to make sure my math was correct.
#include <iostream>
#include <iomanip>
#include <cmath>
using namespace std;
/*int histo(int x);*/
double dev(int count, int* scores, double mn);
double mean(int count, int* stats);
int main()
{
int scores[101];
int count = 0;
int bin[10];
double mn;
cout << "Enter a score (-1 to stop): ";
do
{
cin >> scores[count++];
} while (scores[count - 1] != -1);
count--;
mn = mean(count, scores);
cout << "mean: " << mean(count, scores) << endl;
cout << "dev: " << dev(count, scores, mn) << endl;
system("pause");
return 0;
}
int histo(int* scores)
{
int bins[10]{};
int counter = 0;
for (int i = 0; i < *scores; i++)
{
if (*scores < 10)
{
bins[counter++];
}
else if (*scores >= 10 && *scores < 20)
{
bins[counter++];
}
else if (*scores >= 20 && *scores < 30)
{
bins[counter++];
}
else if (*scores >= 30 && *scores < 40)
{
bins[counter++];
}
else if (*scores >= 40 && *scores < 50)
{
bins[counter++];
}
else if (*scores >= 50 && *scores < 60)
{
bins[counter++];
}
else if (*scores >= 60 && *scores < 70)
{
bins[counter++];
}
else if (*scores >= 80 && *scores < 90)
{
bins[counter++];
}
else if (*scores >= 90)
{
bins[counter++];
}
for (int j = 0; j < )
}
}
double dev(int count, int* scores, double mn)
{
double x = 0;
double y = 0;
double d = 0;
for (int i = 0; i < count; i++)
{
x = pow(scores[i] - mn, 2);
y += x;
}
d = sqrt(y / count);
return d;
}
double mean(int count, int* scores)
{
double total = 0;
for (int i = 0; i < count; i++)
{
total += scores[i];
}
return total / count;
}
I know I went over kill with the if statements. This is just where I am unsure of what to do.
for (int i = 0; i < *scores; i++)
Ok, so scores is a pointer to an array. By de-referencing it you get the value of the first element of that array, but not the count/size of it.
if (*scores < 10)
{
bins[counter++];
}
else if (*scores >= 10 && *scores < 20)
{
bins[counter++];
}
bins[counter++] will go out of bounds very soon with this setup, since your scores array is > 10 (not taking the undefined behaviour into account). Also the else statement already implies that *scores >= the previous statement.
So the solution to this will be something like this, assuming you want to increase the bin on index n every time:
int histo(int *scores, int scoreCount)
{
int bins[9]{};
for (int i = 0; i < scoreCount; i++)
{
if (scores[i] < 10)
{
bins[0]++;
}
else if (scores[i] < 20)
{
bins[1]++;
}
else if (scores[i] < 30)
{
bins[2]++;
}
else if (scores[i] < 40)
{
bins[3]++;
}
else if (scores[i] < 50)
{
bins[4]++;
}
else if (scores[i] < 60)
{
bins[5]++;
}
else if (scores[i] < 70)
{
bins[6]++;
}
else if (scores[i] < 90)
{
bins[7]++;
}
else
{
bins[8]++;
}
}
// Do stuff with your bins
}
I believe you're making the histo function a lot more complicated than it needs to be.
void histo(int* scores)
{
int bins[10] = {0};
for (int i = 0; scores[i] != -1 ; i++)
{
int index = scores[i]/10;
bins[index]++;
}
for(int i = 0; i < 10; i++){
cout << i + 1;
for(int j = 0; j < bins[i]; j++){
cout << "*";
}
cout << endl;
}
}