1. 程式人生 > >1537.Tiger Eat People SDNUOJ1537(2018總結賽)

1537.Tiger Eat People SDNUOJ1537(2018總結賽)

Description
As we all know, lls is the boss of SDNU ACM Training Team, so many team members may be afraid of him.

But this is not the case, lls is very kindly to us. To show his love to us, he plan to give all of us an “Accepted”. The problem shows as follows:
There are n numbers ​.Each time you can choose a subset of them(may be empty), and then add them up.Count how many numbers can be represented in this way.
Input
The first line of the input contains an integer T, denoting the number of test cases.
In each test case, there is a single integers n in one line, as described above.
, ​
Output
For each test case, output one line contains a single integer, denoting the answer.
Sample Input
4
9
7
8
233
Sample Output
512
128
256
13803492693581127574869511724554050904902217944340773110325048447598592

AC程式碼(這就是模擬嗎,有比這簡單很多的方法)

#include<cstdio>
#include<iostream>
#include<algorithm>
#include<cmath>
#include<cstring>
using namespace std;


void solve(int n)
{
    char a[10005] = "2";
            char sum[10005];
            for(int k = 0; k < n - 1; ++k)
            {
                int len = strlen(a);
//            cout << "a的長度 : " << len << '\n';
                int j = 0;
                int i = 0;
                for(; i < len; ++i)
                {
                    sum[i] = (a[i] + a[i] - '0' - '0' + j ) % 10 + '0';
//                cout << "該位的和對10取餘" << sum[i] << '\n';
                    j = (a[i] + a[i] - '0' - '0' + j ) / 10;
//                cout << "該位進位" << j << '\n';
                }
                if(j != 0)
                {
                    sum[i] = j + '0';
//                cout << "最後一位的進位處理" << sum[i] << '\n';
                }
                strcpy(a, sum);
//            cout << "當前的a " << a << '\n';
            }
            if(n == 1)
                strcpy(sum, a);
            if(n == 0)
                strcpy(sum, "1");
//        reverse(sum, sum + sizeof(sum));
            reverse(sum, sum + strlen(sum));
            cout << sum << '\n';
            memset(sum, 0, sizeof(sum));
            memset(a, 0, sizeof(a));
}

int main()
{
    int t;
    while(~scanf("%d", &t))
    {
        for(int m = 0; m < t; ++m)
        {
            int n;
            cin >> n;
            solve(n);
        }
    }
    return 0;
}

簡單版

#include <iostream>
#include <iomanip>
//#include <algorithm>
#include <cmath>
using namespace std;
int main()
{
    int t;
    cin>>t;
    while(t--)
    {
//        double n;
        int n;
        cin >> n;
        ///std::setprecision(n)控制輸出n位有效數字
        ///與fixed連用表示控制輸出到小數點後n位
        cout << fixed << std::setprecision(0) << pow(2, n) << '\n' ;
    }
    return 0;
}