Volume 1. Maths

113 - Power of Cryptography

import java.math.BigInteger;
import java.util.Scanner;

public class Main {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        BigInteger num1, num2;
        int n;
        int kc = 1000000000;
        int l, r, mid, t;
        Scanner in = new Scanner(System.in);
        while (in.hasNext())
        {
            n = in.nextInt();
            num1 = in.nextBigInteger();
            l = 0;
            r = kc;
            while (true)//二分
            {
                mid = (l + r) >> 1;
                num2 = BigInteger.valueOf(mid);
                num2 = num2.pow(n);
                t = num2.compareTo(num1);
                if (t == 0)
                {
                    System.out.println(mid);
                    break;
                }
                else
                    if (t > 0)
                        r = mid - 1;
                    else
                        l = mid + 1;
            }
        }
    }

}
View Code

 10161 Ant on a Chessboard

#include <iostream>
#include <fstream>
#include <cstring>
#include <string>
#include <algorithm>
#include <iomanip>
#include <cstdio>
#include <cstdlib>
#include <cmath>

using namespace std;

int main()
{
    long long int n, nt, mid, x, y;
    while (cin>>n, n)
    {
        x = y = nt = ceil(sqrt(n));
        mid = (nt * nt + (nt - 1) * (nt - 1) + 1) >> 1;/**< long long 防止mid超了 */
        //cout<<mid<<endl;
        if (n != mid)
        {
            if (n > mid)
            {
                if (nt & 1)/**< n为奇数, 从左到右,再下 在左边*/
                {
                    x = x - n + mid;
                }
                else/**< 从下到上再到左 在下边*/
                {
                    y = y - n + mid;
                }
            }
            else
            {
                if (nt & 1)/**< n为奇数, 从左到右,再下 在下边*/
                {
                    y = y + n - mid;
                }
                else/**< 从下到上再到左 在左边*/
                {
                    x = x + n - mid;
                }
            }
        }
        cout<<x<<' '<<y<<endl;
    }
    return 0;
}
View Code

 253 - Cube painting

#include <iostream>
#include <fstream>
#include <cstring>
#include <string>
#include <algorithm>
#include <iomanip>
#include <cstdio>
#include <cstdlib>
#include <cmath>

using namespace std;
char s[8], ts[8];
int a[][6] =/**< 枚举,总共24种情况,
在枚举中可以发现(1, 6)(2, 5)(3, 4)两两一组,
当其中一组换位置时,另外两组中必须只有一组需要换位置,
或者但两组互换时,其中一组也要换位置 */
{
    1, 6, 2, 5, 3, 4,
    1, 6, 5, 2, 4, 3,
    1, 6, 3, 4, 5, 2,
    1, 6, 4, 3, 2, 5,
    6, 1, 5, 2, 3, 4,
    6, 1, 2, 5, 4, 3,
    6, 1, 4, 3, 5, 2,
    6, 1, 3, 4, 2, 5,
    2, 5, 6, 1, 3, 4,
    2, 5, 1, 6, 4, 3,
    2, 5, 3, 4, 1, 6,
    2, 5, 4, 3, 6, 1,
    5, 2, 1, 6, 3, 4,
    5, 2, 6, 1, 4, 3,
    5, 2, 4, 3, 1, 6,
    5, 2, 3, 4, 6, 1,
    3, 4, 2, 5, 6, 1,
    3, 4, 5, 2, 1, 6,
    3, 4, 1, 6, 2, 5,
    3, 4, 6, 1, 5, 2,
    4, 3, 2, 5, 1, 6,
    4, 3, 5, 2, 6, 1,
    4, 3, 6, 1, 2, 5,
    4, 3, 1, 6, 5, 2
};
bool fun()
{
    int i, j;
    for (i = 1; i < 24; i++)
    {
        for (j = 0; j < 6; j++)
            if (s[a[0][j]] != ts[a[i][j]])
                break;
        if (j == 6)
            return true;
    }
    return false;
}
int main()
{
    int i = 1;
    while (cin>>s[i++])
    {
        for (; i < 7; i++)
            cin>>s[i];
        s[i] = '';
        for (i = 1; i < 7; i++)
            cin>>ts[i];
        ts[i] = '';
       cout<<(fun() ? "TRUE" : "FALSE")<<endl;
       if (cin.get() == EOF)
            break;
       i = 1;
    }
    return 0;
}
View Code

 10025 - The ? 1 ? 2 ? ... ? n = k problem

 1 /**< 思路:
 2 先记f(n) = (n*(n+1))/2;
 3 现将k转换为正数,求k对应于f(n)的最小上界n,
 4 从n开始,
 5 第一步:如果f(n) - k这个数是偶数,则输出n,否则
 6 n++,继续第一步。
 7 
 8 i = f(n) - k>=0肯定成立,
 9 如果i为要使得f(n) = ?1?2?...?n = k;
10 就要在f(n)中将i的一半为正,一半为负即可。所以i必定为偶数。
11  */
12 #include <iostream>
13 #include <cmath>
14 using namespace std;
15 
16 int main()
17 {
18     int n, k, i;
19     cin>>n;
20     while (n--)
21     {
22         cin>>k;
23         if (k == 0)//为0输出3
24         {
25             i = 3;
26         }
27         else
28         {
29             if (k < 0)
30             {
31                 k = -k;
32             }
33             i = sqrt(k * 2);
34             if (((i * (i + 1)) / 2 - k) < 0)
35                 i++;
36             while (((i * (i + 1)) / 2 - k) & 1)
37             {
38                 i++;
39             }
40         }
41 
42         cout<<i<<endl;
43         if (n)
44         {
45             cout<<endl;
46         }
47     }
48     return 0;
49 }
View Code

 591 - Box of Bricks

#include <iostream>
#include <cmath>
using namespace std;
int arr[55];
int main()
{
    int n, avg, s, t = 1;
    while (cin>>n, n)
    {
        s = avg = 0;
        for (int i = 0; i < n; i++)
        {
            cin>>arr[i];
            avg += arr[i];
        }
        avg /= n;
        for (int i = 0; i < n; i++)
        {
            s += abs(arr[i] - avg);
        }
        cout<<"Set #"<<t++<<endl;
        cout<<"The minimum number of moves is "<<s / 2<<"."<<endl<<endl;
    }
    return 0;
}
View Code

 621 - Secret Research

//这样就过了
#include <iostream>
#include <string>
using namespace std;
string s;
int main()
{
    int n, len;
    cin>>n;
    char r;
    while(n--)
    {
        cin>>s;
        len = s.size() - 1;
        if (len <= 1)
        {
            r = '+';
        }
        else
            if (s[len] == '5' && s[len - 1] == '3')
            {
                r = '-';
            }
            else
                if (s[len] == '4' && s[0] == '9')
                {
                    r = '*';
                }
                else
                {
                    r = '?';
                }
        cout<<r<<endl;
    }
    return 0;
}

//这样始终过不了,实在是想不明白
#include <iostream>
#include <string>
using namespace std;
string s;
int main()
{
    int n, len;
    cin>>n;
    char r;
    while(n--)
    {
        cin>>s;
        len = s.size() - 1;
        if (len <= 1)
        {
            r = '+';
        }
        else
            if (s[len] == '5')
            {
                r = '-';
            }
            else
                if (s[len] == '4')
                {
                    r = '*';
                }
                else
                {
                    r = '?';
                }
        cout<<r<<endl;
    }
    return 0;
}
View Code

 846 - Steps

#include <iostream>
#include <cmath>
using namespace std;

int main()
{
    int t, x, y, ans;
    cin>>t;
    while(t--)
    {
        cin>>x>>y;
        ans = 0;
        x = abs(x - y);
        if (x <= 3)
        {
            ans = x;
        }
        else
        {
            y = sqrt(x);
            while (x - (1 + y) * y / 2 < (y - 1) * y / 2)
            {
                y--;
            }
            while (x - (1 + y) * y / 2 - (y - 1) * y / 2 > 1)
            {
                ans++;
                x -= y;
            }
            if (x - (1 + y) * y / 2 - (y - 1) * y / 2 == 1)
            {
                ans++;
            }
            ans += 2 * y - 1;
        }
        cout<<ans<<endl;
    }
    return 0;
}
View Code

573 - The Snail

#include <iostream>

using namespace std;

int main()
{
    double h, u, d, f;
    int days;
    double dis, dre;
    while (cin>>h>>u>>d>>f, h)
    {
        days = 1;
        dis = 0;
        dre = u * f / 100;
        while (true)
        {
            dis += u;
            if (dis > h)
            {
                cout<<"success on day "<<days<<endl;
                break;
            }
            dis -= d;
            if (dis < 0)
            {
                cout<<"failure on day "<<days<<endl;
                break;
            }
            u -= dre;
            if (u < 0)
            {
                u = 0;
            }
            days++;
        }
    }
    return 0;
}
View Code

 10499 - The Land of Justice

#include <iostream>

using namespace std;

int main()
{
    long long int n;
    while (cin>>n, n >= 0)
    {
        if (n <= 1)
            cout<<"0%"<<endl;
        else
            cout<<25 * n<<"%"<<endl;
    }
    return 0;
}
View Code

 10790 - How Many Points of Intersection?

/*
f(a, b)=f(a-1,b)+¡¾(a-1)*(b-1) + 0¡¿* b/2
f(a-1, b)=f(a-2,b)+¡¾(a-2)*(b-1) + 0¡¿* b/2
f(a-2, b)=f(a-3,b)+¡¾(a-3)*(b-1) + 0¡¿* b/2
f(2, b)=0+¡¾(b-1)¡¿* b/2
F(a,b) =(b-1)*b/2 *¡¾1+ ...+ (a-3) + (a-2)+(a-1)¡¿
=(b-1)*(b)/2 * a(a-1)/2
*/
#include <iostream>

using namespace std;

int main()
{
    long long a, b, t = 1, bb;
    while (cin>>a>>b, a | b)
    {
        if (b & 1)
        {
            bb = (b - 1) / 2;
        }
        else
        {
            bb = b / 2;
            b = b - 1;
        }
        cout<<"Case "<<t++<<": "<<a * (a - 1) / 2 * b * bb<<endl;
    }
    return 0;
}
View Code
原文地址:https://www.cnblogs.com/jecyhw/p/3691605.html