zoukankan      html  css  js  c++  java
  • 2^n算法

    题目描述:

    如题……

    那么,可以用c++自带快速幂:pow:

    #include<cstdio>
        using namespace std;
        int n;
        int main()
        {
            scanf("%d",&n);
            printf("%d",pow(2,n));
            return 0;
        }

    也可以用手写的快速幂,这里就不给了。

    但是有的时候用的时候会出现超内存的情况,所以建议尽量不要用这样的自带函数(小水题就算了),写一个正版的(高精度+STL):

    #include <iostream>
        #include <vector>
        #include <fstream>
        int N; 
        using namespace std;
        int main()
        {
         scanf("%d",&N);
         vector<int> result;
         result.push_back(1);
         vector<int>::iterator it;
         for (int i = 1; i <= N; i++)
         {
             for (it = result.begin(); it != result.end(); it++)
             {
                 *it = (*it) * 2;
             }
             for (size_t i = 0; i < result.size(); i++)
             {
                 if (result[i] > 9)
                 {
                     if (i == result.size() - 1)
                     {
                         result.push_back(result[i] / 10);
                     }
                     else
                     {
                         result[i + 1] += result[i] / 10;
                     }
                     result[i] %= 10;
                 }
             }
         }
         ofstream out;
         out.open("result.txt", ios::out | ios::trunc);
         if (!out.is_open())
         {
             cout << "open error";
             return -1;
         }
         for (int i = result.size() - 1; i > -1; i--)//???????????
         {
             out << result[i];
             cout << result[i];
         }
         out.close();
         cout << endl;
         system("pause");
         return 0;
        }

    这里有一个#include < fstream > 不用管它,这个是输出一个文本而已,删了也行(跟freopen差不多,因为提交要求而添加的)。

  • 相关阅读:
    Electron dialog 对话框的使用
    Electron BrowserView 的使用
    自动化测试相关
    使用chrome开发者工具中的performance面板解决性能瓶颈
    Electron window.open 函数 和 Browser-window-proxy 对象的使用
    Electron webview 标签
    Electron File对象
    Electron 进程
    JAVA日报
    JAVA日报
  • 原文地址:https://www.cnblogs.com/Zhoier-Zxy/p/8075534.html
Copyright © 2011-2022 走看看