2021.1.11寒假打卡Day8

哈尔滨学院2021ACM冬令营Day2(20级)

The puzzle

Problem Description
Kayaking is playing a puzzle game containing n different blocks. He marks the blocks with integers from 1 to n, which show the blocks’ original positions. Each time he can exchange two blocks and he wants to know how many times he needs at least to restore the puzzle.

Input
The input starts with one line contains exactly one positive integer which is the number of test cases.
Each test case contains two lines.

The first line contains an integer, which indicates the number of puzzle pieces.

The second line contains n different integers, the i-th number means the mark of the block in the i-th position.

Output
For each test case, output one line with one number represents the minimum operations.

Sample Input

2
4
2 3 4 1
4
2 1 4 3

Sample Output

3
2

Hint
1 ≤ T ≤ 20
1 ≤ n ≤ 100000

selection sort

#include<iostream>
using namespace std;

int a[100005];

int main(){
	int t,n,cnt,i,j,minj;
	cin>>t;
	while(t--){
		cin>>n;
		cnt=0;
		for(i=0;i<n;i++) cin>>a[i];
		for(i=0;i<n;i++){
			minj=i;
			for(j=n-1;j>i;j--){
				if(a[j]<a[minj]) minj=j;
			}
			if(minj==i) continue;
			else{
				int s=a[j];
				a[j]=a[minj];
				a[minj]=s;
				++cnt;
			}
		}
		cout<<cnt<<endl;
	} 
	return 0;
}

Overflow

Problem Description
Kayaking is a naughty boy and he loves to play water. One day, Kayaking finds a bucket. The bottom area of the bucket is S and the height is H. Initially, there is V volume water in the bucket. What makes Kayaking happy is that there are N cube woods beside the bucket. The side length of the i-th cube woods is L[i] and its density is P[i]. Kayaking wants to put all the cube woods to the bucket. And then he will put a cover at the top of the bucket. But CoffeeDog doesn’t allow unless Kayaking can tell CoffeeDog the height of the water in the bucket after Kayaking put all the cuboid woods to the bucket. Could you help him?

It is guaranteed that the cube woods aren’t overlapping. And after putting the wood to the bucket, the bottom of the wood is parallel to the bottom of the bucket.

Input
The first line of the input contains a single integer T denoting the number of test cases. The description of T test cases follows.

The first line of each test case contains a single integer N denoting the number of cube woods.

Then comes N line. Each line has two real numbers L[i] and P[i].

The last line contains three integers S, H and V.

Output
For each test cases, print a single line containing one real number—the height of the water in the bucket(the number should be rounded to the second digit after the decimal point).

Sample Input

1
2
1 1.5
1 0.5
5 100 25

Sample Output

5.30

Hint

T ≤ 10
n ≤ 104,P ≤ 6,L ≤ 3,H ≤ 100,L ≤ H
V,S ≤ 107

还好他没有盖上盖子。。。
初中科学:ρgh = ρv → △h = ρL3 / ( ρg ) (ρ = 1 Kg/m3, g = 10 N/Kg)
ans = (V + Σ△hL2)/S
考虑水漫出来的情况 V / S + Σ△h > H,输出H
题目有点坑……存在比水密度大的木头

为啥我手算样例结果是5.20…

#include<iostream>
using namespace std;

int main() {
	int t,n;
	cin>>t;
	while(t--){
		double d,p,h,s,v,ans,dh=0;
		cin>>n;
		while(n--){
			cin>>d>>p;
			dh+=d*d*d*p/10;
		}
		cin>>s>>h>>v;
		ans=(v+dh*d*d)/s;
		if(ans>=h) printf("%.2lf\n",h);
		else printf("%.2lf\n",ans);
	}
	return 0;
}

Flower

Problem Description
Rabbit loves flowers very much and she plants n pots of flowers in her house. But she never prunes them because she is lazy. So the flowers have different heights and look ugly. One day, Kayaking decides to prune the flowers to make them look beautiful. To make them have equal heights, smart Kayaking has a wonderful idea. Initially, the i-th pot of flower’s height is a[i]. Each time, Kayaking will select n-1 pots of flowers and prune them to make their height subtract 1 with his 49m knife. Exactly, the height of the flowers is at least 1. Now, Kayaking wants to know if it is possible to prune the flowers to make them have equal height. If possible, what is the minimum times he prunes the flowers. Could you tell him?

Input
The input starts with a line contains exactly one positive number T which is the number of test case.

Each test case starts with a line contains a number n indicates the number of flowers.

Then there is a line contains n numbers indicates a[i].

Output
For each test case, print a single line containing one integer—the minimum times Kayaking prunes the flowers, or -1 if it is impossible.

Sample Input

2
5
1 2 2 2 2
5
1 2 2 2 3

Sample Output

1
-1

Hint
T ≤ 10
n ≤ 105
ai ≤ 109

🔗来自巨佬的题解:
逆向思维——裁剪n-1个 相当于 增长保留的那一个
边界考虑——max > Σ(max - a[i])

#include<stdio.h>
using namespace std;

int a[100005];

int main(){
	int n, m, t,max;
    cin >> t;
    while(t -- ){
        scanf("%d", &n);
        for(int i = 1; i <= n; ++ i){
            scanf("%d", &a[i]);
            max = max > a[i] ? max : a[i];
        }
        int sum = 0;
        for(int i = 1; i <= n; ++ i)
            sum += max - a[i];
        if(sum >= max) puts("-1");
        else printf("%d\n", sum);
    }
    return 0;
}

A Count Task

Problem Description
Count is one of WNJXYK’s favorite tasks. Recently, he had a very long string and he wondered that how many substrings which contains exactly one kind of lowercase in this long string. But this string is so long that he had kept counting for several days. His friend Kayaking wants to help him, so he turns to you for help.

Input
The input starts with one line contains exactly one positive integer T which is the number of test cases.

Each test case contains one line with a string which you need to do a counting task on.

Output
For each test case, output one line containing “y” where y is the number of target substrings.

Sample Input

3
qwertyuiop
qqwweerrttyyuuiioopp
aaaaaaaaaa

Sample Output

10
30
55

Hint
1 ≤ T ≤ 20
1 ≤ len(string) ≤ 105
1 ≤ ∑len(string) ≤ 105
Strings only contain lowercase English letters.

#include<stdio.h>

int main(){
	char c;
	int t;
	scanf("%d", &t);
	c=getchar();
	while(t--){
		bool flag[200]={true};
		int cnt=0;
		while((c=getchar())!='\n'){
			flag[c]=1;
		}
		for(int i=97;i<=122;i++)
			if(flag[i]) ++cnt;
		printf("%d\n",cnt);
	}
	return 0;
}

Financial Management

Description

Larry graduated this year and finally has a job. He’s making a lot of money, but somehow never seems to have enough. Larry has decided that he needs to grab hold of his financial portfolio and solve his financing problems. The first step is to figure out what’s been going on with his money. Larry has his bank account statements and wants to see how much money he has. Help Larry by writing a program to take his closing balance from each of the past twelve months and calculate his average account balance.

Input
The input will be twelve lines.
Each line will contain the closing balance of his bank account for a particular month.
Each number will be positive and displayed to the penny.
No dollar sign will be included.

Output

The output will be a single number, the average (mean) of the closing balances for the twelve months. It will be rounded to the nearest penny, preceded immediately by a dollar sign, and followed by the end-of-line. There will be no other spaces or characters in the output.

Sample Input

100.00
489.12
12454.12
1234.10
823.05
109.20
5.27
1542.25
839.18
83.99
1295.01
1.75

Sample Output

$1581.42

calculate average, even dont need to plus 0.005

#include<stdio.h>

int main(){
	double a,ave=0;
	for(int i=1;i<=12;i++){
		scanf("%lf",&a);
		ave+=a;
	}
	ave=ave/12.0;
	printf("$%.2lf\n",ave);
	return 0;
}

1046 A^B Mod C

给出3个正整数A B C,求A^B Mod C。
例如,3 5 8,3^5 Mod 8 = 3。

输入
3个正整数A B C,中间用空格分隔。(1 <= A,B,C <= 109)

输出
输出计算结果

输入样例

3 5 8

输出样例

3
  • pow()函数在math.h这个库里
  • pow()函数的声明:double pow(double x, double y) (注意数据类型)
#include<stdio.h>
#include<math.h>

int main(){
	double a,b;
	int c;
	scanf("%lf%lf%d",&a,&b,&c);
	printf("%d\n",(int)pow(a,b)/c);
	return 0;
}

Watermelon

One hot summer day Pete and his friend Billy decided to buy a watermelon. They chose the biggest and the ripest one, in their opinion. After that the watermelon was weighed, and the scales showed w kilos. They rushed home, dying of thirst, and decided to divide the berry, however they faced a hard problem.

Pete and Billy are great fans of even numbers, that’s why they want to divide the watermelon in such a way that each of the two parts weighs even number of kilos, at the same time it is not obligatory that the parts are equal. The boys are extremely tired and want to start their meal as soon as possible, that’s why you should help them and find out, if they can divide the watermelon in the way they want. For sure, each of them should get a part of positive weight.

Input
The first (and the only) input line contains integer number w (1 ≤ w ≤ 100) — the weight of the watermelon bought by the boys.

Output
Print YES, if the boys can divide the watermelon into two parts, each of them weighing even number of kilos; and NO in the opposite case.

Examples
input

8

output

YES

Note
For example, the boys can divide the watermelon into two parts of 2 and 6 kilos respectively (another variant — two parts of 4 and 4 kilos).

cf 第一简单题:判断是否是大于2的整数

#include<stdio.h>

int main()
{
	int a;
	scanf("%d",&a);
	if(a>2&&a%2==0)
		printf("YES");
	else
		printf("NO");
	return 0;
}

Way Too Long Words

Sometimes some words like “localization” or “internationalization” are so long that writing them many times in one text is quite tiresome.

Let’s consider a word too long, if its length is strictly more than 10 characters. All too long words should be replaced with a special abbreviation.

This abbreviation is made like this: we write down the first and the last letter of a word and between them we write the number of letters between the first and the last letters. That number is in decimal system and doesn’t contain any leading zeroes.

Thus, “localization” will be spelt as “l10n”, and "internationalization» will be spelt as “i18n”.

You are suggested to automatize the process of changing the words with abbreviations. At that all too long words should be replaced by the abbreviation and the words that are not too long should not undergo any changes.

Input
The first line contains an integer n (1 ≤ n ≤ 100). Each of the following n lines contains one word. All the words consist of lowercase Latin letters and possess the lengths of from 1 to 100 characters.

Output
Print n lines. The i-th line should contain the result of replacing of the i-th word from the input data.

Examples
input

4
word
localization
internationalization
pneumonoultramicroscopicsilicovolcanoconiosis

output

word
l10n
i18n
p43s

寒假打卡Day1 的水题( ̄▽ ̄)

#include<iostream>
#include<string.h>
using namespace std;

int main(){
	char ch[105];
	int t;
	cin>>t;
	while(t--){
		cin>>ch;
		if(strlen(ch)<=10){
			cout<<ch<<endl;
			continue;
		}
		cout<<ch[0]<<strlen(ch)-2<<ch[strlen(ch)-1]<<endl;
	}
	return 0;
} 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值