Algorithm training of Quantum team (20190927)

Algorithm training of Quantum team (20190927)

A - 水仙花數
春天是鮮花的季節,水仙花就是其中最迷人的代表,數學上有個水仙花數,他是這樣定義的:
“水仙花數”是指一個三位數,它的各位數字的立方和等於其本身,比如:153=13+53+3^3。
現在要求輸出所有在m和n範圍內的水仙花數。
Input
輸入數據有多組,每組佔一行,包括兩個整數m和n(100<=m<=n<=999)。
Output
對於每個測試實例,要求輸出所有在給定範圍內的水仙花數,就是說,輸出的水仙花數必須大於等於m,並且小於等於n,如果有多個,則要求從小到大排列在一行內輸出,之間用一個空格隔開;
如果給定的範圍內不存在水仙花數,則輸出no;
每個測試實例的輸出佔一行。
Sample Input
100 120
300 380
Sample Output
no
370 371

#include "bits/stdc++.h"
using namespace std;

int main() {
	int l,r;
	while(cin >> l >> r) {
		int count = 0;
		for(int i=l; i<=r; i++) {
			int sum = pow(i%10,3) + pow((i/10)%10,3) + pow(i/100,3);
			if(sum == i) {
				count++;
				if(count == 1)
					cout << i;
				else
					cout << " " << i;
			}
		}
		if(count == 0)
			cout << "no";
		cout << endl;
	}
	return 0;
}
/*
100 120
300 380
*/

B - 變形課
呃…變形課上Harry碰到了一點小麻煩,因爲他並不像Hermione那樣能夠記住所有的咒語而隨意的將一個棒球變成刺蝟什麼的,但是他發現了變形咒語的一個統一規律:如果咒語是以a開頭b結尾的一個單詞,那麼它的作用就恰好是使A物體變成B物體.
Harry已經將他所會的所有咒語都列成了一個表,他想讓你幫忙計算一下他是否能完成老師的作業,將一個B(ball)變成一個M(Mouse),你知道,如果他自己不能完成的話,他就只好向Hermione請教,並且被迫聽一大堆好好學習的道理.
Input
測試數據有多組。每組有多行,每行一個單詞,僅包括小寫字母,是Harry所會的所有咒語.數字0表示一組輸入結束.
Output
如果Harry可以完成他的作業,就輸出"Yes.",否則就輸出"No."(不要忽略了句號)
Sample Input
so
soon
river
goes
them
got
moon
begin
big
0
Sample Output
Yes.

Harry 可以念這個咒語:“big-got-them”.
Hint
Hint


F-硬幣
Snoopy has three coins. One day he tossed them on a table then and tried to flip some of them so that they had either all heads or all tails facing up. After several attempts, he found that regardless of the initial configuration of the coins, he could always achieve the goal by doing exactly two flippings, under the condition that only one coin could be flipped each time and a coin could be flipped more than once. He also noticed that he could never succeed with less than two flippings.

Snoopy then wondered, if he had n coins, was there a minimum number x such that he could do exactly x flippings to satisfy his requirements?

Input
The input contains multiple test cases. Each test case consists of a single positive integer n (n < 10,000) on a separate line. A zero indicates the end of input and should not be processed.

Output
For each test case output a single line containing your answer without leading or trailing spaces. If the answer does not exist, output “No Solution!”

Sample Input
2
3
0
Sample Output
No Solution!
2

#include<stdio.h>
#include<string.h>
#include <iostream>
using namespace std;
int main() {
	int n;
	while(scanf("%d",&n)!=EOF&&n) {
		if(n%2==0)
			printf("No Solution!\n");
		else
			printf("%d\n",n-1);
	}
	return 0;
}
/*
2
3
0
*/ 

G - Goldbach’s Conjecture
In 1742, Christian Goldbach, a German amateur mathematician, sent a letter to Leonhard Euler in which he made the following conjecture:
Every even number greater than 4 can be
written as the sum of two odd prime numbers.

For example:
8 = 3 + 5. Both 3 and 5 are odd prime numbers.
20 = 3 + 17 = 7 + 13.
42 = 5 + 37 = 11 + 31 = 13 + 29 = 19 + 23.

Today it is still unproven whether the conjecture is right. (Oh wait, I have the proof of course, but it is too long to write it on the margin of this page.)
Anyway, your task is now to verify Goldbach’s conjecture for all even numbers less than a million.
Input
The input will contain one or more test cases.
Each test case consists of one even integer n with 6 <= n < 1000000.
Input will be terminated by a value of 0 for n.
Output
For each test case, print one line of the form n = a + b, where a and b are odd primes. Numbers and operators should be separated by exactly one blank like in the sample output below. If there is more than one pair of odd primes adding up to n, choose the pair where the difference b - a is maximized. If there is no such pair, print a line saying “Goldbach’s conjecture is wrong.”
Sample Input
8
20
42
0
Sample Output
8 = 3 + 5
20 = 3 + 17
42 = 5 + 37

#include "bits/stdc++.h"
using namespace std;

int p[1000010],n,a[200010],cnt;
void get_prime() {
	p[1]=1;
	for(int i=2; i<=1000; i++)
		if(!p[i])
			for(int j=i*2; j<=1000000; j=j+i)
				p[j]=1;
	for(int i=2; i<=1000000; i++)
		if(!p[i])a[++cnt]=i;
}
int main() {
	get_prime();
	while(scanf("%d",&n)==1&&n) {
		for(int i=1; a[i]<=n/2; i++)
			if(!p[n-a[i]]) {
				printf("%d = %d + %d\n",n,a[i],n-a[i]);
				break;
			} 
	}
	return 0;
}
/*
8
20
42
0
*/
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章