Code(2.5)

mmm is learning division, she's so proud of herself that she can figure out the sum of all the divisors (约数)of numbers no larger than 100 within one day! 
But her teacher said "What if I ask you to give not only the sum but the square-sums(平方和) of all the divisors of numbers within hexadecimal(十六进制) number 100?" mmm get stuck and she's asking for your help. 
Attention, because mmm has misunderstood teacher's words, you have to solve a problem that is a little bit different. 
Here's the problem, given n, you are to calculate the square sums of the digits of all the divisors of n, under the base m. 
InputMultiple test cases, each test cases is one line with two integers. 
n and m.(n, m would be given in 10-based) 
1≤n≤10  9 
2≤m≤16 
There are less then 10 test cases. 
OutputOutput the answer base m.Sample Input
10 2
30 5
Sample Output
110
112

        
  

Hint

Use A, B, C...... for 10, 11, 12......
Test case 1: divisors are 1, 2, 5, 10 which means 1, 10, 101, 1010 under base 2, the square sum of digits is 
1^2+ (1^2 + 0^2) + (1^2 + 0^2 + 1^2) + .... = 6 = 110
#include <iostream>
using namespace std;
char x[20]="0123456789ABCDEFG";//进制转换模板
int n,m;
int f(int x)
{
int  a[33],i=0;
while(x)
{
    a[i++]=x%m;
    x/=m;
}
int ans=0;
while(i--)
    ans+=a[i]*a[i];
    return ans;
}
int solve ()
{
    int i,ans=0;
    for(i=1;i*i<=n;i++)
        if(n%i==0)
    {
        ans+=f(i);
        if(i*i!=n)ans+=f(n/i);
    }
    return ans;
}
int main()
{
    while(cin>>n>>m){
        int a[33],i=0,ans=solve();
    while(ans){
        a[i++]=ans%m;
        ans/=m;
    }
    while(i--)
        cout<<x[a[i]];
    cout<<endl;
    }
        return 0;
} 
 
 
 
 
 
 
Alice and Bob are the topmost hunters in the forest,(森林中最强的猎手) so no preys can escape from them. However, they both think that its hunting skill is better than the other. So they need a match. In their match, the targets are two animals, a tiger and a wolf. They both know that the tiger is living in the south of the forest and the wolf is living in the north of the forest. They decide that the one who kills the tiger scores X points and who kills the wolf scores Y points. If the one who kills both tiger and wolf scores X+Y points. Before the match starts, Alice is in the east of the forest and Bob is in the west of the forest. When the match starts, Alice and Bob will choose one of the preys as targets. Because they haven't known the other's choice, maybe they choose the same target. There will be two situations: (1) If they choose different targets, they both are sure of killing their respective targets. (2) If they choose the same target, the probability of Alice killing the target is P, and the probability of Bob killing it is 1-P. Then they will hunt for the other prey, also the probability of Alice killing it is P and the probability of Bob killing it is 1-P. But Alice knows about Bob. She knows that the probability of Bob choosing tiger as his first target is Q, and the probability of choosing wolf is 1-Q. So that Alice can decide her first target to make her expected score as high as possible. 
InputThe first line of input contains an integer T (1≤T≤10000), the number of test cases. Then T test cases follow. Each test case contains X, Y, P, Q in one line. X and Y are integers and 1≤X, Y≤1000000000. P and Q are decimals and 0≤P, Q≤1, and there are at most two digits after decimal point. OutputFor each test case, output the target Alice should choose and the highest expected score she can get, in one line, separated by a space. The expected score should be rounded to the fourth digit after decimal point. It is guaranteed that Alice will have different expected score between choosing tiger and wolf.Sample Input
3
2 1 0.5 0.5
2 1 0 1
7 7 0.32 0.16
Sample Output
tiger 1.7500
wolf 1.0000
tiger 6.5968
 
 
#include <iostream>
using namespace std;
int main()
{
    int t,x,y;
    double p,q,d1,d2;
    cin>>t;
    while(t--)
    {
        cin>>x>>y>>p>>q;
//x是指的狼y指的老虎        
d1=(1-q)*x+p*q*(x+y);(bob选择狼那么Alice选择虎获得100%x加上 bob也选择虎然后他俩在一起选择狼运算式就是x+y*q*p)
        d2=(1-q)*p*(x+y)+q*y;(都先选择狼,再加上一虎一狼)
        if(d1>d2)
            printf("tiger %.4lf\n",d1);
        else
            printf("wolf %.4lf\n",d2);
    }
    return 0;
}(题目想知道Alice是应该选择虎还是 选择狼)
 
 

Everybody in Russia uses Gregorian calendar. In this calendar there are 31 days in January, 28 or 29 days in February (depending on whether the year is leap or not), 31 days in March, 30 days in April, 31 days in May, 30 in June, 31 in July, 31 in August, 30 in September, 31 in October, 30 in November, 31 in December.

A year is leap in one of two cases: either its number is divisible by 4, but not divisible by 100, or is divisible by 400. For example, the following years are leap: 20002004, but years 1900 and 2018 are not leap.

In this problem you are given n (1 ≤ n ≤ 24) integers a1, a2, ..., an, and you have to check if these integers could be durations in days of n consecutive months, according to Gregorian calendar. Note that these months could belong to several consecutive years. In other words, check if there is a month in some year, such that its duration is a1 days, duration of the next month is a2 days, and so on.

Input

The first line contains single integer n (1 ≤ n ≤ 24) — the number of integers.

The second line contains n integers a1, a2, ..., an (28 ≤ ai ≤ 31) — the numbers you are to check.

Output

If there are several consecutive months that fit the sequence, print "YES" (without quotes). Otherwise, print "NO" (without quotes).

You can print each letter in arbitrary case (small or large).

Example
Input
4
31 31 30 31
Output
Yes

Input
2
30 30
Output
No

Input
5
29 31 30 31 30
Output
Yes

Input
3
31 28 30
Output
No

Input
3
31 31 28
Output
Yes

Note

In the first example the integers can denote months July, August, September and October.

In the second example the answer is no, because there are no two consecutive months each having 30 days.

In the third example the months are: February (leap year) — March — April – May — June.

In the fourth example the number of days in the second month is 28, so this is February. March follows February and has 31 days, but not 30, so the answer is NO.

In the fifth example the months are: December — January — February (non-leap year).


#include <iostream>
#include <stdio.h>
using namespace std;
int n ,a[3],m[13]= {0,31,28,31,30,31,30,31,31,30,31,30,31};
//前面补0为了让月份与数组下标对应。
int aim[33];
int main()
{
    cin>>n;
    for(int i=1; i<=n; i++)
        cin>>aim[i];
        //aim数组用来存储输入的月份天数
    int flag=0;
    int f=0;//两个标记
    for(int i=1; i<=12; i++)
    {
        //因为输入的月份不知道是从几月开始,那么就得从十二个月进行遍历。
        int len=1;//因为输入的月份是连续的所以从一开始进行累加遍历 。进行连续遍历到n为止。
        while(len<=n)
        {
            int to=i+len-1;//to是用来存储
            while(to>=13) to-=12;//如果超过了13进行换年份的调整
            if(to==2)
            {
                if(aim[len]==29&&f==0)
                {
                    f=1;
                    len++;
                    continue;
                }
                if(aim[len]==28)
                {
                    len++;
                    continue;
                }
                //这一步关键是防止连续两年是29 
                //加了这个特殊判定就能保证不会两年闰年的情况
            }
            if(aim[len]==m[to])
            {
                len++;
                continue;
            }
            break;
        }
        if(len==n+1)
        {
            flag=1;
            break;
        }
    }
    if(flag) printf("YES\n");
    else printf("NO\n");
    return 0 ;
}



一个规则的实心十二面体,它的 20个顶点标出世界著名的20个城市,你从一个城市出发经过每个城市刚好一次后回到出发的城市。 
Input前20行的第i行有3个数,表示与第i个城市相邻的3个城市.第20行以后每行有1个数m,m<=20,m>=1.m=0退出. 
Output输出从第m个城市出发经过每个城市1次又回到m的所有路线,如有多条路线,按字典序输出,每行1条路线.每行首先输出是第几条路线.然后个一个: 后列出经过的城市.参看Sample output 
Sample Input
2 5 20
1 3 12
2 4 10
3 5 8
1 4 6
5 7 19
6 8 17
4 7 9
8 10 16
3 9 11
10 12 15
2 11 13
12 14 20
13 15 18
11 14 16
9 15 17
7 16 18
14 17 19
6 18 20
1 13 19
5
0
Sample Output
1:  5 1 2 3 4 8 7 17 18 14 15 16 9 10 11 12 13 20 19 6 5
2:  5 1 2 3 4 8 9 10 11 12 13 20 19 18 14 15 16 17 7 6 5
3:  5 1 2 3 10 9 16 17 18 14 15 11 12 13 20 19 6 7 8 4 5
4:  5 1 2 3 10 11 12 13 20 19 6 7 17 18 14 15 16 9 8 4 5
5:  5 1 2 12 11 10 3 4 8 9 16 15 14 13 20 19 18 17 7 6 5
6:  5 1 2 12 11 15 14 13 20 19 18 17 16 9 10 3 4 8 7 6 5
7:  5 1 2 12 11 15 16 9 10 3 4 8 7 17 18 14 13 20 19 6 5
8:  5 1 2 12 11 15 16 17 18 14 13 20 19 6 7 8 9 10 3 4 5
9:  5 1 2 12 13 20 19 6 7 8 9 16 17 18 14 15 11 10 3 4 5
10:  5 1 2 12 13 20 19 18 14 15 11 10 3 4 8 9 16 17 7 6 5
11:  5 1 20 13 12 2 3 4 8 7 17 16 9 10 11 15 14 18 19 6 5
12:  5 1 20 13 12 2 3 10 11 15 14 18 19 6 7 17 16 9 8 4 5
13:  5 1 20 13 14 15 11 12 2 3 10 9 16 17 18 19 6 7 8 4 5
14:  5 1 20 13 14 15 16 9 10 11 12 2 3 4 8 7 17 18 19 6 5
15:  5 1 20 13 14 15 16 17 18 19 6 7 8 9 10 11 12 2 3 4 5
16:  5 1 20 13 14 18 19 6 7 17 16 15 11 12 2 3 10 9 8 4 5
17:  5 1 20 19 6 7 8 9 10 11 15 16 17 18 14 13 12 2 3 4 5
18:  5 1 20 19 6 7 17 18 14 13 12 2 3 10 11 15 16 9 8 4 5
19:  5 1 20 19 18 14 13 12 2 3 4 8 9 10 11 15 16 17 7 6 5
20:  5 1 20 19 18 17 16 9 10 11 15 14 13 12 2 3 4 8 7 6 5
21:  5 4 3 2 1 20 13 12 11 10 9 8 7 17 16 15 14 18 19 6 5
22:  5 4 3 2 1 20 19 18 17 16 15 14 13 12 11 10 9 8 7 6 5
23:  5 4 3 2 12 11 10 9 8 7 6 19 18 17 16 15 14 13 20 1 5
24:  5 4 3 2 12 13 14 18 17 16 15 11 10 9 8 7 6 19 20 1 5
25:  5 4 3 10 9 8 7 6 19 20 13 14 18 17 16 15 11 12 2 1 5
26:  5 4 3 10 9 8 7 17 16 15 11 12 2 1 20 13 14 18 19 6 5
27:  5 4 3 10 11 12 2 1 20 13 14 15 16 9 8 7 17 18 19 6 5
28:  5 4 3 10 11 15 14 13 12 2 1 20 19 18 17 16 9 8 7 6 5
29:  5 4 3 10 11 15 14 18 17 16 9 8 7 6 19 20 13 12 2 1 5
30:  5 4 3 10 11 15 16 9 8 7 17 18 14 13 12 2 1 20 19 6 5
31:  5 4 8 7 6 19 18 17 16 9 10 3 2 12 11 15 14 13 20 1 5
32:  5 4 8 7 6 19 20 13 12 11 15 14 18 17 16 9 10 3 2 1 5
33:  5 4 8 7 17 16 9 10 3 2 1 20 13 12 11 15 14 18 19 6 5
34:  5 4 8 7 17 18 14 13 12 11 15 16 9 10 3 2 1 20 19 6 5
35:  5 4 8 9 10 3 2 1 20 19 18 14 13 12 11 15 16 17 7 6 5
36:  5 4 8 9 10 3 2 12 11 15 16 17 7 6 19 18 14 13 20 1 5
37:  5 4 8 9 16 15 11 10 3 2 12 13 14 18 17 7 6 19 20 1 5
38:  5 4 8 9 16 15 14 13 12 11 10 3 2 1 20 19 18 17 7 6 5
39:  5 4 8 9 16 15 14 18 17 7 6 19 20 13 12 11 10 3 2 1 5
40:  5 4 8 9 16 17 7 6 19 18 14 15 11 10 3 2 12 13 20 1 5
41:  5 6 7 8 4 3 2 12 13 14 15 11 10 9 16 17 18 19 20 1 5
42:  5 6 7 8 4 3 10 9 16 17 18 19 20 13 14 15 11 12 2 1 5
43:  5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 1 2 3 4 5
44:  5 6 7 8 9 16 17 18 19 20 1 2 12 13 14 15 11 10 3 4 5
45:  5 6 7 17 16 9 8 4 3 10 11 15 14 18 19 20 13 12 2 1 5
46:  5 6 7 17 16 15 11 10 9 8 4 3 2 12 13 14 18 19 20 1 5
47:  5 6 7 17 16 15 11 12 13 14 18 19 20 1 2 3 10 9 8 4 5
48:  5 6 7 17 16 15 14 18 19 20 13 12 11 10 9 8 4 3 2 1 5
49:  5 6 7 17 18 19 20 1 2 3 10 11 12 13 14 15 16 9 8 4 5
50:  5 6 7 17 18 19 20 13 14 15 16 9 8 4 3 10 11 12 2 1 5
51:  5 6 19 18 14 13 20 1 2 12 11 15 16 17 7 8 9 10 3 4 5
52:  5 6 19 18 14 15 11 10 9 16 17 7 8 4 3 2 12 13 20 1 5
53:  5 6 19 18 14 15 11 12 13 20 1 2 3 10 9 16 17 7 8 4 5
54:  5 6 19 18 14 15 16 17 7 8 9 10 11 12 13 20 1 2 3 4 5
55:  5 6 19 18 17 7 8 4 3 2 12 11 10 9 16 15 14 13 20 1 5
56:  5 6 19 18 17 7 8 9 16 15 14 13 20 1 2 12 11 10 3 4 5
57:  5 6 19 20 1 2 3 10 9 16 15 11 12 13 14 18 17 7 8 4 5
58:  5 6 19 20 1 2 12 13 14 18 17 7 8 9 16 15 11 10 3 4 5
59:  5 6 19 20 13 12 11 10 9 16 15 14 18 17 7 8 4 3 2 1 5
60:  5 6 19 20 13 14 18 17 7 8 4 3 10 9 16 15 11 12 2 1 5
#include <iostream>
#include <stdio.h>
#include <cstring>
using namespace std;
int Path[22];
int Map[22][3];
int vis[22];
int T;
int m;


void dfs(int num, int step)
{
    if(num == m && step != 0 && step < 20)
        return;
    if(num == m && step == 20)
    {
        printf("%d:  %d", T ++, m);


        for(int i = 0; i < 20; i ++)
            printf(" %d", Path[i]);


        puts("");
    }
    for(int i = 0; i < 3; i ++)
        if(!vis[Map[num][i]])
        {
            vis[Map[num][i]] = true;
            Path[step] = Map[num][i];
            dfs(Map[num][i], step + 1);
            vis[Map[num][i]] = false;
        }
}


int main(void)
{
    for(int i = 1; i <= 20; i ++)
        cin >> Map[i][0] >> Map[i][1] >> Map[i][2];
    while(scanf("%d", &m) == 1)
    {
        memset(vis, 0, sizeof(vis));
        T = 1;
        dfs(m, 0);
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/beposit/article/details/79261532
2.5