中石油(某些代码)
問題1:順序查找
大概題意:
從輸入的n個整數中查找給定的SearchNum(若存在必唯一)。如果找到,輸出SearchNum的位置(從0開始數);如果沒有找到,輸出“Not Found”。
要求:
有3行。第1行是1個正整數n(n≤20)。第2行是n個整數,數字均不超過基本整型,其間以空格分隔。第3行是SearchNum。
輸出
僅一行。如果找到,輸出SearchNum的位置(從0開始數);如果沒有找到,輸出“Not Found”。
問題2:2的n次方
大概題意:
對于任意給定的n,計算2的n次方。
要求:
輸入一個整數,輸出2的n次方。
問題3:小x與三角形
大概題意:
給出三角形的兩條邊,求滿足條件的三角形有幾個。
#pragma GCC optimize(2) #pragma G++ optimize(2) #include<bits/stdc++.h> using namespace std; typedef long long ll; const int maxn=1e5+9;int main() {int a,b,sum=0,left,right;int i;cin>>a>>b;left=abs(a-b)-1;right=a+b-1;sum=right-left-1;cout<<sum<<endl;return 0; }代碼片本道題用枚舉法會超時,所以要改變思路,利用技巧解答,根據兩邊之和大于第三邊,兩邊之差小于第三邊,兩種情況可以作為兩種邊界條件。
問題3:幼兒園小朋友們的難題
大概題意:輸入多組字符串,找到字符串最大的非負整數。
要求:輸入
輸入有多組數據。每組輸入一個句子(一定包含數字字符,可能包含空格),長度小于256,占一行
輸出
輸出對應有多行,每行輸出所找出的最大的那個非負整數
補充知識點:
對于string的輸入需要注意,當想讀取一整行只能用getline(),在用getline讀取時可以包括空格。
比較字符串出現的數字的大小的方法:
(1)可以直接比較連續數字的長度,長度長的自然大于長度短的。
(2)如果長度相等時,可以利用string類的比較大小,因為string 類型對于字符的比較是按照字典序排序的,從前往后進行比較。
所以對于本題可以直接用string類儲存答案,將最大值初始化成0,遇到非0時再進行記錄,這樣可以避免出現前導零的情況。
問題4:特殊的數字
題目描述
153是一個非常特殊的數,它等于它的每位數字的立方和,即153=111+555+333。編程求所有滿足這種條件的三位十進制數。
輸出
輸出格式
按從小到大的順序輸出滿足條件的三位十進制數,每個數占一行。
問題5:分蘋果
把一堆蘋果分給 n 個小朋友,要使每個人都能拿到蘋果,而且每個人拿到的蘋果數都不同的話,這堆蘋果至少應該有多少個?
輸入
一個不大于 1000 的正整數 n,代表小朋友人數。
輸出
一個整數,表示滿足條件的最少蘋果個數。
問題6:Mad Scientist(中石油16L)
題目描述
Farmer John’s cousin Ben happens to be a mad scientist. Normally, this creates a good bit of friction at family gatherings, but it can occasionally be helpful, especially when Farmer John finds himself facing unique and unusual problems with his cows.
Farmer John is currently facing a unique and unusual problem with his cows. He recently ordered N cows (1≤N≤1000) consisting of two different breeds: Holsteins and Guernseys. He specified the cows in his order in terms of a string of N characters, each either H (for Holstein) or G (for Guernsey). Unfortunately, when the cows arrived at his farm and he lined them up, their breeds formed a different string from this original string.
Let us call these two strings A and B, where A is the string of breed identifiers Farmer John originally wanted, and B is the string he sees when his cows arrive. Rather than simply check if re-arranging the cows in B is sufficient to obtain A, Farmer John asks his cousin Ben to help him solve the problem with his scientific ingenuity.
After several months of work, Ben creates a remarkable machine, the multi-cow-breed-flipinator 3000, that is capable of taking any substring of cows and toggling their breeds: all Hs become Gs and all Gs become Hs in the substring. Farmer John wants to figure out the minimum number of times he needs to apply this machine to transform his current ordering B into his original desired ordering A. Sadly, Ben’s mad scientist skills don’t extend beyond creating ingenious devices, so you need to help Farmer John solve this computational conundrum.
輸入
The first line of input contains N, and the next two lines contain the strings A and B. Each string has N characters that are either H or G.
輸出
Print the minimum number of times the machine needs to be applied to transform BB into AA.
樣例輸入 Copy
7
GHHHGHH
HHGGGHH
樣例輸出 Copy
2
提示
First, FJ can transform the substring that corresponds to the first character alone, transforming B into GHGGGHH. Next, he can transform the substring consisting of the third and fourth characters, giving A. Of course, there are other combinations of two applications of the machine that also work.
問題7: Word Processor
題目描述
Bessie the cow is working on an essay for her writing class. Since her handwriting is quite bad, she decides to type the essay using a word processor.
The essay contains N words (1≤N≤100), separated by spaces. Each word is between 1 and 15 characters long, inclusive, and consists only of uppercase or lowercase letters. According to the instructions for the assignment, the essay has to be formatted in a very specific way: each line should contain no more than K (1≤K≤80) characters, not counting spaces. Fortunately, Bessie’s word processor can handle this requirement, using the following strategy:
·If Bessie types a word, and that word can fit on the current line, put it on that line.
·Otherwise, put the word on the next line and continue adding to that line.
Of course, consecutive words on the same line should still be separated by a single space. There should be no space at the end of any line.
Unfortunately, Bessie’s word processor just broke. Please help her format her essay properly!
輸入
The first line of input contains two space-separated integers N and K.
The next line contains N words separated by single spaces. No word will ever be larger than K characters, the maximum number of characters on a line.
輸出
Bessie’s essay formatted correctly.
樣例輸入 Copy
10 7
hello my name is Bessie and this is my essay
樣例輸出 Copy
hello my
name is
Bessie
and this
is my
essay
問題8:Sleepy Kaguya
Description
Houraisan☆Kaguya is the princess who lives in Literally House of Eternity. However, she is very playful and often stays up late. This morning, her tutor, Eirin Yagokoro was going to teach her some knowledge about the Fibonacci sequence. Unfortunately, the poor princess was so sleepy in class that she fell asleep. Angry Eirin asked her to complete the following task:
This sequence can be described by following equations:
1.F[1]=F[2]=1
2.F[n]=F[n?1]+F[n?2] (n>2)
Now, Kaguya is required to calculate F[k+1]?F[k+1]?F[k]?F[k+2] for each integer k that does
not exceed 1018. Kaguya is so pathetic. You have an obligation to help her. (I love Houraisan Kaguya forever!!!)
Input
Only one integer k
Output
Only one integer as the result which is equal to F[k+1]?F[k+1]?F[k]?F[k+2]
Samples
Input Copy
2
Output
1
Hint
F[2]=1,F[3]=2,F[4]=3
22-13
0<k≤1018
If necessary, please use %I64d instead of %lld when you use “scanf”, or just use “cin” to get the cases.
The online judge of HNU has the above feature, thank you for your cooperation.
找規律判斷奇偶即可。
問題9:B. The Right-angled Triangleswith Sides of Integral Length
Description
Consider the right-angled triangles with sides of integral length. Give you the integral length of the hypotenuse of a right-angled triangle. Can it construct a right triangle with given hypotenuse c such that the two legs of the triangle are all integral length
Input
There are several test cases. The first line contains an integer T(1≤T≤1,000), T is the number of test cases. The following T lines contain T test cases, each line contains one test case. For each test case, there is an integer : c, the length of hypotenuse.(1≤c≤45,00)
Output
For each case, output Yes if it can construct a right triangle with given hypotenuse c and sides of integral length , No otherwise
Samples
Input Copy
4
5
6
15
13
Output
Yes
No
Yes
Yes
如果三角形三邊滿足如下關系,則是直角三角形。
? a=m2-n2
? b=2mn
? c=m2+n2
解釋:這是本原勾股數 因為
a2+b2
=(m^4 + n^4)- 2×m2×n2+(2mn)^2
=(m2+n2)2=c2
? 所以如果斜邊長度能夠表示成2個正整數的平方和,則能使得三邊都是正整數。這樣枚舉的復雜度是O( c)。
? 另外,如果斜邊長度是一個合數,其有一個因子能表示為2個正整數的平方和,那么也能使得三邊都是正整數。比如c=15,有因
子5=12+22,那么也是可以構成三邊全是整數的直角三角形,每邊長度乘以3即可。就是( 9, 12, 15)。
問題10:記數問題
試計算在區間 1 到 n 的所有整數中,數字 x (0 ≤ x ≤ 9)共出現了多少次?例如,在 1到 11 中,即在 1、2、3、4、5、6、7、8、9、10、11 中,數字 1 出現了 4 次。
輸入
輸入共 1 行,包含 2 個整數 n、x,之間用一個空格隔開。1≤n≤1,000,000 ,0≤x≤9
輸出
輸出共 1 行,包含一個整數,表示 x 出現的次數。
樣例輸入 Copy
11 1
樣例輸出 Copy
4
問題11: Teleportation II
題目描述
One of the farming chores Farmer John dislikes the most is hauling around lots of cow manure. In order to streamline this process, he comes up with a brilliant invention: the manure teleporter! Instead of hauling manure between two points in a cart behind his tractor, he can use the manure teleporter to instantly transport manure from one location to another.
Farmer John’s farm is built along a single long straight road, so any location on his farm can be described simply using its position along this road (effectively a point on the number line). A teleporter is described by two numbers x and y, where manure brought to location x can be instantly transported to location y, or vice versa.
Farmer John wants to transport manure from location a to location b, and he has built a teleporter that might be helpful during this process (of course, he doesn’t need to use the teleporter if it doesn’t help). Please help him determine the minimum amount of total distance he needs to haul the manure using his tractor.
輸入
The first and only line of input contains four space-separated integers: a and b, describing the start and end locations, followed by x and y, describing the teleporter. All positions are integers in the range 0…100, and they are not necessarily distinct from each-other.
輸出
Print a single integer giving the minimum distance Farmer John needs to haul manure in his tractor.
樣例輸入 Copy
3 10 8 2
樣例輸出 Copy
3
提示
In this example, the best strategy is to haul the manure from position 3 to position 2, teleport it to position 8, then haul it to position 10. The total distance requiring the tractor is therefore 1 + 2 = 3.
問題12:Training Camp
題目描述
Snuke loves working out. He is now exercising N times.
Before he starts exercising, his power is 1. After he exercises for the i-th time, his power gets multiplied by i.
Find Snuke’s power after he exercises N times. Since the answer can be extremely large, print the answer modulo 109+7.
Constraints
1≤N≤105
輸入
The input is given from Standard Input in the following format:
N
輸出
Print the answer modulo 109+7.
樣例輸入 Copy
3
樣例輸出 Copy
6
提示
After Snuke exercises for the first time, his power gets multiplied by 1 and becomes 1.
After Snuke exercises for the second time, his power gets multiplied by 2 and becomes 2.
After Snuke exercises for the third time, his power gets multiplied by 3 and becomes 6.
求1到n的階乘對1e9+7取余即可
問題13:Scc Puzzle
題目描述
Snuke loves puzzles.
Today, he is working on a puzzle using S- and c-shaped pieces. In this puzzle, you can combine two c-shaped pieces into one S-shaped piece, as shown in the figure below:
Snuke decided to create as many Scc groups as possible by putting together one S-shaped piece and two c-shaped pieces.
Find the maximum number of Scc groups that can be created when Snuke has N S-shaped pieces and M c-shaped pieces.
Constraints
1≤N,M≤1012
輸入
The input is given from Standard Input in the following format:
N M
輸出
Print the answer.
樣例輸入 Copy
1 6
樣例輸出 Copy
2
問題14:表達式求值
題目描述
給定一個只包含加法和乘法的算術表達式,請你編程計算表達式的值。
輸入
輸入僅有一行,為需要你計算的表達式,表達式中只包含數字、加法運算符“+”和乘法運算符“*”,且沒有括號,所有參與運算的數字均為 0 到 2^31-1 之間的整數。輸入數據保證這一行只有 0~ 9、+、這 12 種字符。
輸出
輸出只有一行,包含一個整數, 表示這個表達式的值。 注意: 當答案長度多于 4 位時,請只輸出最后 4 位,前導 0 不輸出。
樣例輸入 Copy
1+10000000031
樣例輸出 Copy
4
問題15:小朋友的數字
題目描述
有n個小朋友排成一列。每個小朋友手上都有一個數字,這個數字可正可負。規定每個小朋友的特征值等于排在他前面(包括他本人)的小朋友中連續若干個(最少有一個)小朋友手上的數字之和的最大值。
作為這些小朋友的老師,你需要給每個小朋友一個分數,分數是這樣規定的:第一個小朋友的分數是他的特征值,其它小朋友的分數為排在他前面的所有小朋友中(不包括他本人),小朋友分數加上其特征值的最大值。
請計算所有小朋友分數的最大值,輸出時保持最大值的符號,將其絕對值對p取模后輸出。
輸入
第一行包含兩個正整數n、p,之間用一個空格隔開。
第二行包含n個數,每兩個整數之間用一個空格隔開,表示每個小朋友手上的數字。
輸出
輸出只有一行,包含一個整數,表示最大分數對p取模的結果。
樣例輸入 Copy
5 997
1 2 3 4 5
樣例輸出 Copy
21
提示
樣例1小朋友的特征值分別為1、3、6、10、15,分數分別為1、2、5、11、21,最大值21對997的模是21。
對于 100%的數據,1 ≤ n ≤ 1,000,000,1 ≤ p ≤ 10^9,其他數字的絕對值均不超過 10^9。
#include<bits/stdc++.h> using namespace std; typedef long long ll; ll a[1000005][3],n,p,x,maxn; bool bo=0; int main() {ll n,p,x,i;cin>>n>>p;p=abs(p);cin>>x;a[1][1]=a[1][0]=a[1][2]=x;maxn=x;for(i=2;i<=n;i++){cin>>x;if(a[i-1][1]>0) a[i][1]=a[i-1][1]+x;else a[i][1]=x;a[i][0]=max(maxn,a[i][1]);maxn=a[i][0];}a[2][2]=a[1][0]*2;if(a[2][2]>=a[1][2]) bo=1;for(i=3;i<=n;i++){if(a[i-1][0]<0) a[i][2]=a[i-1][2];else {a[i][2]=a[i-1][2]+a[i-1][0];if(a[i][2]>a[1][2]) bo=1;if(a[i][2]>1000000000)a[i][2]%=p;}}if(bo) cout<<a[n][2]%p<<'\n';else cout<<a[1][2]%p<<'\n';return 0; }問題16:Three-letter acronym
題目描述
You are given three words s1, s2 and s3, each composed of lowercase English letters, with spaces in between. Print the acronym formed from the uppercased initial letters of the words.
Constraints
s1, s2 and s3 are composed of lowercase English letters.
1≤|si|≤10(1≤i≤3)
輸入
Input is given from Standard Input in the following format:
s1 s2 s3
輸出
Print the answer.
樣例輸入 Copy
atcoder beginner contest
樣例輸出 Copy
ABC
提示
The initial letters of atcoder, beginner and contest are a, b and c. Uppercase and concatenate them to obtain ABC.
問題17:買鉛筆
題目描述
P老師需要去商店買n支鉛筆作為小朋友們參加NOIP的禮物。她發現商店一共有3種包裝的鉛筆,不同包裝內的鉛筆數量有可能不同,價格也有可能不同。為了公平起見,P老師決定只買同一種包裝的鉛筆。
商店不允許將鉛筆的包裝拆開,因此P老師可能需要購買超過n支鉛筆才夠給小朋友們發禮物。
現在P老師想知道,在商店每種包裝的數量都足夠的情況下,要買夠至少n支鉛筆最少需要花費多少錢。
輸入
輸入的第一行包含一個正整數n,表示需要的鉛筆數量。接下來三行,每行用兩個正整數描述一種包裝的鉛筆:其中第一個整數表示這種包裝內鉛筆的數量,第二個整數表示這種包裝的價格。保證所有的7個數都是不超過10000的正整數。
輸出
輸出一行一個整數,表示P老師最少需要花費的錢。
樣例輸入 Copy
57
2 2
50 30
30 27
樣例輸出 Copy
54
提示
鉛筆的三種包裝分別是:
·2支裝,價格為2;
·50支裝,價格為30;
·30支裝,價格為27。
P老師需要購買至少_57支鉛筆。
如果她選擇購買第一種包裝,那么她需要購買29份,共計2×29=5 8支,需要花費的錢為2×29=58。
實際上,P老師會選擇購買第三種包裝,這樣需要買2份。雖然最后買到的鉛筆數量更多了,為30×2=60支,但花費卻減少為27×2=_54,比第一種少。
對于第二種包裝,雖然每支鉛筆的價格是最低的,但要夠發必須買2份,實際的花費達到了30×2=60,因此P老師也不會選擇。
所以最后輸出的答案是54。
注意本題不是貪心題哦!
問題18:Comparison
題目描述
You are given two positive integers A and B. Compare the magnitudes of these numbers.
Constraints
1≤A,B≤10100
Neither A nor B begins with a 0.
輸入
Input is given from Standard Input in the following format:
A
B
輸出
Print GREATER if A>B, LESS if A<B and EQUAL if A=B.
樣例輸入 Copy
36
24
樣例輸出 Copy
GREATER
提示
Since 36>24, print GREATER.
問題19:回文日期
題目描述
在日常生活中,通過年、月、日這三個要素可以表示出一個唯一確定的日期。
牛牛習慣用8位數字表示一個日期,其中,前4位代表年份,接下來2位代表月份,最后2位代表日期。顯然:一個日期只有一種表示方法,而兩個不同的日期的表示方法不會相同。
牛牛認為,一個日期是回文的,當且僅當表示這個日期的8位數字是回文的。現在,牛牛想知道:在他指定的兩個日期之間(包含這兩個日期本身),有多少個真實存在的日期是回文的。
【提示】
一個8位數字是回文的,當且僅當對于所有的i(1<i<8)從左向右數的第i個數字和第9-i個數字(即從右向左數的第i個數字)是相同的。
例如:
·對于2016年11月19日,用8位數字20161119表示,它不是回文的。
·對于2010年1月2日,用8位數字20100102表示,它是回文的。
·對于2010年10月2日,用8位數字20101002表示,它不是回文的。
每一年中都有12個月份: 其中,1, 3, 5, 7, 8, 10, 12月每個月有31天;4, 6, 9, 11月每個月有30天;而對于2月,閏年時有29天,平年時有28天。 一個年份是閏年當且僅當它滿足下列兩種情況其中的一種: 1.這個年份是4的整數倍,但不是100的整數倍; 2.這個年份是400的整數倍。
例如:
·以下幾個年份都是閏年:2000 , 2012 , 2016。
·以下幾個年份是平年:1900, 2011、2014。
輸入
輸入包括兩行,每行包括一個8位數字。
第一行表示牛牛指定的起始日期dates,第二行表示牛牛指定的終止日期date2。保證dates和date2都是真實存在的日期,且年份部分一定為4位數字,且首位數字不為0;保證dates一定不晚于date2。
輸出
輸出一行,包含一個整數,表示在date1和date2之間,有多少個日期是回文的。
樣例輸入 Copy
20110101
20111231
樣例輸出 Copy
1
提示
對于樣例1,符合條件的日期是20111102。
總結
- 上一篇: 迷惑新手的IOS开发问题
- 下一篇: 走着走着,就剩下了沉默