Results 1 to 8 of 8

Thread: C++

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Registered User
    Join Date: Jul:2014
    Location: Dobrich
    Posts: 10

    C++

    1 n , , 1 endless loop . , .

    #include<iostream>
    using namespace std;

    Code:
    int main()
    {
    	int n;
    	cout << "Enter N:";
    	cin >> n;
    	int automorphicNum = 1;
    	int squareAutoNum = automorphicNum*automorphicNum;
    	while (n != 0)
    	{
    		int y = automorphicNum;
    		int x = squareAutoNum;
    		bool isAutomorphic = true;
    
    		while (y != 0)
    		{
    			int r = y % 10;
    			int q = x % 10;
    			y /= 10;
    			x /= 10;
    
    			if (r != q)
    			{
    				isAutomorphic = false;
    			}
    
    			if (isAutomorphic && y == 0)
    			{
    				n--;
    				cout << automorphicNum<<",";
    				automorphicNum++;
    			}
    			else if (!isAutomorphic && y == 0)
    			{
    				automorphicNum++; 
    			}
    		}
    	}
    
    	return 0;
    }

  2. #2
    Registered User suggs's Avatar
    Join Date: Jul:2009
    Location: ,
    Posts: 1,276
    , ... , , squareAutoNum automorphicNum*automorphicNum , . 1. "automorphicNum++; " "if-else", .
    , .
    .

  3. #3
    ɐ-əpoɔᴉu⋂ ɐ ə anrieff's Avatar
    Join Date: Apr:2004
    Location: Sofia
    Posts: 8,448
    while

    Code:
    for (int automorphicNum = 1; automorphicNum <= n; automorphicNum++)
    / n automorphicNum , for- .

    Code:
    			if (isAutomorphic && y == 0)
    			{
    				n--;
    				cout << automorphicNum<<",";
    				automorphicNum++;
    			}
    			else if (!isAutomorphic && y == 0)
    			{
    				automorphicNum++; 
    			}
    while.

    *off: C++, . , :

    Code:
    n = int(raw_input("N = "))
    print filter(lambda x: str(x*x)[-len(str(x)):] == str(x), xrange(1, n))
    " ". 200 project euler- , C++,
    Last edited by anrieff; 5th November 2014 at 09:00.
    , . .
    "640K ught to be enough for anybody" - Bill Gates, 1981
    ::Machine specs::Fract::AGG::::Baileys::blog::YouTube channel

  4. #4
    Registered User ined's Avatar
    Join Date: Nov:2006
    Location: Varna
    Posts: 1,078
    Code:
    #include <iostream>
    
    using namespace std;
    
    int main () {
        unsigned long long a=1, aq, i=0, n, z=10;
        
        cout<<"N = ";
        cin>>n; 
        if (n>18) n=18;
    
        while (i<n) {
             aq = a * a;
             if (aq%z == a) {
                 i++;
                 cout<<i<<".  "<<a<<"  "<<aq<<endl;;
                 }
             a++;
             if (a>z) z*=10;
        }
        system("pause");
        return 0;    
    }
    n 18 - * unsigned long long (64 )

    [edit] 1 . 10x hateras
    Last edited by ined; 5th November 2014 at 11:12.
    ASRock B85 Pro4, Pentium G3240, DD3 8GB/1333MHz, 120GB SSD + 1TB + 640GB HDD
    Gigabyte GV-R657OC-1GI, CM B500, ASUS 24B1S1, LCD SAMSUNG SM931BW, 173

  5. #5
    ɐ-əpoɔᴉu⋂ ɐ ə anrieff's Avatar
    Join Date: Apr:2004
    Location: Sofia
    Posts: 8,448
    . , 1..n, n . , squareAutoNum:

    Code:
    int squareAutoNum = automorphicNum*automorphicNum;
    while , .


    out of curiosity, :
    Code:
    import itertools
    n = int(raw_input("N = "))
    print list(itertools.islice((x for x in itertools.count(1) if str(x*x)[-len(str(x)):] == str(x)), 0, n))
    , . .
    "640K ught to be enough for anybody" - Bill Gates, 1981
    ::Machine specs::Fract::AGG::::Baileys::blog::YouTube channel

  6. #6
    Registered User hateras's Avatar
    Join Date: Jan:2011
    Location: Kronos III
    Posts: 1,028
    /off
    @anrieff:
    , , ...
    - ,
    /eoff

    @ined: .

    , , 5 6.
    , .
    ASRock B550M Pro 4; Ryzen R5 3600; 2x16 GiB G.SKILL Aegis 3200; 1TB Samsung QVO 960 + 3TB Seagate IronWolf; Zalman Z1

  7. #7
    Registered User ined's Avatar
    Join Date: Nov:2006
    Location: Varna
    Posts: 1,078
    , - - - . 4*109 n 18 - 1018 n 33

    Code:
    #include <iostream>
    #include <iomanip>
    
    #define  MIL 1000000000ull
    
    using namespace std;
    
    int i=0, n;
    
    void print(unsigned long long a) {
          i++;
          cout<<setw(2)<<i<<". "<<setw(3+n/2)<<a<<endl;
    }
     
    unsigned long long ssq(unsigned long long a) {
         unsigned long long h, l, r, s;
         
         l = a % MIL;
         h = a / MIL;
         
         r = l * l;
         if (h==0) return r;
         s = ((l * h) % MIL) * MIL;
         return r+s+s;
    }
         
    int main () {
        unsigned long long a5=5, s5, z5=10;
        unsigned long long a6=6, s6, z6=10;
        
        cout<<"N = ";
        cin>>n; 
        
        if (n>33) n=33;  
        
        print(1);
    
        while (i<n) {
             if (a5<a6) {
                 if (ssq(a5)%z5 == a5) {
                     print(a5);
                     s5 = z5;
                     z5 *= 10;
                 }
                 a5+=s5;
                 if (a5>z5) z5*=10;
             }
             if (i==n) break;
             if (a5>a6) {
                  if (ssq(a6)%z6 == a6) {
                      print(a6);
                      s6 = z6;
                      z6 *=10;
                  }
                  a6+=s6;
                  if (a6>z6) z6*=10;
             }
        }
        system("pause");
        return 0;    
    }
    Last edited by ined; 7th November 2014 at 18:41.
    ASRock B85 Pro4, Pentium G3240, DD3 8GB/1333MHz, 120GB SSD + 1TB + 640GB HDD
    Gigabyte GV-R657OC-1GI, CM B500, ASUS 24B1S1, LCD SAMSUNG SM931BW, 173

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  

Copyright © 1999-2011 . .
iskamPC.com | mobility.BG | Bloody's Techblog | | 3D Vision Blog |