The Drunk Jailer、Speed Limit、Number Sequence、I Think I Need a Houseboat、Word Reversal

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/summer2day/article/details/85047302

The Drunk Jailer

Problem
A certain prison contains a long hall of n cells, each right next to each other. Each cell has a prisoner in it, and each cell is locked.
One night, the jailer gets bored and decides to play a game. For round 1 of the game, he takes a drink of whiskey, and then runs down the hall unlocking each cell. For round 2, he takes a drink of whiskey, and then runs down the hall locking every other cell (cells 2, 4, 6, …). For round 3, he takes a drink of whiskey, and then runs down the hall. He visits every third cell (cells 3, 6, 9, …). If the cell is locked, he unlocks it; if it is unlocked, he locks it. He repeats this for n rounds, takes a final drink, and passes out.
Some number of prisoners, possibly zero, realizes that their cells are unlocked and the jailer is incapacitated. They immediately escape.
Given the number of cells, determine how many prisoners escape jail.
Input
The first line of input contains a single positive integer. This is the number of lines that follow. Each of the following lines contains a single integer between 5 and 100, inclusive, which is the number of cells n.
Output
For each line, you must print out the number of prisoners that escape when the prison has n cells.
Sample Input
2
5
100
Sample Output
2
10

#include<iostream>
#include<vector>
using namespace std;
int main()
{
	int num;
	cin>>num;//输入数据的个数
	while(num--)
	{
		int n; 
		cin>>n;//牢房的数量
		int p=1;
		vector<int> cell(n+1);//默认初始化为0 ,关闭。牢房编号从1开始 
		for(int j=0;j<n;j++)
		{
			for(int i=1;i<cell.size();i++)
			{
				if(i%p==0)
					cell[i]=1-cell[i];
			}
			p++; 
		}
		int count=0;
		for(int i=1;i<cell.size();i++)
		{
			if(cell[i]==1)
				count++;
		}
		cout<<count<<endl;	
	} 
 } 

Speed Limit

Bill and Ted are taking a road trip. But the odometer in their car is broken, so they don’t know how many miles they have driven. Fortunately, Bill has a working stopwatch, so they can record their speed and the total time they have driven. Unfortunately, their record keeping strategy is a little odd, so they need help computing the total distance driven. You are to write a program to do this computation.
For example, if their log shows

Speed in miles per hour Total elapsed time in hours
20 2
30 6
10 7

this means they drove 2 hours at 20 miles per hour, then 6-2=4 hours at 30 miles per hour, then 7-6=1 hour at 10 miles per hour. The distance driven is then (2)(20) + (4)(30) + (1)(10) = 40 + 120 + 10 = 170 miles. Note that the total elapsed time is always since the beginning of the trip, not since the previous entry in their log.
Input: The input consists of one or more data sets. Each set starts with a line containing an integer n, 1 ≤ n ≤ 10, followed by n pairs of values, one pair per line. The first value in a pair, s, is the speed in miles per hour and the second value, t, is the total elapsed time. Both s and t are integers, 1 ≤ s ≤ 90 and 1 ≤ t ≤ 12. The values for t are always in strictly increasing order. A value of -1 for n signals the end of the input.
Output: For each input set, print the distance driven, followed by a space, followed by the word “miles”
在这里插入图片描述

#include<iostream>
#include<vector>
using namespace std;
int main()
{
	while(1)
	{
		int n;
		cin>>n;//输入的行数
		if(n==-1)
			break;
		int mile=0;
		vector<int> ss;
		vector<int> tt;
		while(n--)
		{
			int s,t;
			cin>>s;
			cin>>t;
			ss.push_back(s);
			tt.push_back(t);
			
		 } 
		 mile=mile+ss[0]*tt[0];
		 for(int i=1;i<ss.size();i++)
		 {
		 	mile=mile+ss[i]*(tt[i]-tt[i-1]);			 
		 }
		 cout<<mile<<" miles"<<endl;
	}

}

Number Sequence

Given a positive integer number, we want to generate a number sequence with the following rules:
If the current number is 1, the process will be terminated. Otherwise, if the current number is even, the next number will be n/2. If the current number is odd (except 1), the next number will be 3*n+1. Then we turn to deal with the new number, until we get 1.
For example, given the first number 22, we will get {22, 11, 34, 17, 52, 26, 13, 40, 20, 10, 5, 16, 8, 4, 2, 1}. The length of this sequence is 16.
Given the first number, your task is to find the length of the sequence generated with these rules. We assume that the length will not be more than 1000.
Input
There is only one number N (1 ≤ N ≤ 1000) for each test case, indicating the first number of the sequence.
The input is terminated by a zero.
Output
Output one number in a line for each test case, indicating the length of the sequence we generate.
Sample Input

22
1000
1
0

Sample Output

16
112
1

#include<iostream>
using namespace std;
int main()
{
	while(1)
	{
		int n;
		cin>>n;
		if(n==0)
			break;
		int count=1;
		while(n!=1)
		{
			if(n%2==0)
				n=n/2;
			else
				n=n*3+1;
			count++;
				
		}
		cout<<count<<endl;
		
		
	}
	
 } 

I Think I Need a Houseboat

Fred Mapper is considering purchasing some land in Louisiana to build his house on. In the process of investigating the land, he learned that the state of Louisiana is actually shrinking by 50 square miles each year, due to erosion caused by the Mississippi River. Since Fred is hoping to live in this house the rest of his life, he needs to know if his land is going to be lost to erosion.
在这里插入图片描述
After doing more research, Fred has learned that the land that is being lost forms a semicircle. This semicircle is part of a circle centered at (0,0), with the line that bisects the circle being the X axis. Locations below the X axis are in the water. The semicircle has an area of 0 at the beginning of year 1. (Semicircle illustrated in the Figure.)
Input Format

The first line of input will be a positive integer indicating how many data sets will be included (N).

Each of the next N lines will contain the X and Y Cartesian coordinates of the land Fred is considering. These will be floating point numbers measured in miles. The Y coordinate will be non-negative. (0,0) will not be given.

在这里插入图片描述
Output Format

For each data set, a single line of output should appear. This line should take the form of:

“Property N: This property will begin eroding in year Z.”

Where N is the data set (counting from 1), and Z is the first year (start from 1) this property will be within the semicircle AT THE END OF YEAR Z. Z must be an integer.

After the last data set, this should print out “END OF OUTPUT.”
Notes:
No property will appear exactly on the semicircle boundary: it will either be inside or outside.
This problem will be judged automatically. Your answer must match exactly, including the capitalization, punctuation, and white-space. This includes the periods at the ends of the lines.
All locations are given in miles.

Sample Input
2
1.0 1.0
25.0 0.0

Sample Output
Property 1: This property will begin eroding in year 1.
Property 2: This property will begin eroding in year 20.
END OF OUTPUT.

#include<iostream>
#include<math.h>
#define pi 3.1415926
using namespace std;
int main()
{
	int n;
	cin>>n;
	int num=1;
	while(n--)
	{
		float x,y;
		cin>>x>>y;
		float r=x*x+y*y;
		float area=0.5*pi*r;
		int count=(int)ceil(area/50);

		cout<<"Property "<<num<<": This property will begin eroding in year "<<count<<"."<<endl;
		num++;
	}
	cout<<"END OF OUTPUT."<<endl;
}

Word Reversal

For each list of words, output a line with each word reversed without changing the order of the words.

Input
You will be given a number of test cases. The first line contains a positive integer indicating the number of cases to follow. Each case is given on a line containing a list of words separated by one space, and each word contains only uppercase and lowercase letters.

Output
For each test case, print the output on one line.

Sample Input
3
I am happy today
To be or not to be
I want to win the practice contest

Sample Output
I ma yppah yadot
oT eb ro ton ot eb
I tnaw ot niw eht ecitcarp tsetnoc

#include<iostream>
#include<vector>
#include <algorithm>
#include<stdio.h> 
using namespace std;
int main()
{
	int n;
	cin>>n;
	while(n--)
	{
		vector<string> res;
		string s;

		while(cin>>s)
		{
			res.push_back(s);
			char ch=getchar();//接收空格和换行 
			if(ch=='\n')
				break;
			
		}
		for(int i=0;i<res.size();i++)
		{
			reverse(res[i].begin(),res[i].end());
		}
		for(int i=0;i<res.size()-1;i++)
		{
			
			cout<<res[i]<<" ";
		}
		cout<<res[res.size()-1]<<endl;
	}
}

猜你喜欢

转载自blog.csdn.net/summer2day/article/details/85047302