Climbing Worm 、Binary Numbers、Guessing Game、Number Steps

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

Climbing Worm

An inch worm is at the bottom of a well n inches deep. It has enough energy to climb u inches every minute, but then has to rest a minute before climbing again. During the rest, it slips down d inches. The process of climbing and resting then repeats. How long before the worm climbs out of the well? We’ll always count a portion of a minute as a whole minute and if the worm just reaches the top of the well at the end of its climbing, we’ll assume the worm makes it out.

Input

There will be multiple problem instances. Each line will contain 3 positive integers n, u and d. These give the values mentioned in the paragraph above. Furthermore, you may assume d < u and n < 100. A value of n = 0 indicates end of output.

Output

Each input instance should generate a single integer on a line, indicating the number of minutes it takes for the worm to climb out of the well.

Sample Input
10 2 1
20 3 1
0 0 0

Sample Output
17
19

#include<iostream>
using namespace std;
int main()
{
	int n,u,d;//n是洞的高度,每分钟爬u,休息时每分钟下降d
	while(cin>>n>>u>>d&&n!=0&&u!=0&&d!=0)
	{
		int min=0,num=0;//分钟数,现在爬的高度
		while(num<n)
		{
			num=num+u;
			min++;
			if(num>=n)
			{
				cout<<min<<endl;
				break;
			}
			else
			{
				num=num-d;
				min++;
			}
			
		} 
	}
	
}

Binary Numbers

Given a positive integer n, find the positions of all 1’s in its binary representation. The position of the least significant bit is 0.
Example
The positions of 1’s in the binary representation of 13 are 0, 2, 3.
Task
Write a program which for each data set:
reads a positive integer n,
computes the positions of 1’s in the binary representation of n,
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, should contain increasing sequence of integers separated by single spaces - the positions of 1’s in the binary representation of the i-th input number.
Do not output any spaces in the end of a line.
Sample Input

1
13
Sample Output

0 2 3
注意:输出格式除了最后一个字符,前边每一个后边都是有空格的,最后一个没有直接是回车

#include<iostream>
using namespace std;
int main()
{
	int num,n;
	cin>>num;//个数 	
	while(num--)
	{
		cin>>n;
		int pos=0;
		int r,p=0;
		while(n)
		{
			r=n&1;//按位与
			if(r==1)
			{
				p++;
				if(p==1)
				   cout<<pos;
				else
				   cout<<' '<<pos;
			}
			pos++;
			n>>=1;				
		} 
		cout<<endl;
	} 
	return 0;
} 

Guessing Game

Stan and Ollie are playing a guessing game. Stan thinks of a number between 1 and 10 and Ollie guesses what the number might be. After each guess, Stan indicates whether Ollie’s guess is too high, too low, or right on.
After playing several rounds, Ollie has become suspicious that Stan cheats; that is, that he changes the number between Ollie’s guesses. To prepare his case against Stan, Ollie has recorded a transcript of several games. You are to determine whether or not each transcript proves that Stan is cheating.
Standard input consists of several transcripts. Each transcript consists of a number of paired guesses and responses. A guess is a line containing single integer between 1 and 10, and a response is a line containing “too high”, “too low”, or “right on”. Each game ends with “right on”. A line containing 0 follows the last transcript.
For each game, output a line “Stan is dishonest” if Stan’s responses are inconsistent with the final guess and response. Otherwise, print “Stan may be honest”.

Sample Input
10
too high
3
too low
4
too high
2
right on
5
too low
7
too high
6
right on
0
Sample Output
Stan is dishonest
Stan may be honest

#include<iostream>
#include<cstring>
using namespace std;
int main()
{
	int n;
	char str[10];
	while(1)
	{
		int max=11;
		int min=0;
		cin>>n;//接收猜的数字
		if(n==0)//结束游戏
			break;
		getchar();//接收换行符 
		gets(str);
		while(1)
		{
			if(strcmp(str,"right on")==0)
				break;//进行下一轮
			if(strcmp(str,"too high")==0&&n<max)
				max=n;
			if(strcmp(str,"too low")==0&&n>min)
				min=n;
			cin>>n;
			getchar();
			gets(str);
		}
		if(n>min&&n<max)
			cout<<"Stan may be honest"<<endl;
		else
			cout<<"Stan is dishonest"<<endl;
	}
	return 0;
}

Number Steps

Starting from point (0,0) on a plane, we have written all non-negative integers 0, 1, 2,… as shown in the figure. For example, 1, 2, and 3 has been written at points (1,1), (2,0), and (3, 1) respectively and this pattern has continued.
在这里插入图片描述
You are to write a program that reads the coordinates of a point (x, y), and writes the number (if any) that has been written at that point. (x, y) coordinates in the input are in the range 0…5000.

Input

The first line of the input is N, the number of test cases for this problem. In each of the N following lines, there is x, and y representing the coordinates (x, y) of a point.

Output

For each point in the input, write the number written at that point or write No Number if there is none.

Sample Input
3
4 2
6 6
3 4

Sample Output
6
12
No Number

这题主要是找规律

#include <iostream>
using namespace std;
int main ()
{
	int N;
	cin>>N;//个数
	while(N--)
	{
		int x,y;
		cin>>x>>y;
		int num;
		if(!(y==x||y==x-2))
			cout<<"No Number"<<endl;
		else
		{
			if(x%2==0)
				num=x+y;
			else
				num=x+y-1;
			cout<<num<<endl;
		}		
	}
}

猜你喜欢

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