A Simple Task、Ants、Ferry Loading IV、IBM Minus One、 Find the Diagonal

A Simple Task

Given a positive integer n, find the odd integer o and the non-negative integer p such that n=o2p.
Example
For n=24, o=3 and p=3.
Task
Write a program which for each data set:
reads a positive integer n,
computes the odd integer o and the non-negative integer p such that n=o2p,
writes the result.
Input
The first line of the input contains exactly one positive integer d equal to the number of data sets, 1 ≤ d ≤ 10. The data sets follow.
Each data set consists of exactly one line containing exactly one integer n, 1 ≤ n ≤ 106.
Output
The output should consists of exactly d lines, one line for each data set.
Line i, 1 ≤ i ≤ d, corresponds to the i-th input and should contain two integers o and p separated by a single space such that n=o2p.
Sample Input
1
24
Sample Output
3 3

#include<iostream>c
using namespace std;
int main()
{
	int num;
	cin>>num;//用例个数
	while(num--)
	{
		int n;
		cin>>n;
		int o=1;
		while(1)
		{
			if(n%o==0)
			{
				int chu=n/o;
				int p=0;
			
				while(chu%2==0)
				{	
					chu=chu/2;
					p++;
				}
				if(chu!=1)
					o=o+2;
				else
				{
					cout<<o<<" "<<p<<endl;
					break;
				}
					
			}
			else
				o=o+2;
		}
		
	}
		
}

Ants

An army of ants walk on a horizontal pole of length l cm, each with a constant speed of 1 cm/s. When a walking ant reaches an end of the pole, it immediatelly falls off it. When two ants meet they turn back and start walking in opposite directions. We know the original positions of ants on the pole, unfortunately, we do not know the directions in which the ants are walking. Your task is to compute the earliest and the latest possible times needed for all ants to fall off the pole.
The first line of input contains one integer giving the number of cases that follow. The data for each case start with two integer numbers: the length of the pole (in cm) and n, the number of ants residing on the pole. These two numbers are followed by n integers giving the position of each ant on the pole as the distance measured from the left end of the pole, in no particular order. All input integers are not bigger than 1000000 and they are separated by whitespace.
For each case of input, output two numbers separated by a single space. The first number is the earliest possible time when all ants fall off the pole (if the directions of their walks are chosen appropriately) and the second number is the latest possible such time.
Sample Input
2
10 3
2 6 7
214 7
11 12 7 13 176 23 191
Sample Output
4 8
38 207

解析:

最短的用时就是所有蚂蚁距离哪边近往哪边爬,所有的时间中最长的那个就是最短用时。
最长的用时就是所有蚂蚁距离哪边远往哪边爬,所有的时间中最长的那个就是最短用时。

#include<iostream>
#include<vector>
using namespace std;
int main()
{
	int num;
	cin>>num;//测试用例的个数
	while(num--)
	{
		int l,n;
		cin>>l>>n;//长度,数量
		vector<int> res;//存放各个蚂蚁在杆上的距离 
		vector<int> earliest;
		vector<int> latest;
		int tmp=n;
		while(n--)
		{
			int s;
			cin>>s;
			res.push_back(s);
		} 
		n=tmp;
		 for(int i=0;i<n;i++)
		 {
		 	if(res[i]<=(l/2))
		 	{
		 		earliest.push_back(res[i]);
		 		latest.push_back(l-res[i]);
			 }
			 else
			 {
			 	earliest.push_back(l-res[i]);
			 	latest.push_back(res[i]);
			 }
		  }
		  int maxear=earliest[0];
		  for(int i=1;i<n;i++)
		  {
		  	 if(earliest[i]>maxear)
		  	 	maxear=earliest[i];
		  }
		  int maxlae=latest[0];
		  for(int j=1;j<n;j++)
		  {
		  	if(latest[j]>maxlae)
		  		maxlae=latest[j];
		  }
		  cout<<maxear<<" "<<maxlae<<endl;
	}
}

Ferry Loading IV

Before bridges were common, ferries were used to transport cars across rivers. River ferries, unlike their larger cousins, run on a guide line and are powered by the river’s current. Cars drive onto the ferry from one end, the ferry crosses the river, and the cars exit from the other end of the ferry.
There is an l-meter-long ferry that crosses the river. A car may arrive at either river bank to be transported by the ferry to the opposite bank. The ferry travels continuously back and forth between the banks so long as it is carrying a car or there is at least one car waiting at either bank. Whenever the ferry arrives at one of the banks, it unloads its cargo and loads up cars that are waiting to cross as long as they fit on its deck. The cars are loaded in the order of their arrival; ferry’s deck accommodates only one lane of cars. The ferry is initially on the left bank where it broke and it took quite some time to fix it. In the meantime, lines of cars formed on both banks that await to cross the river.
The first line of input contains c, the number of test cases. Each test case begins with l, m. m lines follow describing the cars that arrive in this order to be transported. Each line gives the length of a car (in centimeters), and the bank at which the car arrives (“left” or “right”).
For each test case, output one line giving the number of times the ferry has to cross the river in order to serve all waiting cars.

Sample input
4
20 4
380 left
720 left
1340 right
1040 left
15 4
380 left
720 left
1340 right
1040 left
15 4
380 left
720 left
1340 left
1040 left
15 4
380 right
720 right
1340 right
1040 right
Output for sample input
3
3
5
6
注意:开始时渡轮是在left岸边的,记得换算单位。

#include<iostream>
#include<queue>
using namespace std;
int main()
{
	int num;
	cin>>num;//测试用例的数量
	while(num--)
	{
		int l,m;
		cin>>l>>m;//轮渡的长度,车的数量
		l=l*100;
		queue<int> left;
		queue<int> right;
		int count;//运输的次数 
		while(m--)
		{
			int len;//车的长度 
			string dir;//车等在哪个岸边 
			cin>>len>>dir;
			if(dir=="left")
				left.push(len); 
			else
				right.push(len);
		 } 
		 int leftcount=0,rightcount=0;//left和right要运几趟
		 int sum=0;
		 while(!left.empty())
		 {
		 	while(!left.empty()&& sum+left.front()<l)
		 	{
		 		sum=sum+left.front();
		 		left.pop();
			 }
			 leftcount++;
			 sum=0;
		 }
		 while(!right.empty())
		 {
		 	while(!right.empty()&& sum+right.front()<l)
		 	{
		 		sum=sum+right.front();
		 		right.pop();
			 }
			 rightcount++;
			 sum=0;
		 }
		 if(leftcount>rightcount)
		 	cout<<2*leftcount-1<<endl;
		else
			cout<<2*rightcount<<endl;
	 } 
}

IBM Minus One

You may have heard of the book ‘2001 - A Space Odyssey’ by Arthur C. Clarke, or the film of the same name by Stanley Kubrick. In it a spaceship is sent from Earth to Saturn. The crew is put into stasis for the long flight, only two men are awake, and the ship is controlled by the intelligent computer HAL. But during the flight HAL is acting more and more strangely, and even starts to kill the crew on board. We don’t tell you how the story ends, in case you want to read the book for yourself ?
After the movie was released and became very popular, there was some discussion as to what the name ‘HAL’ actually meant. Some thought that it might be an abbreviation for ‘Heuristic ALgorithm’. But the most popular explanation is the following: if you replace every letter in the word HAL by its successor in the alphabet, you get … IBM.
Perhaps there are even more acronyms related in this strange way! You are to write a program that may help to find this out.

Input
The input starts with the integer n on a line by itself - this is the number of strings to follow. The following n lines each contain one string of at most 50 upper-case letters.

Output
For each string in the input, first output the number of the string, as shown in the sample output. The print the string start is derived from the input string by replacing every time by the following letter in the alphabet, and replacing ‘Z’ by ‘A’.
Print a black line after each test case.

Sample Input
2
HAL
SWERC

Sample Output
String #1
IBM

String #2
TXFSD

#include<iostream>
using namespace std;
int main()
{
	int num;
	cin>>num;//测试用例的数量
	int p=1;
	while(num--)
	{
		string s;
		cin>>s;
		for(int i=0;i<s.length();i++)
		{
			if(s[i]=='Z')
				s[i]='A';
			else 
				s[i]=s[i]+1;
		}
		cout<<"String #"<<p<<endl;
		cout<<s<<endl;
		cout<<endl; 
		p++;
		
	} 
}

Find the Diagonal

A square matrix contains equal number of rows and columns. If the order of the matrix is known it can be calculated as in the following format:
Order: 3
在这里插入图片描述
Order: 5
在这里插入图片描述
and so on…

Now look at the diagonals of the matrices. In the second matrix - the elements of a diagonal are marked with circles but this is not the only one in this matrix but there are some other minor diagonals like <6, 12, 18, 24> as well as <2, 8, 14, 20> and many others.
Input
Each input line consists of two values. The first value is the order of the matrix and the later is an arbitrary element of that matrix. The maximum element of the matrix will fit as a 32-bit integer.
Output
Each output line will display the diagonal elements of the matrix containing the given input element.
Sample Input
4 5
5 11
10 81
Sample Output
5 10 15
11 17 23
81 92

#include<iostream>
using namespace std;
int main()
{
	int order,element;
	while(cin>>order>>element)
	{	
		int r=order+1;
		while(element%r!=1&&element>order)
		{
			element=element-r;
		}
		cout<<element;//对角线的第一个 
		while(element+r<order*order&&element%order!=0)
		{
			element=element+r;
			cout<<" "<<element; 
		}
		cout<<endl;
	}
	
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值