When I input 6 I only get 2.
When I input 91 I only get only 7.
However:
When I input 18 I get 2 3 3.
When I input 2121 (which is 3*7*101) I get 3 7.
I can't seem to find what is wrong. Does anybody have any suggestions?
#include<iostream>
using namespace std;
bool is_prime( int fac )
{
int i;
bool found;
found=true;
for( i = 2; i < fac; i++ )
{
if ( fac % i == 0 && found)
{
found=false;
break;
}
}
return found;
}
void prime_factors(int x)
{
int i;
for ( i = 2; i < x; i++ )
{
if ( is_prime(i) )
{
while ( x % i == 0 )
{
cout << i << " ";
x = x / i;
}
}
}
}
int main(){
int x;
cin>>x;
prime_factors(x);
}
Aside of the code indentation woes (I fixed those for you), there are two major things wrong here:
You're modifying x within your prime_factors() function, and your loop tests against x to know when to exit early. You should make a copy of x so that you don't do this.
You're not restoring x between loops.
You can also cut the number of tests you're doing in half in your is_prime() function.
Corrected Code Listing
#include<iostream>
using namespace std;
bool is_prime( int fac )
{
int i;
bool found = true;
for( i = 2; i < fac; i++)
{
if ( fac % i == 0 )
{
found=false;
break;
}
}
return found;
}
void prime_factors( int x )
{
int i;
int test;
for ( i = 2; i <= x; i++ )
{
test = x;
if ( is_prime(i) )
{
while ( test % i == 0 )
{
cout << i << " ";
test /= i;
}
}
}
}
int main(){
int x;
cin >> x;
prime_factors(x);
}
Sample Output
./a.out
100000
2 2 2 2 2 5 5 5 5 5
./a.out
6
2 3
./a.out
1001
7 11 13
Related
I had an idea to make a program that would take user input and make a... I am not quite sure how to call it correctly, so my WIP is top down pyramid. So that we don't get confused it should look something like this.
If c is 5:
11111
10001
10101
10001
11111
If c is 7:
0000000
0111110
0100010
0101010
0100010
0111110
0000000
Here is an image to help visualize the problem
The only conditions are that there has to be a 1 in the middle and that cin is odd.
Now, I've been thinking about it in my spare time and it seemed quite easy in my head, but when I try to put my thoughts into my code it never works out.
Is there anyone who could help me? I am quite desperate .-.
PS: Here is my WIP code so far (Please excuse my Czech ints and texts)
#include <iostream>
using namespace std;
void FillArray(int **PyramidArray,int a,int b,int c);
void ExtractArray(int **PyramidArray,int a,int b,int c);
int main()
{
cout << "input array size.(only odd numbers)" << endl;
int c;
cin >> c;
if (c%2 == 0)
{
cout << "Only odd numbers!" << endl;
return 1;
}
int **PyramidArray;
PyramidArray = new int*[c];
for (int i =0;i<c;i++)
{
PyramidArray[i] = new int[i];
}
FillArray(PyramidArray,c,c,c);
ExtractArray(PyramidArray,c,c,c);
return 0;
}
void FillArray(int **PyramidArray, int a, int b, int c)
{
for(int i=0;i<a;i++)
{
for (int j=0;j<b;j++)
{
PyramidArray [i][j] = 1;
}
}
}
void ExtractArray(int **PyramidArray, int a,int b,int c)
{
for(int i=0;i<a;i++)
{
for (int j=0;j<b;j++)
{
cout << PyramidArray [i][j] << " ";
}
cout << endl;
}
cout << endl;
}
For solving this kind of problem, if you find you need a new, then something is wrong. Just keep tracing the current state of printing, everything can be done in one-pass.
#include <iostream>
int main() {
int n = 0, half_n = 0;
std::cin >> n;
if ( n % 2 == 0 ) return -1;
half_n = n / 2;
// the first symbol to print, 1 or 0
int start_symbol = 1 - half_n % 2;
// how many steps from beginning require alternating symbol ?
int alternate_range = 0;
// 0 for upperhalf, 1 for lowerhalf
int direct = 0;
for ( int i = 0; i < n; ++i ) {
int current_symbol = start_symbol;
for ( int j = 0; j < alternate_range; ++j ) {
std::cout << current_symbol;
current_symbol = 1 - current_symbol;
}
for ( int j = alternate_range; j < n - alternate_range; ++j ) {
std::cout << current_symbol;
}
for ( int j = n - alternate_range; j < n; ++j ) {
current_symbol = 1 - current_symbol;
std::cout << current_symbol;
}
std::cout << "\n";
if ( alternate_range == half_n ) {
direct = 1;
}
if ( direct == 0 ) {
++alternate_range;
} else {
--alternate_range;
}
}
}
Here is my program, which aims to show whether the input integer is a perfect number or not. It is required to use Boolean function and call it back in main function. However, after running the trial, there is no output. Can anyone help out this programming newbie...Thanks in advance for any help.
#include <iostream>
using namespace std;
bool perfect ( int num )
{
int sum = 0, i = 1;
while( i < num ) {
if ( num % i == 0 ) {
sum = sum + i;
i++;
}
}
if ( num == sum )
return 1 ;
else
return 0 ;
}
int main()
{
int num ;
cin >> num ;
if ( perfect ( num ) == 1 )
cout << " YES " << endl ;
else
cout << " NO " << endl ;
}
Let's look at your loop when num == 3 and i == 2.
int i = 1;
while( i < num ) {
if ( num % i == 0 ) {
sum = sum + i;
i++;
}
}
i < num is 2 < 3 which is true, so we'll enter the while loop.
num % i == 0 is 3 % 2 == 0 which is false, so we won't enter the conditional.
We head back to the top of the while loop.
i and num haven't changed, so this is an infinite loop.
You probably want something like:
bool perfect_number(int x) {
int sum_of_divisors = 0;
for (int divisor = 1; divisor < x; divisor++)
if (x % divisor == 0)
sum_of_divisors += divisor;
return sum_of_divisors == x;
}
Which we can optimize into:
bool perfect_number(int x) {
return x == 6 || x == 28 || x == 496 || x == 8128 || x == 33550336;
}
Your function perfect(int) return a bool and not an integer, so if(perfect(num)) can be used directly.
You could have used return type int for function perfect() to use 'if' condition as: if(perfect(num)==1)
I'm trying to print the following pattern:
*
**
***
****
*****
****
***
**
*
Now, I know how to do it using 4 for loops:
for(i=1;i<=n;i++)
{
for(j=1;j<=i;j++)
{
cout<<"*";
}
cout<<"\n";
}
to print the first half and to print the second half:
for(i=1;i<=n;i++)
{
for(j=n;j>i;j--)
{
cout<<"*";
}
cout<<"\n";
}
Looking closely, both the outer for loops are the same. i.e.,
for(i=1;i<=n;i++).
Is there anyway to nest both the 'j' for-loops inside the i-for loop?
Using only one loop:
#include <iostream>
#include <string>
int main() {
for (unsigned i = 0; i < 10; ++i)
std::cout << std::string( i < 5 ? (i+1) : 10 - (i+1), '*') << std::endl;
return 0;
}
Would you care for not even three loops, but just two loops?
int n=5, i, j, k;
for (i=0; i<n*2-1; i++)
{
j=i;
if (j >= n)
j=n*2-2-j;
for (k=0; k<=j; k++)
std::cout << '*';
std::cout << std::endl;
}
using 2 loops:
int g=1;
for(int i=0;i<=5;i++){
for (int y=0;y<=i;y+=g){
cout<<"*";
}
cout<<endl;
if (i==4 && g==1){
cout<<"*****";
i=3;
g=-1;
}}
Instead of printing 5 lines, and then another 5, you could print 10, and calculate the number of stars in each line.
for(i = 1; i <= 2*n - 1; i++)
{
for(j = 1; j <= n - abs(i - n); j++)
{
cout<<"*";
}
cout<<"\n";
}
The expression n-abs(i-n) evaluates to i for values of i between 1 and n, and to 2n-i for values of i greater than n.
Just for fun, how about one loop:
for (int i=1, j=0, dir=1; i!=0;) {
cout << '*';
++j;
if (j==i) {
cout << '\n';
j = 0;
i += dir;
if (i==6) {
dir = -1;
i -= 2;
}
}
}
You can do it in one loop (maybe cheating a little bit):
size_t max = 5;
size_t rows = max * 2 - 1;
std::string line(std::string(max, '*') + '\n');
for ( size_t j = 0, k = max; j < rows; ++j ) {
std::cout << line.c_str() + ( j < max ? --k : ++k );
}
I know you didn't ask for it, but for completeness here's one with zero loops (recursion instead):
#include <iostream>
void print_stars(int count)
{
if (count > 0)
{
std::cout << '*';
print_stars(count - 1);
}
}
void print_line(int lines, int stars)
{
if (lines == 1)
print_stars(stars);
else
{
if (stars > 0)
{
print_stars(stars);
std::cout << std::endl;
}
print_line(lines - 1, stars + 1);
std::cout << std::endl;
if (stars > 0)
print_stars(stars);
}
}
int main()
{
int star_count = 5;
print_line(star_count + 1, 0);
return 0;
}
The pattern can be outputted using only one for loop.
Here is a demonstrative program.
#include <iostream>
#include <iomanip>
int main()
{
while ( true )
{
const char c = '*';
std::cout << "Enter non-negative number (0 - exit): ";
unsigned int n;
if ( !( std::cin >> n ) || n == 0 ) break;
std::cout << '\n';
for ( unsigned int i = 1; i < 2 * n; i++ )
{
unsigned int w = i <= n ? i : 2 * n - i;
std::cout << std::setfill( c ) << std::setw( w + 1 ) << '\n';
}
std::cout << std::endl;
}
return 0;
}
If to enter sequentially
5 4 3 2 1 0
then the program output will look the following way
Enter non-negative number (0 - exit): 5
*
**
***
****
*****
****
***
**
*
Enter non-negative number (0 - exit): 4
*
**
***
****
***
**
*
Enter non-negative number (0 - exit): 3
*
**
***
**
*
Enter non-negative number (0 - exit): 2
*
**
*
Enter non-negative number (0 - exit): 1
*
Enter non-negative number (0 - exit): 0
Using the same variables of this well-structured program as function parameters you can write a separate function that outputs the pattern.
Here you are.
#include <iostream>
#include <iomanip>
std::ostream & pattern( unsigned int n, char c = '*', std::ostream &os = std::cout )
{
for ( unsigned int i = 1; i < 2 * n; i++ )
{
unsigned int w = i <= n ? i : 2 * n - i;
os << std::setfill( c ) << std::setw( w + 1 ) << '\n';
}
return os;
}
int main()
{
while ( true )
{
std::cout << "Enter non-negative number (0 - exit): ";
unsigned int n;
if ( !( std::cin >> n ) || n == 0 ) break;
std::cout << '\n';
pattern( n );
std::cout << std::endl;
}
return 0;
}
Take into account that for example it is a bad idea to use the standard class std::string to output the pattern because the program will be inefficient due to allocation and reallocation of the dynamic memory for an object of the class.
If not to use the standard stream manipulators then you can use standard algorithm std::fill_n to hide the inner loop. In this case the program also will have only one explicit loop.
For example
#include <iostream>
#include <algorithm>
#include <iterator>
int main()
{
while ( true )
{
const char c = '*';
std::cout << "Enter non-negative number (0 - exit): ";
unsigned int n;
if ( !( std::cin >> n ) || n == 0 ) break;
std::cout << '\n';
for ( unsigned int i = 1; i < 2 * n; i++ )
{
unsigned int w = i <= n ? i : 2 * n - i;
*std::fill_n( std::ostream_iterator<char>( std::cout ), w, c ) = '\n';
}
std::cout << std::endl;
}
return 0;
}
#include <iostream>
using namespace std;
int main() {
int i;
int j;
int count = 1;
for(i= 0;i<10;i++) {
if(i < 5) {
for(j=0;j<=i;j++) {
cout<<"*";
}
} else {
for(j=i-count;j>0;j--) {
cout<<"*";
}
count +=2;
}
cout<< "\n";
}
return 0;
}
I was trying to solve this problem with the following code. But the answers aren't accurate for all inputs.
Problem Statement
Ikbal has two arrays a and b of length N, initially all values equals to zero. We have Q operation. Let's define three types of operations on this arrays:
1 l r c Increase al,al+1,...,ar by c.
2 l r c Increase bl,bl+1,...,br by c.
3 l r Print (al∗bl)+(al+1∗bl+1)+...+(ar∗br) in modulo 1000000007
Input Format
First line of the input consists of N and Q. Next Q lines contain one of the three types of operations.
Constraints
1≤N≤109
1≤Q≤200000
1≤c≤10000
1≤l≤r≤N
Output Format
Whenever you get a type 3 operation, you should print the answer in a new line.
Sample Input
5 3
1 1 5 5
2 2 4 2
3 3 4
Sample Output
20
Explanation
After first operation arrays look like this:
a=5,5,5,5,5
b=0,0,0,0,0
After second operation arrays look like this:
a=5,5,5,5,5
b=0,2,2,2,0
Answer of the third operation: 5∗2+5∗2=20
**MY code **
#include <cmath>
#include <cstdio>
#include <vector>
#include <iostream>
#include <algorithm>
using namespace std;
int main() {
/* Enter your code here. Read input from STDIN. Print output to STDOUT */
vector<int> a,b,c;
int n,q,r,p;
cin >> n;
cin >> q;
for(int i=0;i<q;i++) {
cin >> r;
a.push_back(r);
if(r==3) {
p = 3;
} else {
p = 4;
}
for(int j=1;j<p;j++) {
cin >> r;
a.push_back(r);
}
}
vector<int> aa(n,0),bb(n,0);
int g,start,endd,val,anss=0;
for(int i=0;i<a.size();) {
if(a[i]==3) {
start = a[i+1]-1;
endd = a[i+2]-1;
if(start==endd) {
anss = (aa[start]*bb[start])%1000000007;
} else {
anss = (aa[start]*bb[start] + aa[endd]*bb[endd])%1000000007;
}
cout << anss << endl;
i+= 3;
} else {
start = a[i+1] - 1;
endd = a[i+2];
val = a[i+3];
if(a[i]==1) {
for(int j=start;j<endd;j++) {
aa[j] += val;
}
} else {
for(int j=start;j<endd;j++) {
bb[j] += val;
}
}
i+= 4;
}
}
/*
for(int i=0;i<n;i++) {
cout << aa[i] << " " ;
cout << bb[i] << endl;
}
for(int i=0;i<a.size();i++) {
cout << a[i] << endl;
} */
return 0;
}
Expected output for given input :
http://i.stack.imgur.com/4OsSo.jpg
It's not an overflow problem. This:
if(start==endd)
anss = (aa[start]*bb[start])%1000000007;
else
anss = (aa[start]*bb[start] + aa[endd]*bb[endd])%1000000007;
is flat wrong. You misread the instructions.
Be careful not to cause overflow.
You have to calculate (al∗bl)+(al+1∗bl+1)+...+(ar∗br), not (al∗bl)+(ar∗br)
At least, the output for the given input
10 20
1 9 9 6768
2 5 5 2202
3 7 7
2 3 9 1167
2 1 7 8465
3 1 5
2 1 1 1860
3 9 9
2 5 5 2153
1 5 7 749
3 1 1
2 8 10 3129
3 1 1
1 2 10 2712
2 1 8 79
1 1 6 4645
1 7 7 1358
3 2 10
1 9 9 8677
3 8 10
is corrected.
#include <cmath>
#include <cstdio>
#include <vector>
#include <iostream>
#include <algorithm>
using namespace std;
int add(int a, int b) {
int r = a + b;
if (r >= 1000000007) r -= 1000000007;
return r;
}
int mul(int a, int b) {
int r = 0;
while (b > 0) {
if (b % 2 != 0) r = add(r, a);
a = add(a, a);
b /= 2;
}
return r;
}
int main() {
/* Enter your code here. Read input from STDIN. Print output to STDOUT */
vector<int> a,b,c;
int n,q,r,p;
cin >> n;
cin >> q;
for(int i=0;i<q;i++) {
cin >> r;
a.push_back(r);
if(r==3) {
p = 3;
} else {
p = 4;
}
for(int j=1;j<p;j++) {
cin >> r;
a.push_back(r);
}
}
vector<int> aa(n,0),bb(n,0);
int g,start,endd,val,anss=0;
for(int i=0;i<a.size();) {
if(a[i]==3) {
start = a[i+1]-1;
endd = a[i+2]-1;
#if 0
if(start==endd) {
anss = (aa[start]*bb[start])%1000000007;
} else {
anss = (aa[start]*bb[start] + aa[endd]*bb[endd])%1000000007;
}
#else
anss = 0;
if (start <= endd) {
for(int j=start;j<=endd;j++)anss = add(anss, mul(aa[j], bb[j]));
} else {
for(int j=endd;j<=start;j++)anss = add(anss, mul(aa[j], bb[j]));
}
#endif
cout << anss << endl;
i+= 3;
} else {
start = a[i+1] - 1;
endd = a[i+2];
val = a[i+3];
if(a[i]==1) {
for(int j=start;j<endd;j++) {
#if 0
aa[j] += val;
#else
aa[j] = add(aa[j], val);
#endif
}
} else {
for(int j=start;j<endd;j++) {
#if 0
bb[j] += val;
#else
bb[j] = add(bb[j], val);
#endif
}
}
i+= 4;
}
}
/*
for(int i=0;i<n;i++) {
cout << aa[i] << " " ;
cout << bb[i] << endl;
}
for(int i=0;i<a.size();i++) {
cout << a[i] << endl;
} */
return 0;
}
I'm doing an online challenge and the challenge is the following:
"Kids are playing a game called "Counting digits". For given numbers S and K, they firstly write all numbers between those numbers and then count how many times each digit appears (0,1,2,3,4,5,6,7,8,9). For example, S=767, K=772, numbers will be: 767,768,769,770,771,772
So, 0 will show once (in 770), 1 will show once (in 771) and so on..
Basically, my program have to do the following (given example):
Input:
1 9
(These are numbers 1,2,3,4,5,6,7,8,9)
Output:
0 1 1 1 1 1 1 1 1 1
(0 doesn't show, other numbers show once)."
I'm stuck on this code... out of ideas.
#include <iostream>
using namespace std;
int main()
{
int s,k;
int array[10];
int c0=0,c1=0,c2=0,c3=0,c4=0,c5=0,c6=0,c7=0,c8=0,c9=0;
cin >> s >> k;
int saves = s;
int savek = k;
cout << s%10;
for(int i=s;i<=k;i++)
{
int savei=i;
while(savei!=0)
{
savei=savei%10;
}
}
Any pseudo code/snippet/code/hint is appreciated.
Purely numeric solution to a purely numeric problem:
#include <iostream>
int main()
{
int s, k, i, tmp;
std::cin >> s >> k;
int count[10] = { 0 };
for (i = s; i <= k; i++) {
tmp = i;
do {
count[tmp % 10]++;
tmp /= 10;
} while(tmp);
}
for (i = 0; i < 10; i++) {
std::cout << i << " appears " << count[i] << " times" << std::endl;
}
return 0;
}
My solution is like this:
int main(){
int s,k;
cin >> s >> k;
int numbers[10]={0};
string sum;
for(int i=s;i<=k;i++)
{
sum=to_string(i);
for(int i=0;i<sum.length();i++){
numbers[(int)sum.at(i)-48]++;
}
}
for(int i=0;i<10;i++){
cout<<numbers[i]<<endl;
}
return 0;
}
public static void getDigitsInBook(int n) {
for(int i=0;i<10;i++) {
int x = n,val=0,k=1;
while(x!=0) {
int left = x/10;
int num = x%10;
int right = n%k;
if(i == 0) {
val = val+ (left*k);
}
else if(i<num) {
val = val + ((left+1)*k);
}
else if(i==num) {
val = val + (left*k) + right+1;
}
else {
val = val+ (left*k);
}
k=k*10;
x = n/k;
}
System.out.println(val);
}
}
What you usually do with such tasks is calculating the number between 0 and S and between 0 and K and subtracting those.
How many are between 0 and 767? First count the numbers of the last digit. There are 77 times 0, 1, 2, 3, 4, 5, 6, 7 each and 76 times 8 and 9. More formally, 767/10+1 between 0 and 767%10 and 767/10+1 on the rest. Then calculate the number of occurences of the last digit for 767/10=76, multiply by 10, add 7 times 7 and 6 (for the error on the last one) and do the same for the remaining digits, here 76/10=7. Finally, add the results up.
This solves the problem in O(log_10 K).
try this code:
for(int n=s ; n<=k ; n++)
{
tempN = abs(n);
while(tempN > 0)
{
tempDigit = tempN % 10;
tempN /= 10;
//count tempDigit here
}
}
assuming your variables are ints, "tempN /= 10;" should be no problem.