Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <stdio.h>
- using namespace std;
- int countDigits(int num)
- {
- int cnt=0;
- while (num>0)
- {
- num = num / 10;
- cnt++;
- }
- return (cnt);
- }
- bool HappyChet(int num, int numLen)
- {
- int j, n1 = 0, n2 = 0;
- for (j=0; j<numLen; j++)
- {
- if (j >= numLen/2)
- n1 += num % 10;
- else
- n2 += num % 10;
- num = num / 10;
- }
- return n1==n2;
- }
- bool HappyNechet(int num, int numLen)
- {
- int j, n1 = 0, n2 = 0;
- for (j=0; j<numLen; j++)
- {
- if (j >= (numLen+1)/2)
- n1 += num % 10;
- else
- n2 += num % 10;
- num = num / 10;
- }
- return n1==n2;
- }
- int main(int argc, char* argv[])
- {
- bool *n;
- int len =0;
- int x=0,y=0;
- if(argc <2)
- {
- cerr<<"Wgong argument"<<endl;
- return(0);
- }
- sscanf(argv[1], "%d-%d", &x, &y);
- if((x<0) || (x>y) || (x==y))
- { cout<<"Wrong range"<<endl;
- return(-1);
- }
- for (int i=2;i<=y;i++) {
- n[i] = true;
- }
- for (int i=2;i<=y;i++) {
- if (n[i] == true) {
- for (int j=i*i;j<=y;j+=i)
- n[j] = false;
- }
- }
- for (int i=x; i<=y;i++) {
- if (n[i] == true)
- { len = countDigits(i);
- if (len % 2 == 0)
- {
- if (HappyChet(i, len))
- cerr << i << endl;}
- }
- }
- for(int i=x; i<=y;i++) {
- if (n[i] == true)
- { len = countDigits(i);
- if (len % 2 != 0)
- {
- if (HappyNechet(i, len))
- cerr << i << endl;}
- }
- }
- return(0);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement