杭电ACM Steps中Chapter One ——Section Two 所有的ac代码及解析

还有两题是很简单的,也是我直接在submit中写的,所以代码就不贴了,希望可以帮助到大家。
 

Biker's Trip Odometer
 
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)  
Total Submission(s): 3110 Accepted Submission(s): 1348  

 

Problem Description

Most bicycle speedometers work by using a Hall Effect sensor fastened to the front fork of the bicycle. A magnet is attached to one of the spokes on the front wheel so that it will line up with the Hall Effect switch once per revolution of the wheel. The speedometer monitors the sensor to count wheel revolutions. If the diameter of the wheel is known, the distance traveled can be easily be calculated if you know how many revolutions the wheel has made. In addition, if the time it takes to complete the revolutions is known, the average speed can also be calculated. 
For this problem, you will write a program to determine the total distance traveled (in miles) and the average speed (in Miles Per Hour) given the wheel diameter, the number of revolutions and the total time of the trip. You can assume that the front wheel never leaves the ground, and there is no slipping or skidding.


 

 

Input

Input consists of multiple datasets, one per line, of the form:

diameter revolutions time

The diameter is expressed in inches as a floating point value. The revolutions is an integer value. The time is expressed in seconds as a floating point value. Input ends when the value of revolutions is 0 (zero).


 

 

Output

For each data set, print:

Trip #N: distance MPH

Of course N should be replaced by the data set number, distance by the total distance in miles (accurate to 2 decimal places) and MPH by the speed in miles per hour (accurate to 2 decimal places). Your program should not generate any output for the ending case when revolutions is 0.

Constants

For p use the value: 3.1415927.
There are 5280 feet in a mile.
There are 12 inches in a foot.
There are 60 minutes in an hour.
There are 60 seconds in a minute.
There are 201.168 meters in a furlong.


 

 

Sample Input

26 1000 5
27.25 873234 3000
26 0 1000

 

 

Sample Output

Trip #1: 1.29 928.20
Trip #2: 1179.86 1415.84

 

 
 

Source

Greater New York 2003

  
//
//#include<iostream>
//#include<cstdio>
//#define p 3.1415927
//using namespace std;
//int main(){
//double di=0.00;
//int  re=0;
//double se=0;
//int i=0;
//while((scanf("%lf%d%lf",&di,&re,&se))&&(re!=0)){
//	i++;
//	double L=(di*p*re)/63360;
//	double speed=(L*3600/se);
//	printf("Trip #%d: %.2lf %.2lf\n",i,L,speed);
//}
//return 0;
//}

hide handkerchief 
Time Limit: 10000/3000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)  
Total Submission(s): 3925 Accepted Submission(s): 1317  

 
Problem Description
The Children’s Day has passed for some days .Has you remembered something happened at your childhood? I remembered I often played a game called hide handkerchief with my friends.
Now I introduce the game to you. Suppose there are N people played the game ,who sit on the ground forming a circle ,everyone owns a box behind them .Also there is a beautiful handkerchief hid in a box which is one of the boxes .
Then Haha(a friend of mine) is called to find the handkerchief. But he has a strange habit. Each time he will search the next box which is separated by M-1 boxes from the current box. For example, there are three boxes named A,B,C, and now Haha is at place of A. now he decide the M if equal to 2, so he will search A first, then he will search the C box, for C is separated by 2-1 = 1 box B from the current box A . Then he will search the box B ,then he will search the box A.
So after three times he establishes that he can find the beautiful handkerchief. Now I will give you N and M, can you tell me that Haha is able to find the handkerchief or not. If he can, you should tell me "YES", else tell me "POOR Haha".
 
 
Input
There will be several test cases; each case input contains two integers N and M, which satisfy the relationship: 1<=M<=100000000 and 3<=N<=100000000. When N=-1 and M=-1 means the end of input case, and you should not process the data.
 
 
Output
For each input case, you should only the result that Haha can find the handkerchief or not.
 
 
Sample Input
3 2
-1 -1 
 
Sample Output
YES 
题意分析,首先很好懂就是M可去遍历N,haha可以无限次的走下去,那个Three times是多次的意思
这就是看能不能把N遍历一遍,如果可以遍历一遍就证明可以找到,如果不能遍历一遍就不能找到
查看给出的N,M是否能够达到遍历一遍N,可以想到这两个数是否互为质数,不用在意谁大谁小
因为可想,只要N,M之前存在不是1的公约数就会有背书存在,产生有遍历不到的情况。可以举例如9和3.大家写一下就知道了。
另外为什么求最大公约数的方法用辗转相除法主要是(n,m)的公约数等于(m,n%m)的公约数,证明可以“查找欧几里德算法”
//#include<stdio.h>
两种辗转相除方法
int gcd(int n,int m)
{
if(n%m)
{
 return gcd(m,n%m);
}
else
return m;
}
//int gcd(int n,int m)
//{
//	int r;
//while(m)
//{
//	r=n%m;
//    n=m;
//    m=r;
//}
//return n;
//}
//int main(){
//	int n,m;
//	while(scanf("%d%d",&n,&m)&&(m!=-1)||(n!=-1)&&(1<=m)&&(m<=100000000)&&(1<=n)&&(n<=100000000)){
//	if(gcd(n,m)==1)
//		printf("YES\n");
//	else
//	printf("POOR Haha\n");
//	}
//	return 0;
//}


Rightmost Digit
 
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)  
Total Submission(s): 5051 Accepted Submission(s): 1319  

 

Problem Description

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次方结果的个位数是什么。
算法,只要最后一个数乘n次就可以了。
//#include<stdio.h>
//int getlast(int n)
//{
//	return n%10;
//}
//int main()
//{
//	int t;
//	double n;
//	scanf("%d",&t);
//	while(t--)
//	{
//		scanf("%lf",&n);
//		int a=getlast(n);
//		int b=getlast(n);
//		n-=1;
//		while(n--)
//		{
//			b=getlast(b)*a;
//		}
//		printf("%d\n",getlast(b));
//	}
//	return 0;
//}



//方法二://n缩小了一倍来运算,原因是你算一次b*b,
		//相当于偶数个n中每两个b*b都已经算过了,因为b会进入下一轮的运算
        //例如:3*3*3*3*3*3=9*9*9,因为下一轮的b变成了9了,但是偶数的时候要把他再乘以自身一下。  
//这一题n用long long直接wa了,好无语,不知道原因,long long输入输出都用lld
/*#include<stdio.h>
int main()
{
    int T;
    int  n;
    scanf("%d",&T);
while(T--)
    {
        scanf("%d",&n);
		int t=1;
		int b=n%10;
		while(n)
		{
			if(n%2==1)
			{
				t*=b;
				t%=10;
			}
			b*=b;
			b%=10;
			n/=2;
		}
		printf("%d\n",t);
    }   
    return 0; 
} */    

//方法三:这一题其实是有规律的
//看最后一位数字的次方
//1的所有次方都是1
//0的所有次方都是0
//5的所有次方都是5
//6的所有次方都是6
//2^1=2 2^2=4 2^3=8 2^4=6(四个一循环)
//3^1=3 3^2=9 3^3=7 3^4=1(四个一循环)
//7^1=7 7^2=9 7^3=3 7^4=1(四个一循环)
//4^1=4 4^2=6(两个一循环)
//8^1=8 8^2=4(两个一循环)
//9^1=9 9^2=1(两个一循环)
//故程序如下:
//#include<stdio.h>
//int main(void)
//{
//	int n;
//	int a[10][4]={{0},{1},{6,2,4,8},{1,3,9,7},{6,4},{5},{6},{1,7,9,3},{6,8,4,2},{1,9}};
//	int d,t;
//	scanf("%d",&t);
//	while(t--)
//	{   
//		scanf("%ld",&n);   
//		d=n%10;   
//		if(d==0||d==1||d==5||d==6)   
//			printf("%d\n",d);
//		else if(d==4||d==9)    
//			printf("%d\n",a[d][n%2]);  
//		else if(d==2||d==3||d==7||d==8)   
//			printf("%d\n",a[d][n%4]);
//	}
//	return 0;
//} 
//方法三:
//可以发现一个规律:
//当   n = 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 27 28 29 30 31 ...
//rdigit = 1 4 7 6 5 6 3 6 9 0  1   6  3  6  5  6  7  4  9  0  1  4  7  6  5  6  3  6 9   0    ...
//所以是以20为周期的规律。  
//#include<stdio.h> 
//int rdigit[25] = {0,1,4,7,6,5,6,3,6,9,0,1,6,3,6,5,6,7,4,9,0};
//int main ()
//{
//	int t , n ; 
//	scanf ( "%d" , &t ) ;
//	while ( t-- )
//	{
//		scanf ( "%d" , &n ) ;
//		printf ( "%d\n" , rdigit[n%20] ) ;
//	}
//	return 0 ;
//} 

//Buildings 
//Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)  
//Total Submission(s): 1814 Accepted Submission(s): 1309  
//
// 
//Problem Description
//We divide the HZNU Campus into N*M grids. As you can see from the picture below, the green grids represent the buidings. Given the size of the HZNU Campus, and the color of each grid, you should count how many green grids in the N*M grids.
//
//
//
// 
// 
//Input
//Standard input will contain multiple test cases. The first line of the input is a single integer T which is the number of test cases. T test cases follow. 
//The first line of each test case contains two integers n and m(1<=n,m<=100), the size of the campus. Then follow n lines, each line containing m integers. The j-th integer in the i-th line is the color of that grid, 0 stands for white color, while 1 stands for green.
// 
// 
//Output
//
//            Results should be directed to standard output. For each case, output an integers T, the total green grids in the N*M size campus.
// 
// 
//Sample Input
//2
//2 2
//1 1
//0 0
//3 3
//1 0 1
//0 0 1
//1 1 0 
// 
//Sample Output
//2
//5 
//解题思路:创建二维数组,遍历查找多少个1.
//#include<stdio.h>
//int main()
//{
//	int t,n,m;
//	int a[101][101];
//	int num=0;
//	scanf("%d",&t);
//while(t--)
//	{
//		scanf("%d%d",&n,&m);
//		for(int i=0;i<n;i++)
//		for(int j=0;j<m;j++)
//		{
//			scanf("%d",&a[i][j]);
//		}
//		
//		for(int i=0;i<n;i++)
//		for(int j=0;j<m;j++)
//		{
//         if(a[i][j]==1)
//            num++;
//		}
//		printf("%d\n",num);
//		num=0;
//	}
//	return 0;
//}

Higher Math 
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)  
Total Submission(s): 4072 Accepted Submission(s): 1329  

 
Problem Description
You are building a house. You’d prefer if all the walls have a precise right angle relative to the ground, but you have no device to measure angles. A friend says he has a great idea how you could ensure that all walls are upright: All you need to do is step away a few feet from the wall, measure how far away you are from the wall, measure the height of the wall, and the distance from the upper edge of the wall to where you stand. You friend tells you to do these measurements for all walls, then he’ll tell you how to proceed. Sadly, just as you are done, a timber falls on your friend, and an ambulance brings him to the hospital. This is too bad, because now you have to figure out what to do with your measurements yourself.

Given the three sides of a triangle, determine if the triangle is a right triangle, i.e. if one of the triangle’s angles is 90 degrees.

 
 
Input
The inputs start with a line containing a single integer n. Each of the n following lines contains one test case. Each test case consists of three integers 1 <= a, b, c <= 40000 separated by a space. The three integers are the lengths of the sides of a triangle.

 
 
Output
The output for every scenario begins with a line containing “Scenario #i:”, where i is the number of the scenario counting from 1. After that, output a single line containing either the string “yes” or the string “no”, depending on if the triangle in this test case has a right angle. Terminate each test case with an empty line.

 
 
Sample Input
2
36 77 85
40 55 69 
 
Sample Output
Scenario #1:
yes

Scenario #2:
no 
题意:计算三角形是不是正三角形,解题思路,a*a+b*b==c*c||a*a+c*c==b*b||b*b+c*c==a*a,并且最后一行有一个换行,要不然就PE了
//#include<stdio.h>
//int main()
//{
//	int n,a,b,c;
//	scanf("%d",&n);
//	int k=n;
//	while(k--)
//	{
//		scanf("%d%d%d",&a,&b,&c);
//		if(a*a+b*b==c*c||a*a+c*c==b*b||b*b+c*c==a*a)
//			printf("Scenario #%d:\nyes\n",n-k);
//		else
//			printf("Scenario #%d:\nno\n",n-k);
//		 printf("\n");
//
//	}
//}

//
//Specialized Four-Digit Numbers
// 
//Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)  
//Total Submission(s): 1559 Accepted Submission(s): 1046  
//
// 
//
//Problem Description
//
//Find and list all four-digit numbers in decimal notation that have the property that the sum of its four digits equals the sum of its digits when represented in hexadecimal (base 16) notation and also equals the sum of its digits when represented in duodecimal (base 12) notation.
//
//For example, the number 2991 has the sum of (decimal) digits 2+9+9+1 = 21. Since 2991 = 1*1728 + 8*144 + 9*12 + 3, its duodecimal representation is 1893(12), and these digits also sum up to 21. But in hexadecimal 2991 is BAF16, and 11+10+15 = 36, so 2991 should be rejected by your program.
//
//The next number (2992), however, has digits that sum to 22 in all three representations (including BB016), so 2992 should be on the listed output. (We don't want decimal numbers with fewer than four digits - excluding leading zeroes - so that 2992 is the first correct answer.)
//
//
// 
//
// 
//
//Input
//
//There is no input for this problem.
//
//
// 
//
// 
//
//Output
//
//Your output is to be 2992 and all larger four-digit numbers that satisfy the requirements (in strictly increasing order), each on a separate line with no leading or trailing blanks, ending with a new-line character. There are to be no blank lines in the output. The first few lines of the output are shown below.
//
//
// 
//
// 
//
//Sample Input
//
//There is no input for this problem.
//
// 
//
// 
//
//Sample Output
//
//2992
//2993
//2994
//2995
//2996
//2997
//2998
//2999
// 
//题意:大概题意是一个四位数,十进制它的每个数的和等于十二进制时每个数的和又等于16进制时每个数的和。求这样的四位数从2992开始
//算法:从2993开始遍历到10000,分别计算这个数在十二进制和十六进制中的数的和是否相等。
//关键在于十进制转化到任意进制,这里提供一个方法:一个十进制的数比如2992想要转化成K进制,会形如这样的表达式:2992=a*k^x1+b*k^x2+c*k^x3;
//所以这里需要这个数先对进制数取余,得到各位,再除以进制数,就把十位上的乘以k,这个k约掉了,同时个位上原来的数肯定是小于k的,除过之后由于是int,所以为0了,
//再对进制取余又得到这个十位数,
#include<iostream>
using namespace std;
int  TransAndGetSum(int n,int base) //定义实现转换的函数
{
int num[20],i=0,j,m=n,sum=0;;
while(m!=0)
 {
 i++;
 num[i]=m%base;
 m=m/base;
}
for(int j=i;j>=0;j--)
{
	sum+=num[j];
}
return sum;
}
int main()
{
	for(int i=2992;i<10000;i++)
	{
		if(TransAndGetSum(i,10)==TransAndGetSum(i,12)&&TransAndGetSum(i,10)==TransAndGetSum(i,16))
			printf("%d\n",i);
	}
return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值