哈尔滨学院夏令营day6:训练赛2

A - Rightmost Digit

Given a positive integer N, you should output the most right digit of N^N.
Input
The input contains several test cases. The first line of the input is a single integer T which is the number of test cases. T test cases follow.
Each test case contains a single positive integer N(1<=N<=1,000,000,000).
Output
For each test case, you should output the rightmost digit of N^N.
Sample Input
2
3
4
Sample Output
7
6
Hint
In the first case, 3 * 3 * 3 = 27, so the rightmost digit is 7.
In the second case, 4 * 4 * 4 * 4 = 256, so the rightmost digit is 6.
题意:求n的n次幂的最后一位
思路:快速幂,最后对10取模
AC代码

#include<cstdio>
#include<iostream>
using namespace std;
typedef long long ll;
ll pow(ll a,ll b)
{
    ll ans=1;
    while(b!=0)
    {
        if(b%2==1)
            ans=ans*a%10;
        a=a*a%10;
        b=b/2;
    }
    return ans;
}
int main()
{
    ll n,m,ans;
    cin>>n;
  while(n--)
        {
            scanf("%lld",&m);
            ans=pow(m,m)%10;
            printf("%lld\n",ans);
        }

    return 0;
}

B - Ignatius and the Princess II

Now our hero finds the door to the BEelzebub feng5166. He opens the door and finds feng5166 is about to kill our pretty Princess. But now the BEelzebub has to beat our hero first. feng5166 says, “I have three question for you, if you can work them out, I will release the Princess, or you will be my dinner, too.” Ignatius says confidently, “OK, at last, I will save the Princess.”

“Now I will show you the first problem.” feng5166 says, “Given a sequence of number 1 to N, we define that 1,2,3…N-1,N is the smallest sequence among all the sequence which can be composed with number 1 to N(each number can be and should be use only once in this problem). So it’s easy to see the second smallest sequence is 1,2,3…N,N-1. Now I will give you two numbers, N and M. You should tell me the Mth smallest sequence which is composed with number 1 to N. It’s easy, isn’t is? Hahahahaha…”
Can you help Ignatius to solve this problem?
Input
The input contains several test cases. Each test case consists of two numbers, N and M(1<=N<=1000, 1<=M<=10000). You may assume that there is always a sequence satisfied the BEelzebub’s demand. The input is terminated by the end of file.
Output
For each test case, you only have to output the sequence satisfied the BEelzebub’s demand. When output a sequence, you should print a space between two numbers, but do not output any spaces after the last number.
Sample Input
6 4
11 8
Sample Output
1 2 3 5 6 4
1 2 3 4 5 6 7 9 8 11 10
题意:求1到n数列中全排列第m大的序列
思路:利用next_permutation函数很好解决。
AC代码:

#include<cstdio>
#include<iostream>
#include<algorithm>
#include<stdlib.h>
#include<cstring>
using namespace std;
int a[10000];
int main()
{
    int n,m;
    while(~scanf("%d%d",&n,&m))
    {

        int l=0,k=0;
        for(int i=1; i<=n; i++)
        {
            a[k++]=i;
        }
        while(next_permutation(a,a+n))
        {
            l++;
            if(l==m-1)
            {
                for(int i=0; i<n-1; i++)
                {
                    printf("%d ",a[i]);
                }
                printf("%d\n",a[n-1]);
                break;
            }
        }


    }
    return 0;
}

C - Ignatius and the Princess III

“Well, it seems the first problem is too easy. I will let you know how foolish you are later.” feng5166 says.

“The second problem is, given an positive integer N, we define an equation like this:
N=a[1]+a[2]+a[3]+…+a[m];
a[i]>0,1<=m<=N;
My question is how many different equations you can find for a given N.
For example, assume N is 4, we can find:
4 = 4;
4 = 3 + 1;
4 = 2 + 2;
4 = 2 + 1 + 1;
4 = 1 + 1 + 1 + 1;
so the result is 5 when N is 4. Note that “4 = 3 + 1” and “4 = 1 + 3” is the same in this problem. Now, you do it!”
Input
The input contains several test cases. Each test case contains a positive integer N(1<=N<=120) which is mentioned above. The input is terminated by the end of file.
Output
For each test case, you have to output a line contains an integer P which indicate the different equations you have found.
Sample Input
4
10
20
Sample Output
5
42
627
题意:整数划分。求划分次数
思路:刚开始直接用了整数划分准备的函数,TEL了
此函数如下:

int splitint(int n,int m)
{
if(n= =1||m= =1)
   return 1;
if(n<m)
     return splitint(n,n);
else if(n==m)
    return (1+splitint(n,m-1));
else
   return(splitint(n-m,m)+splitint(n,m-1));
}

然后就不用这个方法提交,但用这个方法算出N的1到120所有答案,就是等的时间长点儿,然后初始化进数组中,想要哪个拿哪个。
AC代码:

#include<cstdio>
#include<iostream>
#include<algorithm>
#include<stdlib.h>
#include<cstring>
using namespace std;

int main()
{
int a[1000]={1,
2,
3,
5,
7,
11,
15,
22,
30,
42,
56,
77,
101,
135,
176,
231,
297,
385,
490,
627,
792,
1002,
1255,
1575,
1958,
2436,
3010,
3718,
4565,
5604,
6842,
8349,
10143,
12310,
14883,
17977,
21637,
26015,
31185,
37338,
44583,
53174,
63261,
75175,
89134,
105558,
124754,
147273,
173525,
204226,
239943,
281589,
329931,
386155,
451276,
526823,
614154,
715220,
831820,
966467,
1121505,
1300156,
1505499,
1741630,
2012558,
2323520,
2679689,
3087735,
3554345,
4087968,
4697205,
5392783,
6185689,
7089500,
8118264,
9289091,
10619863,
12132164,
13848650,
15796476,
18004327,
20506255,
23338469,
26543660,
30167357,
34262962,
38887673,
44108109,
49995925,
56634173,
64112359,
72533807,
82010177,
92669720,
104651419,
118114304,
133230930,
150198136,
169229875,
190569292,
214481126,
241265379,
271248950,
304801365,
342325709,
384276336,
431149389,
483502844,
541946240,
607163746,
679903203,
761002156,
851376628,
952050665,
1064144451,
1188908248,
1327710076,
1482074143,
1653668665,
1844349560};
  int n;
  while(~scanf("%d",&n))
  {
      printf("%d\n",a[n-1]);
  }
    return 0;
}

D - Misha and Changing Handles

Misha hacked the Codeforces site. Then he decided to let all the users change their handles. A user can now change his handle any number of times. But each new handle must not be equal to any handle that is already used or that was used at some point.

Misha has a list of handle change requests. After completing the requests he wants to understand the relation between the original and the new handles of the users. Help him to do that.

Input
The first line contains integer q (1 ≤ q ≤ 1000), the number of handle change requests.

Next q lines contain the descriptions of the requests, one per line.

Each query consists of two non-empty strings old and new, separated by a space. The strings consist of lowercase and uppercase Latin letters and digits. Strings old and new are distinct. The lengths of the strings do not exceed 20.

The requests are given chronologically. In other words, by the moment of a query there is a single person with handle old, and handle new is not used and has not been used by anyone.

Output
In the first line output the integer n — the number of users that changed their handles at least once.

In the next n lines print the mapping between the old and the new handles of the users. Each of them must contain two strings, old and new, separated by a space, meaning that before the user had handle old, and after all the requests are completed, his handle is new. You may output lines in any order.

Each user who changes the handle must occur exactly once in this description.

Examples
Input
5
Misha ILoveCodeforces
Vasya Petrov
Petrov VasyaPetrov123
ILoveCodeforces MikeMirzayanov
Petya Ivanov
Output
3
Petya Ivanov
Misha MikeMirzayanov
Vasya VasyaPetrov123
题意:n次修改操作,输出最初名字和最终名字。
思路:用map映射,看名字是否出现过。
总结:还剩十分钟的时候刚开始看这个题,由于在实战没用过map,不熟练,也就没出。
AC代码

#include<stdio.h>
#include<iostream>
#include<string>
#include<map>
#include<algorithm>
using namespace std;
int main(void)
{
    int q;
    map<string, string>mp;
    string olds, news;
    scanf("%d", &q);
    while (q--)
    {
        cin >> olds >> news;
        if (!mp.count(olds)) mp[news] = olds;
        else
        {
            mp[news] = mp[olds];
            mp.erase(olds);
        }
    }
    cout << mp.size() << endl;
    for (auto it = mp.begin(); it != mp.end(); it++)
        cout << it->second <<' '<<it->first<< endl;
    return 0;
}

E - Where is the Marble?

Raju and Meena love to play with Marbles. They have got a lot of
marbles with numbers written on them. At the beginning, Raju would
place the marbles one after another in ascending order of the numbers
written on them. Then Meena would ask Raju to find the first marble
with a certain number. She would count 1…2…3. Raju gets one point
for correct answer, and Meena gets the point if Raju fails. After some
fixed number of trials the game ends and the player with maximum
points wins. Today it’s your chance to play as Raju. Being the smart
kid, you’d be taking the favor of a computer. But don’t underestimate
Meena, she had written a program to keep track how much time you’re
taking to give all the answers. So now you have to write a program,
which will help you in your role as Raju.
Input
There can be multiple test cases. Total no of test cases is less than 65. Each test case consists begins
with 2 integers: N the number of marbles and Q the number of queries Mina would make. The next
N lines would contain the numbers written on the N marbles. These marble numbers will not come
in any particular order. Following Q lines will have Q queries. Be assured, none of the input numbers
are greater than 10000 and none of them are negative.
Input is terminated by a test case where N = 0 and Q = 0.
Output
For each test case output the serial number of the case.
For each of the queries, print one line of output. The format of this line will depend upon whether
or not the query number is written upon any of the marbles. The two different formats are described
below:
• ‘x found at y’, if the first marble with number x was found at position y. Positions are numbered
1, 2, . . . , N.
• ‘x not found’, if the marble with number x is not present.
Look at the output for sample input for details.
Sample Input
4 1
2
3
5
1
5
5 2
1
3
3
3
1
2
3
0 0
Sample Output
CASE# 1:
5 found at 4
CASE# 2:
2 not found
3 found at 3
题意:求升序情况下,一个数的位置。
思路:排序,二分查找。
代码:

#include <iostream>
#include <cstdio>
#include <algorithm>
using namespace std;
int a[10000],b[10000];
int main()
{
    int n,q;
    int kase = 1;
    while(cin>>n>>q)
    {
        if(n == 0 && q == 0)
            break;
        for(int i = 0; i < n; i++)
        {
            cin>>a[i];
        }
        for(int i = 0; i < q; i++)
        {
            cin>>b[i];
        }
        cout<<"CASE# "<<kase++<<":"<<endl;
        sort(a,a+n);
        for(int i = 0; i < q; i++)
        {
            int t = lower_bound(a,a+n,b[i]) - a;
            if(a[t] == b[i])
            {
                cout<<b[i]<<" found at "<<t+1<<endl;
            }
            else
            {
                cout<<b[i]<<" not found"<<endl;
            }
        }
    }
    return 0;
}

F - Andy’s First Dictionary

Andy, 8, has a dream - he wants to produce his
very own dictionary. This is not an easy task for
him, as the number of words that he knows is,
well, not quite enough. Instead of thinking up all
the words himself, he has a briliant idea. From
his bookshelf he would pick one of his favourite
story books, from which he would copy out all
the distinct words. By arranging the words in
alphabetical order, he is done! Of course, it is
a really time-consuming job, and this is where a
computer program is helpful.
You are asked to write a program that lists
all the different words in the input text. In this
problem, a word is defined as a consecutive sequence of alphabets, in upper and/or lower case.
Words with only one letter are also to be considered. Furthermore, your program must be CaSe InSeNsItIvE. For example, words like “Apple”, “apple” or “APPLE” must be considered the same.
Input
The input file is a text with no more than 5000 lines. An input line has at most 200 characters. Input
is terminated by EOF.
Output
Your output should give a list of different words that appears in the input text, one in a line. The
words should all be in lower case, sorted in alphabetical order. You can be sure that he number of
distinct words in the text does not exceed 5000.
Sample Input
Adventures in Disneyland
Two blondes were going to Disneyland when they came to a fork in the
road. The sign read: “Disneyland Left.”
So they went home.
Sample Output
a
adventures
blondes
came
disneyland
fork
going
home
in
left
read
road
sign
so
the
they
to
two
went
were
when
题意:输入文本,将输入但单词去重,排序输出。
思路:筛出字母,变成小写,利用set。

代码:

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<set>
using namespace std;
int main()
{
  string s;
  set<string> st;
  while(cin>>s)
  {
      string str;
      for(int i=0;i<s.length();i++)
      {
          if(s[i]>='a'&&s[i]<='z')
            str=str+s[i];
          else if(s[i]>='A'&&s[i]<='Z')
          {
              str+=s[i]-'A'+'a';
          }
          else if(!str.empty())
          {
              st.insert(str);
              str.clear();
          }
      }
      if(!str.empty())
        st.insert(str);
  }
  for(auto i=st.begin();i!=st.end();i++)
    cout<<*i<<endl;
    return 0;
}

G - Ananagrams

Most crossword puzzle fans are used to anagrams — groups of words with the same letters in different
orders — for example OPTS, SPOT, STOP, POTS and POST. Some words however do not have this
attribute, no matter how you rearrange their letters, you cannot form another word. Such words are
called ananagrams, an example is QUIZ.
Obviously such definitions depend on the domain within which we are working; you might think
that ATHENE is an ananagram, whereas any chemist would quickly produce ETHANE. One possible
domain would be the entire English language, but this could lead to some problems. One could restrict
the domain to, say, Music, in which case SCALE becomes a relative ananagram (LACES is not in the
same domain) but NOTE is not since it can produce TONE.
Write a program that will read in the dictionary of a restricted domain and determine the relative
ananagrams. Note that single letter words are, ipso facto, relative ananagrams since they cannot be
“rearranged” at all. The dictionary will contain no more than 1000 words.
Input
Input will consist of a series of lines. No line will be more than 80 characters long, but may contain any
number of words. Words consist of up to 20 upper and/or lower case letters, and will not be broken
across lines. Spaces may appear freely around words, and at least one space separates multiple words
on the same line. Note that words that contain the same letters but of differing case are considered to
be anagrams of each other, thus ‘tIeD’ and ‘EdiT’ are anagrams. The file will be terminated by a line
consisting of a single ‘#’.
Output
Output will consist of a series of lines. Each line will consist of a single word that is a relative ananagram
in the input dictionary. Words must be output in lexicographic (case-sensitive) order. There will always
be at least one relative ananagram.
Sample Input
ladder came tape soon leader acme RIDE lone Dreis peat
ScAlE orb eye Rides dealer NotE derail LaCeS drIed
noel dire Disk mace Rob dries

Sample Output
Disk
NotE
derail
drIed
eye
ladder
soon
题意:输入单词,判断哪个单次中的所有单词重新排列以后得不到此文本中的其他任何一个单次(大小写可互换,都算),就把此单词按原大小写按顺序输出。
思路:对于单个单词来说:如果两个单词重新排列成立,所有字母都变成小写,再重新排序,就会变成一样的单词。如果出现一次就成立,再进行排序,输出。用map,vector
代码:

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<set>
#include<map>
#include<vector>
using namespace std;
map<string,int> cnt;
vector<string> words;
string f(const string &s)
{
    string ans=s;
    for(int i=0; i<ans.length(); i++)
    {
        ans[i]=tolower(ans[i]);
    }
    sort(ans.begin(),ans.end());
    return ans;

}
int main()
{
    int n=0;
    string s;
    while(cin>>s)
    {
        if(s[0]=='#')
            break;
        words.push_back(s);
        string r=f(s);
        cnt[r]++;
    }
    vector<string>ans;
    for(int i=0; i<words.size(); i++)
    {
        if(cnt[f(words[i])]==1)
                ans.push_back(words[i]);
    }
        sort(ans.begin(),ans.end());
    for(int i=0; i<ans.size(); i++)
        cout<<ans[i]<<endl;

    return 0;
}

H - Team Queue

Queues and Priority Queues are data structures which are known to most computer scientists. The
Team Queue, however, is not so well known, though it occurs often in everyday life. At lunch time the
queue in front of the Mensa is a team queue, for example.
In a team queue each element belongs to a team. If an element enters the queue, it first searches
the queue from head to tail to check if some of its teammates (elements of the same team) are already
in the queue. If yes, it enters the queue right behind them. If not, it enters the queue at the tail
and becomes the new last element (bad luck). Dequeuing is done like in normal queues: elements are
processed from head to tail in the order they appear in the team queue.
Your task is to write a program that simulates such a team queue.
Input
The input file will contain one or more test cases. Each test case begins with the number of teams
t (1 ≤ t ≤ 1000). Then t team descriptions follow, each one consisting of the number of elements
belonging to the team and the elements themselves. Elements are integers in the range 0…999999. A
team may consist of up to 1000 elements.
Finally, a list of commands follows. There are three different kinds of commands:
• ENQUEUE x — enter element x into the team queue
• DEQUEUE — process the first element and remove it from the queue
• STOP — end of test case
The input will be terminated by a value of 0 for t.
Warning: A test case may contain up to 200000 (two hundred thousand) commands, so the implementation of the team queue should be efficient: both enqueing and dequeuing of an element should
only take constant time.
Output
For each test case, first print a line saying ‘Scenario #k’, where k is the number of the test case. Then,
for each ‘DEQUEUE’ command, print the element which is dequeued on a single line. Print a blank line
after each test case, even after the last one.
Sample Input
2
3 101 102 103
3 201 202 203
ENQUEUE 101
ENQUEUE 201
ENQUEUE 102
ENQUEUE 202
ENQUEUE 103
ENQUEUE 203
DEQUEUE
DEQUEUE
DEQUEUE
DEQUEUE
DEQUEUE
DEQUEUE
STOP
2
5 259001 259002 259003 259004 259005
6 260001 260002 260003 260004 260005 260006
ENQUEUE 259001
ENQUEUE 260001
ENQUEUE 259002
ENQUEUE 259003
ENQUEUE 259004
ENQUEUE 259005
DEQUEUE
DEQUEUE
ENQUEUE 260002
ENQUEUE 260003
DEQUEUE
DEQUEUE
DEQUEUE
DEQUEUE
STOP
0
Sample Output
Scenario #1
101
102
103
201
202
203
Scenario #2
259001
259002
259003
259004
259005
260001
题意:多个队伍在一起排队,每个不同团队人都去排,他会找在此队伍内有没有自己队友,乳沟有的话,他会排在自己团队的队友的最后一个位置,如果没有熟人,他就会排在队尾。
思路:模拟过程,用queue和map

#include<iostream>
#include<cstring>
#include<queue>
#include<map>
using namespace std;
int main()
{
    int i,n,m,t,k=1;
    bool a[1020];
    char b[100];
    while(~scanf("%d",&n))
    {
    	if(n==0)break;
        queue<int> q[1002],d;
		map<int,int> c;
        for(i=0;i<n;i++)
        {
            scanf("%d",&m);
            while(m--)
            {
                scanf("%d",&t);
                c[t]=i;
            }
        }
        memset(a,0,sizeof(a));
        printf("Scenario #%d\n",k++);
        while(scanf("%s",b))
        {
            if(strcmp(b,"STOP")==0)
            {
                puts("");
                break;
            }
            else if(strcmp(b,"ENQUEUE")==0)
            {
                scanf("%d",&t);
                q[c[t]].push(t);
                if(a[c[t]]==false)
                {
                    d.push(c[t]);
                    a[c[t]]=true;
                }
            }
            else
            {
                printf("%d\n",q[d.front()].front());
                q[d.front()].pop();
                if(q[d.front()].empty())
                {
                    a[d.front()]=false;
                    d.pop();
                }    
            }
        }
    }
    return 0;
}

I - Ugly Numbers

Ugly numbers are numbers whose only prime factors are 2, 3 or 5. The sequence
1, 2, 3, 4, 5, 6, 8, 9, 10, 12, 15, …
shows the first 11 ugly numbers. By convention, 1 is included.
Write a program to find and print the 1500’th ugly number.
Input
There is no input to this program.
Output
Output should consist of a single line as shown below, with ‘’ replaced by the number
computed.
Sample Output
The 1500’th ugly number is .
题意:丑数只能被2,3,5整除,输出第1500个丑数
思路:如果此数除完2,3,5后还剩1,则它就是丑数。
一开始是这样写的,但TEL了。
然后还是用慢程序跑出第1500个丑数,最后提交程序只要输出就好了。。。
代码:

#include<cstdio>
#include<iostream>
using namespace std;
typedef long long ll;
int main()
{

            cout<<"The 1500'th ugly number is 859963392."<<endl;



    return 0;
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值