7091 Height Ordering

Mrs. Chambers always has her class line up in height order (shortest at the front of the line). EverySeptember a new class of exactly 20 3rd graders arrive, all of different height. For the first few days it takes a long time to get the kids in height order, since no one knows where they should be in the line.Needless to say, there is quite a bit of jockeying around. This year Mrs. Chambers decided to try a new method to minimize this ordering chaos. One student would be selected to be the first person in line.Then, another student is selected and would find the first person in the line that is taller than him, andstand in front of that person, thereby causing all the students behind him to step back to make room.If there is no student that is taller, then he would stand at the end of the line. This process continues,one student at-a-time, until all the students are in line, at which point the students will be lined up in height order.For this problem, you will write a program that calculates the total number of steps taken back during the ordering process for a given class of students.
Input
The first line of input contains a single integer P, (1 ≤ P ≤ 1000), which is the number of data sets that follow. Each data set should be processed identically and independently.
Each data set consists of a single line of input. It contains the data set number, K, followed by 20 non-negative unique integers separated by a single space. The 20 integers represent the height (in millimeters) of each student in the class.
Output
For each data set there is one line of output. The single output line consists of the data set number, K, followed by a single space followed by total number of steps taken back.
Sample Input
4
1 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919
2 919 918 917 916 915 914 913 912 911 910 909 908 907 906 905 904 903 902 901 900
3 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 900
4 918 917 916 915 914 913 912 911 910 909 908 907 906 905 904 903 902 901 900 919
Sample Output
1 0
2 190
3 19
4 171

题目意思:给20个学生按照身高由高到低排序,按照顺序依次将学生排到第一个比他高的学生前面,他腾出的空间将由后面的学生补上,问所需要移动的次数。

解题思路:这道题其实抽象过来就是依次求所有学生其后比其矮的学生的人数,因为比他矮的学生经过最后一定会排在他的前面。

#include<cstdio>
#include<cstring>
using namespace std;
int main()
{
int i,j,n,counts,ranks;
int a[];
scanf("%d",&n);
while(n--)
{
scanf("%d",&ranks);
counts=;
for(i=; i<; i++)
{
scanf("%d",&a[i]);
}
for(i=; i<; i++)
{
for(j=i+; j<; j++)
{
if(a[j]<a[i])
{
counts++;
}
}
}
printf("%d %d\n",ranks,counts);
}
return ;
}

7092 Islands in the Data Stream

Given a sequence of integers a1, a2, a3, …, an, an island in the sequence is a contiguous subsequence for which each element is greater than the elements immediately before and after the subsequence. In the examples below, each island in the sequence has a bracket below it. The bracket for an island contained within another island is below the bracket of the containing island. Write a program that takes as input a sequence of 12 non-negative integers and outputs the number of islands in the sequence.

Input

The first line of input contains a single integer P, (1 ≤ P ≤ 1000), which is the number of data sets that follow. Each data set should be processed identically and independently. Each data set consists of a single line of input. It contains the data set number, K, followed by 12 non-negative integers separated by a single space. The first and last integers in the sequence will be ‘0’.

Output

For each data set there is one line of output. The single output line consists of the data set number, K, followed by a single space followed by the number of islands in the sequence.

题目意思:给出12个数字,如果一个子序列的两个端点及以内的每一个元素都大于子序列端点两边前一个和后一个数字,那么这个子序列称之为岛,求出这个序列一共有多少个岛。
解题思路: 直接可以暴力,利用三个for循环,把每个子序列都遍历一遍,令i,j为两个端点,用k进行遍历。

#include<cstdio>
#include<cstring>
using namespace std;
int main()
{
int i,j,n,counts,ranks;
int a[];
scanf("%d",&n);
while(n--)
{
scanf("%d",&ranks);
counts=;
for(i=; i<; i++)
{
scanf("%d",&a[i]);
}
for(i=; i<; i++)
{
for(j=i+; j<; j++)
{
if(a[j]<a[i])
{
counts++;
}
}
}
printf("%d %d\n",ranks,counts);
}
return ;
}

7094 Happy Happy Prime Prime

RILEY VASHTEE: [reading from display] Find the next number in the sequence:
313 331 367 ...? What?
THE DOCTOR: 379.
MARTHA JONES: What?
THE DOCTOR: It’s a sequence of happy primes — 379.
MARTHA JONES: Happy what?
THE DOCTOR: Any number that reduces to one when you take the sum of the square of its digits and continue iterating it until it yields 1 is a happy number. Any number that doesn’t, isn’t. A happy prime is both happy and prime.
THE DOCTOR: I dunno, talk about dumbing down. Don’t they teach recreational mathematics anymore?
Excerpted from “Dr. Who”, Episode 42 (2007).
The number 7 is certainly prime. But is it happy?
7 → 7
2 = 49
49 → 4
2 + 92 = 97
97 → 9
2 + 72 = 130
130 → 1
2 + 32 = 10
10 → 1
2 + 02 = 1
It is happy :-). As it happens, 7 is the smallest happy prime. Please note that for the purposes of this problem, 1 is not prime.
For this problem you will write a program to determine if a number is a happy prime.
Input
The first line of input contains a single integer P, (1 ≤ P ≤ 1000), which is the number of data sets that follow. Each data set should be processed identically and independently.
Each data set consists of a single line of input. It contains the data set number, K, followed by the happy prime candidate, m, (1 ≤ m ≤ 10000).
Output
For each data set there is a single line of output. The single output line consists of the data set number, K, followed by a single space followed by the candidate, m, followed by a single space, followed by ‘YES’ or ‘NO’, indicating whether m is a happy prime.
Sample Input
4
1 1
2 7
3 383
4 1000
Sample Output
1 1 NO
2 7 YES
3 383 YES
4 1000 NO

题目意思:判断一个数是不是快乐素数,要求该数既是素数,并且该数经过有限次变化后的各位数平方之后和为1。

解题思路:直接模拟即可,注意去重判断。

#include<cstdio>
#include<cmath>
#include<cstring>
using namespace std;
int vis[];
int isprime(int n)
{
int i;
if(n==)
{
return ;
}
for(i=;i<=sqrt(n);i++)
{
if(n%i==)
{
return ;
}
}
return ;
}
int judge(int n)
{
int ans,i;
memset(vis,,sizeof(vis));
while(!vis[n]&&n!=)
{
ans=;
vis[n]=;
while(n>=)
{
ans=ans+(n%)*(n%);
n=n/;
}
if(ans!=)
{
n=ans;
}
else
{
return ;
}
}
return ;
}
int main()
{
int i,j,n,flag,no,m;
scanf("%d",&n);
while(n--)
{
scanf("%d%d",&no,&m);
printf("%d %d ",no,m);
if(isprime(m)&&judge(m))
{
printf("YES\n");
}
else
{
printf("NO\n");
}
}
return ;
}

7096 A Rational Sequence

An infinite full binary tree labeled by positive rational numbers is defined by:
• The label of the root is 1/1.
• The left child of label p/q is p/(p+q).
• The right child oflabel p/q is (p+q)/q.
The top of the tree is shown in the following figure:
A rational sequence is defined by doing a level order (breadth first) traversal of the tree (indicated by the light dashed line). So that:
F(1) = 1/1, F(2) = 1/2, F(3) = 2/1, F(4) = 1/3, F(5) = 3/2, F(6) = 2/3, . . .
Write a program which takes as input a rational number, p/q, in lowest terms and finds the next rational number in the sequence. That is, if F(n) = p/q, then the result is F(n + 1).

Input
The first line of input contains a single integer P, (1 ≤ P ≤ 1000), which is the number of data sets that follow. Each data set should be processed identically and independently.
Each data set consists of a single line of input. It contains the data set number, K, which is then followed by a space, then the numerator of the fraction, p, followed immediately by a fonward slash (/), followed immediately by the denominator of the fraction, q. Both p and q will be relatively prime and 0 ≤ p, q ≤ 2147483647.
Output
For each data set there is a single line of output. It contains the data set number, K, followed by a single space which is then followed by the numerator of the fraction, followed immediately by a forward slash (‘/’) followed immediately by the denominator of the fraction. Inputs will be chosen such that neither the numerator nor the denominator will overflow a 32-bit integer.

Sample Input
5
1 1/1
2 1/3
3 5/2
4 2178309/1346269
5 1/1OOOOOOO
Sample Output
1 1/2
2 3/2
3 2/5
4 1346269/1860498
5 1OOOOOOO/9999999

题目意思:每个结点有左右两个值(p/q),其左孩子是(p/(p+q)),右孩子是((p+q)/q),给出下一个结点的值。

解题思路:实际上这道题就是按照二叉树层次遍历求下一个结点,我们分成三种情况:

1如果是根结点,那么下一个结点必然是1/2。

2如果该结点是某个结点的左孩子,那么可以推得下一个结点。

3如果该结点是某个结点的右孩子,那么首先要向上找到一个父亲结点,再从这个父亲结点向下找。

#include<cstdio>
#include<cmath>
#include<cstring>
#define ll long long int
using namespace std;
int main()
{
ll a,b,t,n,p,q,counts;
scanf("%lld",&t);
while(t--)
{
scanf("%lld%lld/%lld",&n,&p,&q);
if(p==q)
{
a=;
b=;
}
else if(p<q)///如果是左孩子,能够直接得到右孩子
{
a=q;
b=q-p;
}
else if(p>q)
{
a=q;
counts=;///记录返回所需要的层数
/*while(1)
{
if(p<=q)
{
break;
}
p=p-q;
counts++;
}*/
///化减法为除法
counts=(p-p%q)/q;
b=q-p+counts*q;///向上回溯获得新的分母
b=b+counts*q;///向下获得新的分子
//b=q-p+2*counts*q;//化简后的式子
}
printf("%lld %lld/%lld\n",n,a,b);
}
return ;
}

Live Archive 训练题的更多相关文章

  1. Live Archive 训练题 2019/3/9

    7454 Parentheses A bracket is a punctuation mark, which is used in matched pairs, usually used withi ...

  2. X-NUCA 2017 web专题赛训练题 阳光总在风雨后和default wp

     0X0.前言 X-NUCA 2017来了,想起2016 web专题赛,题目都打不开,希望这次主办方能够搞好点吧!还没开赛,依照惯例会有赛前指导,放一些训练题让CTFer们好感受一下题目. 题目有一大 ...

  3. PAT乙级真题及训练题 1025. 反转链表 (25)

    PAT乙级真题及训练题 1025. 反转链表 (25) 感觉几个世纪没打代码了,真是坏习惯,调了两小时把反转链表调出来了,心情舒畅. 这道题的步骤 数据输入,数组纪录下一结点及储存值 创建链表并储存上 ...

  4. 日常 java+雅思+训练题1

    今天主要学了一些类似c中的一些语句,java也是一样类似的,只有一些点需要稍微注意一下,一些语句是新增的需要知道. 完完全全新学的知识就是class和instance的区别.如何创建实例.数据的封装. ...

  5. java基础集合经典训练题

    第一题:要求产生10个随机的字符串,每一个字符串互相不重复,每一个字符串中组成的字符(a-zA-Z0-9)也不相同,每个字符串长度为10; 分析:*1.看到这个题目,或许你脑海中会想到很多方法,比如判 ...

  6. 2016huas暑假集训训练题 G-Who's in the Middle

    题目链接:http://acm.hust.edu.cn/vjudge/contest/121192#problem/G 此题大意是给定一个数n 然后有n个数 要求求出其中位数  刚开始以为是按数学中的 ...

  7. 2016HUAS暑假集训训练题 G - Oil Deposits

    Description The GeoSurvComp geologic survey company is responsible for detecting underground oil dep ...

  8. c语言训练题:关于张三李四王五说谎的问题(此处用javascript实现)

    (第一篇博文) 今天在一个交流群里见他们无聊,然后找到之前收藏的一些c语言题目放出去想让他们做,结果反倒是自己不会做,于是花了很多时间去想. 原题:张三说李四在说谎,李四说王五在说谎,王五说张三和李四 ...

  9. 中南大学2018年ACM暑期集训前期训练题集(入门题) X: 又一道简单题

    简直智障,上一题V题,样例输出里面的“Case:”不要输出,到了这题又是要输出的了 #include<iostream> using namespace std; int num[1000 ...

随机推荐

  1. RHS 和 LHS

    不成功的的RHS 引用会导致抛出 ReferenceError异常 不成的的LHS 引用会导致自动隐式地创建一个全局变量(非严格模式下)   function foo(a) { var b = a; ...

  2. less的编译

    less其实也文本类型,跟txt的性质差不多 less有自己语法(变量,函数,作用域.Mixin混入),使css样式更加方便,有逻辑性,提高可维护性,减少重复性代码的冗余. 把less编译成css文件 ...

  3. 数据结构09—— 并查集(Union-Find)

    一.关于并查集 并查集(Union-Find)是一种树型的数据结构,常用于处理一些不相交集合(Disjoint Sets)的合并及查询问题.并查集(Union-Find)从名字可以看出,主要它涉及两种 ...

  4. 树莓派3B+简单入门

    刚刚入手一个树莓派3B+,树莓派板子.3.5寸电阻触摸屏.16G内存卡.外壳电源等一系列配件一共花了360大洋,这东西真不便宜.这里介绍一下系统安装.3.5寸屏幕安装.VNC远程屏幕. 先给大家看一下 ...

  5. PTA基础编程题目集6-2多项式求值(函数题)

    本题要求实现一个函数,计算阶数为n,系数为a[0] ... a[n]的多项式f(x)=∑​i=0​n​​(a[i]×x​i​​) 在x点的值. 函数接口定义: double f( int n, dou ...

  6. 1111: [POI2007]四进制的天平Wag

    1111: [POI2007]四进制的天平Wag 链接 题意: 用一些四进制数,相减得到给定的数,四进制数的数量应该尽量少,满足最少的条件下,求方案数. 分析: 这道题拖了好久啊. 参考Claris的 ...

  7. idea css文件不识别的问题的解决方案

    可以看到 progressBar不识别 发现仅仅是这一个文件名不识别 所以换一个任意别的文件名即可识别

  8. 一、EnterpriseFrameWork框架总体介绍

    EnterpriseFrameWork框架是自己在工作之余的得意之作,经过了几年时间的不断重构,现在终于有了现在的样子:刚开始只是为了方便开发WEB系统,随着项目越做越多,新的功能也就不断补充进去,补 ...

  9. PHP反序列化漏洞代码审计—学习资料

    1.什么是序列化 A.PHP网站的定义: 所有php里面的值都可以使用函数serialize()来返回一个包含字节流的字符串来表示.unserialize()函数能够重新把字符串变回php原来的值. ...

  10. Jmeter接口测试(四)传递参数

    参数设置 Jmeter 支持通过 查询字符串参数(Query String Parameters) 或者 Request body 请求体来传递参数. 1.get请求是普通键值对 get请求一般通过p ...