ACM简单计算题

ACM算法与程序设计

? Problem Description

2/42

? Sample input:

1 12 44
? Sample output:

4 132

3/42

? ? ? ? ? ? ? ? ? ? ? ? ? ? ?

#include <stdio.h> int gcd(int, int); int main() { int T; 有没有问题 int a,b,r,s; 呢？ scanf("%d",&T); while(T--) { scanf("%d %d",&a,&b); s=gcd(a,b); printf("%d %d\n",s,a*b/s); } return 0; }
4/42

? int gcd(int a, int b) ? { ? int r; ? if(a<b) ? { ? r=a; ? a=b; ? b=r; ? } ? do ? { ? r=a%b; ? a=b; ? b=r; ? }while(b!=0); ? return a; ? }

5/42

? int gcd(int x,int y) ? { ? while (x!=y) ? { ? if (x>y) x=x-y; ? else ? y=y-x; ? } ? return x; ? }
6/42

Fibonacci Again
http://acm.hdu.edu.cn/showproblem.php?pid=1021

7/42

? Problem Description There are another kind of Fibonacci numbers: F(0) = 7, F(1) = 11, F(n) = F(n-1) + F(n-2) (n>=2). ? Input Input consists of a sequence of lines, each containing an integer n. (n < 1,000,000). ? Output Print the word "yes" if 3 divide evenly into F(n). Print the word "no" if not.

8/42

? Sample input: 0123456 ? Sample output: no no yes no no no yes

9/42

? 能被3整除的整数的特点？ ? 如果两个数的和能被3整除，这两个数有什么特点？

? 关于能否被3整除，这两个数一共有多少种组合？

10/42

#include<stdio.h> int main() { long n; while(scanf("%ld",&n) == 1) if (n%8==2 || n%8==6) printf("yes\n"); else printf("no\n"); return 0; }

11/42

? http://poj.grids.cn/practice/2750

12/42

? Problem Description

? Input

? Output

13/42

? Sample input: 2 3 20 ? Sample output: 00 5 10

14/42

#include<stdio.h> int main() { int nCases,I,nFeet； //InCases表示输入测试数据的组 数，nFeet表示输入的脚数 scanf(“%d”, &nCases)； for(i=0; i<nCases; i++) { scanf((“%d”,& nFeet); if(nFeet % 2 !=0) //如果有奇数只脚，则没有满足题 意的答案，因为不论2只还是4只，都是偶数 printf(“0 0＼n”)； else if(nFeet % 4 !=0) //若要动物最少,使动物尽量有4只脚， //若要动物最多,使动物尽量有2只脚 printf(“%d %d\n”, nFeet / 4 + 1, nFeet / 2)； else printf(“%d %d\n”, nFeet / 4, nFeet / 2); } return 0; }
15/42

Elevator
http://acm.hdu.edu.cn/showproblem.php? pid=1008

16/42

17/42

? Problem Description The highest building in our city has only one elevator. A request list is made up with N positive numbers. The numbers denote at which floors the elevator will stop, in specified order. It costs 6 seconds to move the elevator up one floor, and 4 seconds to move down one floor. The elevator will stay for 5 seconds at each stop.
For a given request list, you are to compute the total time spent to fulfill the requests on the list. The elevator is on the 0th floor at the beginning and does not have to return to the ground floor when the requests are fulfilled. ? Input There are multiple test cases. Each case contains a positive integer N, followed by N positive numbers. All the numbers in the input are less than 100. A test case with N = 0 denotes the end of input. This test case is not to be processed. ? Output Print the total time on a single line for each test case.
18/42

? Sample input: 12 3231 0 ? Sample output: 17 41

19/42

#include <stdio.h> int main(void) { int n, i, a[101], value, sum; while(scanf("%d",&n) && n!=0) { a[0] = 0; for(i = 1; i <= n; i++) { scanf("%d",&value); a[i]=value; } sum = n * 5; for(i = 0; i < n; i++) { if(a[i]<a[i+1]) sum = sum + (a[i+1]-a[i])*6; else if(a[i]>a[i+1]) sum = sum + (a[i]-a[i+1])*4; } printf("%d\n", sum); } return 0; }
20/42

Rightmost Digit
? http://acm.hdu.edu.cn/showproblem.php?pid =1061

21/42

? 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.
22/42

? Sample input: 2 3 4 ? Sample output: 7 6

23/42

?数据规模? 很大 ?暴力方法? 该打 ?基本思路? 规律

24/42

#include<stdio.h> int main(void) { int T,i,a,digit; int n; scanf("%d",&T); for(i=0;i<T;i++) { scanf("%d",&n); a=n%10; if(a==0||a==1||a==5 ||a==6||a==9) digit=a; else if(a==2) { if(n%4==0) digit=6; else digit=4; }

else if(a==3) { if(n%4==1) digit=3; else digit=7; } else if(a==4) digit=6; else if(a==7) { if(n%4==1) digit=7; else digit=3; } else //a=8 { if(n%4==0) digit=6; else digit=4; } printf("%d\n",digit); } return 0;}
25/42

http://acm.uestc.edu.cn/problem.php?pid=1025

2、Description

26/42

3、Input 第一行包括3个整数，N,L,V,表示小路上的人数、小路的长 度、所有人前进的速率 (N<=100, L<= 1000000,V>0) 接下来有N行，每行3个数据，第i行的数据表示第i个人的 位置（从0到L的整数）、性别（M或F）、方向（W表示面 朝西、E表示面朝东） 当N=L=V=0时，输入结束 4、Output 对于每组输入，输出一行两个小数，表示最后一个人离开 的时间以及所有人在小路上走的路程的总和，用一个空格隔 开，答案四舍五入保留两位小数 。

27/42

5、Sample Input 242 1ME 3MW 000 6、Sample Output 1.50 6.00

7、Source royce
28/42

29/42

#include <stdio.h> int N, L, V, pos; char sex[4], dir[4]; int main() { double last, dist;//最后时间与路程 double len;//当前人的路程 while (scanf("%d %d %d", &N, &L, &V) != EOF) { if (N == 0 && L == 0 && V == 0) break; last = 0.0, dist = 0.0;//清零 for (int i = 0; i < N; i++) { scanf("%d %s %s", &pos, sex, dir); if (dir[0] == 'W') len = pos; else len = L - pos; if (len / V > last) last = len / V; dist += len; } printf("%.2f %.2f\n", last, dist); } return 0; }

30/42

http://acm.uestc.edu.cn/problem.php?pid=1033

2、Description

31/42

32/42

3、Input 含有多组测试数据，每组数据都包含一个正 整数n（n <= 1000）。 当n = 0的时候结束程序，证明silentky经受 住考验了的O(∩_∩)O~ 4、Output 对于每个n，输出被瓶盖边缘的水弄湿了的格 子数为多少。

33/42

5、Sample Input 1 2 0 6、Sample Output 4 12

7、Source love8909 & silentsky
34/42

35/42

#include<stdio.h>
int main()

{
int n; while(scanf("%d", &n)!=EOF&&n) printf("%d\n", n*8-4); return 0; }
36/42

The Story of ZCS
http://acm.uestc.edu.cn/problem.php?pid=1088

2、Description
Love and Eternal lay hid in night; God said "Let CS be" and all was light. Walking alone in darkness, lonely and never knew what I would go through. How many times I walked lonely in the campus, how many times I woke up with a start at midnight, and how many times I sang a sorrow song but no one cared about? The terrible feeling is just like a albatross around my neck until I met CS. My door is opened and the sky is lightened.It is a moment for my renaissance, which makes me understand the meaning of sunshine and rainbow once again. When I catch sight of you, you are the only one in my view. When you are out of sight, you are the only one in my heart. When I am awake, you are the only one in my mind. When I am asleep, you are the only one in my dream. You are my only one.I want to catch sight of you every minute and every second. -- Letter from Kamel (ZS) to CS
37/42

The Story of ZCS
Kamel has lots of photos of CS. He wants to fill his desktop with her photos. The desktop is made up of m rows and n columns. One photo occupies b consecutive squares in a row or b consecutive squares in a column. He want to know whether it is possible to cover his desktop with CS's photos while no two photos overlap with each other and the whole desktop is full?

You may assume that Kamel has tens of thousands of photos of CS. If it is possible then print "YES" else print "NO".

38/42

The Story of ZCS
3、Input The first line of the inputs is T(no more than 120), which stands for the number of test cases you need to solve. Each line has three numbers M,N,B (1 <= M,N <= 1000, 1 <= B <= M,N). 4、Output Each "YES" or "NO" occupies one line.

39/42

The Story of ZCS
5、Sample Input

2 484 573
6、Sample Output

YES NO
7、Source

kamel
40/42

The Story of ZCS

41/42

The Story of ZCS
#include<stdio.h> int main() { int T,M,N,B; scanf("%d",&T); while(T--) { scanf("%d%d%d",&M,&N,&B); if(M%B==0||N%B==0) printf("YES\n"); else printf("NO\n"); } return 0; }

42/42

ACM经典算法及配套练习题

ACM经典算法及配套练习题_计算机软件及应用_IT/计算机...(poj1837,poj1276) (2)型如下表的简单 DP(可...(poj2635, poj3292,poj1845,poj2115) (3)计算...

ACM百练习题分类汇总

ACM百练习题分类汇总_IT认证_资格考试/认证_教育专区。百练上的题,ACM入门用 ...计算表达式的值 字符串数组排序问题 简单密码 最短前缀 浮点数格式 词典 W ...

ACM算法题以及答案

ACM算法题以及答案_计算机软件及应用_IT/计算机_专业...看下面这个简单的示例: #include <iostream> #...(str); } } 幂指数计算 long Power(int index,...

ACM练习加答案(值得一看)

Description 对于任意给定的非负整数 n, 计算 2n-1...简单排列(2) answer #include <stdio.h> #define...ACM练习 10页 1下载券 ACM习题 1页 免费 ...

POJ推荐50题以及ACM训练方案

POJ推荐50题以及ACM训练方案_计算机软件及应用_IT/计算机_专业资料。ACMPOJ...少题数要求) 2352 (可用简单方法) 2528 第十二类 计算几何 (至少 2 题,...

acm计算几何基础题目

ACM课件!!(lecture_08)搜索... 55页 免费 计算几何复习题 (新) 5页 免费...这样其实就化为了一个简单的有向图, 这时要寻找最短的长板其实最小生成树的问题...

ACM必做50题的解题-计算几何

ACM 必做 50 题的解题-计算几何.txt 生活,是用来经营的,而不是用来计较的。...这样其实就化为了一个简单的有向图, 这时要寻找最短的长板其实最小生成树的问...

acm编程比赛入门题目集

,km-1,km 棵,现在有一个简单的任务, 就请你帮忙计算一下每个部门分别种了...acm编程比赛题 暂无评价 69页 2下载券 ACM入门 26页 2下载券 编程竞赛acm入门...