日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問(wèn) 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 编程资源 > 编程问答 >内容正文

编程问答

中石油(某些代码)

發(fā)布時(shí)間:2024/1/8 编程问答 27 豆豆
生活随笔 收集整理的這篇文章主要介紹了 中石油(某些代码) 小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

問(wèn)題1:順序查找

大概題意:
從輸入的n個(gè)整數(shù)中查找給定的SearchNum(若存在必唯一)。如果找到,輸出SearchNum的位置(從0開(kāi)始數(shù));如果沒(méi)有找到,輸出“Not Found”。

要求:
有3行。第1行是1個(gè)正整數(shù)n(n≤20)。第2行是n個(gè)整數(shù),數(shù)字均不超過(guò)基本整型,其間以空格分隔。第3行是SearchNum。
輸出
僅一行。如果找到,輸出SearchNum的位置(從0開(kāi)始數(shù));如果沒(méi)有找到,輸出“Not Found”。

方法1#include<bits/stdc++.h> using namespace std; int main() {int n,i,m,flag=0;int a[10001];cin>>n;for(i=0;i<n;i++){cin>>a[i];}cin>>m;for(i=0;i<n;i++){if(a[i]==m){cout<<i;flag=1;/用flag標(biāo)記一下,flag=1證明真。}}if(flag==0)cout<<"Not Found";return 0; } 方法2int main() {int i, n, x, index;int a[N];scanf("%d", &n);scanf("%d", &x);for (i = 0; i < n; i++){scanf("%d", &a[i]);}for (i = 0; i < n; i++){if (a[i] == x){index = i;/用index來(lái)標(biāo)記查找元素的下標(biāo)printf("%d", index);break;}}if (i == n){printf("Not Found\n");}return 0; }

問(wèn)題2:2的n次方

大概題意:
對(duì)于任意給定的n,計(jì)算2的n次方。

要求:
輸入一個(gè)整數(shù),輸出2的n次方。

方法1#include<bits/stdc++.h> using namespace std;int main() {int n;cin>>n;int power=1;/必須要初始成1for(int i=1;i<=n;i++)/此處的for循環(huán)代表的意思是n次{power=power*2;}cout<<power<<endl;return 0;相當(dāng)于:1*21*2*21*2*2*2;方法2:直接用pow(2,n)即可,但是建議不要使用,數(shù)越大誤差越大。 }插入代碼片

問(wèn)題3:小x與三角形

大概題意:

給出三角形的兩條邊,求滿足條件的三角形有幾個(gè)。

#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; }代碼片

本道題用枚舉法會(huì)超時(shí),所以要改變思路,利用技巧解答,根據(jù)兩邊之和大于第三邊,兩邊之差小于第三邊,兩種情況可以作為兩種邊界條件。

問(wèn)題3:幼兒園小朋友們的難題

大概題意:輸入多組字符串,找到字符串最大的非負(fù)整數(shù)。

要求:輸入
輸入有多組數(shù)據(jù)。每組輸入一個(gè)句子(一定包含數(shù)字字符,可能包含空格),長(zhǎng)度小于256,占一行

輸出
輸出對(duì)應(yīng)有多行,每行輸出所找出的最大的那個(gè)非負(fù)整數(shù)

#include<bits/stdc++.h> using namespace std; int main() {string s;//定義一個(gè)字符串while(getline(cin,s)){string max="0";//因?yàn)轭}目中要求的是最大負(fù)整數(shù),所以假設(shè)最大為0即可for(int i=0;i<s.size();i++){if(isdigit(s[i])&&s[i]!='0')//判斷字符串是否為數(shù)字并且不含有前導(dǎo)零{string a;while(isdigit(s[i])){a+=s[i++];//如果為數(shù)字那么將字符儲(chǔ)存到字符串a(chǎn)中即可}if(a.size() >max.size() ||(a.size() ==max.size() &&a>max)){max=a;}}}cout<<max<<endl;}return 0; }

補(bǔ)充知識(shí)點(diǎn):
對(duì)于string的輸入需要注意,當(dāng)想讀取一整行只能用getline(),在用getline讀取時(shí)可以包括空格。

比較字符串出現(xiàn)的數(shù)字的大小的方法:
(1)可以直接比較連續(xù)數(shù)字的長(zhǎng)度,長(zhǎng)度長(zhǎng)的自然大于長(zhǎng)度短的。
(2)如果長(zhǎng)度相等時(shí),可以利用string類的比較大小,因?yàn)閟tring 類型對(duì)于字符的比較是按照字典序排序的,從前往后進(jìn)行比較。
所以對(duì)于本題可以直接用string類儲(chǔ)存答案,將最大值初始化成0,遇到非0時(shí)再進(jìn)行記錄,這樣可以避免出現(xiàn)前導(dǎo)零的情況。

問(wèn)題4:特殊的數(shù)字

題目描述
153是一個(gè)非常特殊的數(shù),它等于它的每位數(shù)字的立方和,即153=111+555+333。編程求所有滿足這種條件的三位十進(jìn)制數(shù)。

輸出
輸出格式
按從小到大的順序輸出滿足條件的三位十進(jìn)制數(shù),每個(gè)數(shù)占一行。

#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,c,sum=0;for(int i=100;i<=999;i++){int t=i;a=t%10;b=t/10%10;c=t/100;sum=pow(a,3)+pow(b,3)+pow(c,3);if(t==sum)cout<<t<<endl;}return 0; }

問(wèn)題5:分蘋果

把一堆蘋果分給 n 個(gè)小朋友,要使每個(gè)人都能拿到蘋果,而且每個(gè)人拿到的蘋果數(shù)都不同的話,這堆蘋果至少應(yīng)該有多少個(gè)?

輸入
一個(gè)不大于 1000 的正整數(shù) n,代表小朋友人數(shù)。
輸出
一個(gè)整數(shù),表示滿足條件的最少蘋果個(gè)數(shù)。

#include<bits/stdc++.h> using namespace std; typedef long long ll;int main() {ll n,sum=0,i;cin>>n;for(i=1;i<=n;i++){sum+=i;/從第一個(gè)開(kāi)始后一個(gè)比前一個(gè)多一即可:12345....}cout<<sum<<endl;return 0; }

問(wè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.

#include<bits/stdc++.h> using namespace std; int k,n,sum=0; string a,b; int flag[10001]; int main() {cin>>n;cin>>a;cin>>b;for(int i=0;i<n;i++){if(a[i]!=b[i])flag[i]=1;/不相等記為1elseflag[i]=0;相等記為0}for(int i=0;i<n;i++){if(flag[i]==1&&flag[i+1]!=1)sum++;}cout<<sum<<endl;return 0; }

問(wèn)題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

#include<bits/stdc++.h> using namespace std; typedef long long ll; const int maxn=1e5+9; int main() {int n,k;cin>>n>>k;string s[n];for(int i=0 ; i<n ;i++) cin>>s[i];int cnt=0;/cnt代表當(dāng)前的長(zhǎng)度for(int i=0;i<n;i++){cout<<s[i]<<" ";cnt+=s[i].size();if(cnt+s[i+1].size()>k){cout<<endl;cnt=0;}}return 0; }

問(wèn)題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.

#include<bits/stdc++.h> using namespace std; typedef long long ll; const int maxn=1e5+9; int main() {ll n;cin>>n;if(n%2==0)cout<<"1"<<endl;elsecout<<"-1"<<endl;return 0; }

找規(guī)律判斷奇偶即可。

問(wèn)題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

#include<bits/stdc++.h> using namespace std; typedef long long ll; const int maxn=1e5+9; bool fac(ll c) {ll c1=c*c;for(int i=1;i*i<=c1/2;i++){ll d=sqrt(c1-i*i);if(d*d==c1-i*i)return 1;}return 0; } int main() {ll n,a;cin>>n;while(n--){cin>>a;if(fac(a)){cout<<"Yes"<<endl;}elsecout<<"No"<<endl;}return 0; }

如果三角形三邊滿足如下關(guān)系,則是直角三角形。
? a=m2-n2
? b=2mn
? c=m2+n2

解釋:這是本原勾股數(shù) 因?yàn)?br /> a2+b2
=(m^4 + n^4)- 2×m2×n2+(2mn)^2
=(m2+n2)2=c2
? 所以如果斜邊長(zhǎng)度能夠表示成2個(gè)正整數(shù)的平方和,則能使得三邊都是正整數(shù)。這樣枚舉的復(fù)雜度是O( c)。
? 另外,如果斜邊長(zhǎng)度是一個(gè)合數(shù),其有一個(gè)因子能表示為2個(gè)正整數(shù)的平方和,那么也能使得三邊都是正整數(shù)。比如c=15,有因
子5=12+22,那么也是可以構(gòu)成三邊全是整數(shù)的直角三角形,每邊長(zhǎng)度乘以3即可。就是( 9, 12, 15)。

問(wèn)題10:記數(shù)問(wèn)題

試計(jì)算在區(qū)間 1 到 n 的所有整數(shù)中,數(shù)字 x (0 ≤ x ≤ 9)共出現(xiàn)了多少次?例如,在 1到 11 中,即在 1、2、3、4、5、6、7、8、9、10、11 中,數(shù)字 1 出現(xiàn)了 4 次。
輸入
輸入共 1 行,包含 2 個(gè)整數(shù) n、x,之間用一個(gè)空格隔開(kāi)。1≤n≤1,000,000 ,0≤x≤9
輸出
輸出共 1 行,包含一個(gè)整數(shù),表示 x 出現(xiàn)的次數(shù)。
樣例輸入 Copy
11 1
樣例輸出 Copy
4

#include<bits/stdc++.h> using namespace std; typedef long long ll; const int maxn=1e5+9; int main() {ll n,sum=0,x,ans=0,b;cin>>n>>x;for(int i=1;i<=n;i++){ll b=i;while(b!=0){sum=b%10;b/=10;if(sum==x)ans++;}}cout<<ans<<'\n';return 0; }

問(wèn)題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.

#include<bits/stdc++.h> using namespace std; typedef long long ll; const int maxn=1e5+5; const int N=1e9+7; int main() {int a,b,x,y,sum=0;cin>>a>>b>>x>>y;int tem=abs(a-b);if(abs(a-x)<abs(a-y)){sum+=abs(a-x)+abs(b-y);}else{sum+=abs(b-x) +=abs(a-y);}int ans=min(sum,tem);cout<<ans<<'\n';return 0; }

問(wèn)題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.

#include<bits/stdc++.h> using namespace std; typedef long long ll; const int maxn=1e5+5; const int N=1e9+7; ll a[maxn]; int main() {ll n;a[0]=1;for(int i=1;i<=maxn;i++)//打表{a[i]=a[i-1]*i%N;}cin>>n;cout<<a[n]<<'\n';return 0; } //第二種方法: {ll n,sum=1;cin>>n;for(int i=1;i<=n;i++){sum=sum*i%N;}cout<<sum%N<<'\n'; }

求1到n的階乘對(duì)1e9+7取余即可

問(wèn)題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

#include<bits/stdc++.h> using namespace std; typedef long long ll; const int maxn=1e5+5; const int N=1e9+7; ll a[maxn]; int main() {ll n,m;cin>>n>>m;if(m/2<n) cout<<m/2;else cout<<n+((m-n*2)/4)<<'\n';return 0; }

問(wèn)題14:表達(dá)式求值

題目描述
給定一個(gè)只包含加法和乘法的算術(shù)表達(dá)式,請(qǐng)你編程計(jì)算表達(dá)式的值。
輸入
輸入僅有一行,為需要你計(jì)算的表達(dá)式,表達(dá)式中只包含數(shù)字、加法運(yùn)算符“+”和乘法運(yùn)算符“*”,且沒(méi)有括號(hào),所有參與運(yùn)算的數(shù)字均為 0 到 2^31-1 之間的整數(shù)。輸入數(shù)據(jù)保證這一行只有 0~ 9、+、這 12 種字符。
輸出
輸出只有一行,包含一個(gè)整數(shù), 表示這個(gè)表達(dá)式的值。 注意: 當(dāng)答案長(zhǎng)度多于 4 位時(shí),請(qǐng)只輸出最后 4 位,前導(dǎo) 0 不輸出。
樣例輸入 Copy
1+10000000031
樣例輸出 Copy
4

#include<bits/stdc++.h> using namespace std; typedef long long ll; const int maxn=1e5+9; stack<int> x; int main() {int a,b;char c;cin>>a;int m=10000;a=a%m;x.push(a);while(cin>>c>>b){if(c=='*'){a=x.top();x.pop();x.push(a*b%m);}elsex.push(b);}a=0;while(x.size()){a+=x.top();a%=m;x.pop();}cout<<a<<'\n';return 0; }

問(wèn)題15:小朋友的數(shù)字

題目描述
有n個(gè)小朋友排成一列。每個(gè)小朋友手上都有一個(gè)數(shù)字,這個(gè)數(shù)字可正可負(fù)。規(guī)定每個(gè)小朋友的特征值等于排在他前面(包括他本人)的小朋友中連續(xù)若干個(gè)(最少有一個(gè))小朋友手上的數(shù)字之和的最大值。

作為這些小朋友的老師,你需要給每個(gè)小朋友一個(gè)分?jǐn)?shù),分?jǐn)?shù)是這樣規(guī)定的:第一個(gè)小朋友的分?jǐn)?shù)是他的特征值,其它小朋友的分?jǐn)?shù)為排在他前面的所有小朋友中(不包括他本人),小朋友分?jǐn)?shù)加上其特征值的最大值。

請(qǐng)計(jì)算所有小朋友分?jǐn)?shù)的最大值,輸出時(shí)保持最大值的符號(hào),將其絕對(duì)值對(duì)p取模后輸出。
輸入
第一行包含兩個(gè)正整數(shù)n、p,之間用一個(gè)空格隔開(kāi)。
第二行包含n個(gè)數(shù),每?jī)蓚€(gè)整數(shù)之間用一個(gè)空格隔開(kāi),表示每個(gè)小朋友手上的數(shù)字。
輸出
輸出只有一行,包含一個(gè)整數(shù),表示最大分?jǐn)?shù)對(duì)p取模的結(jié)果。
樣例輸入 Copy
5 997
1 2 3 4 5
樣例輸出 Copy
21
提示
樣例1小朋友的特征值分別為1、3、6、10、15,分?jǐn)?shù)分別為1、2、5、11、21,最大值21對(duì)997的模是21。

對(duì)于 100%的數(shù)據(jù),1 ≤ n ≤ 1,000,000,1 ≤ p ≤ 10^9,其他數(shù)字的絕對(duì)值均不超過(guò) 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; }

問(wèn)題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.

#include<bits/stdc++.h> using namespace std; typedef long long ll; const int maxn=1e5+9;int main() {char a[200],ch1;while(cin>>a){int len1=strlen(a);for(int i=0;i<len1;i++){ch1=a[0]-32;}cout<<ch1; }return 0; }

問(wèn)題17:買鉛筆

題目描述
P老師需要去商店買n支鉛筆作為小朋友們參加NOIP的禮物。她發(fā)現(xiàn)商店一共有3種包裝的鉛筆,不同包裝內(nèi)的鉛筆數(shù)量有可能不同,價(jià)格也有可能不同。為了公平起見(jiàn),P老師決定只買同一種包裝的鉛筆。
商店不允許將鉛筆的包裝拆開(kāi),因此P老師可能需要購(gòu)買超過(guò)n支鉛筆才夠給小朋友們發(fā)禮物。
現(xiàn)在P老師想知道,在商店每種包裝的數(shù)量都足夠的情況下,要買夠至少n支鉛筆最少需要花費(fèi)多少錢。
輸入
輸入的第一行包含一個(gè)正整數(shù)n,表示需要的鉛筆數(shù)量。接下來(lái)三行,每行用兩個(gè)正整數(shù)描述一種包裝的鉛筆:其中第一個(gè)整數(shù)表示這種包裝內(nèi)鉛筆的數(shù)量,第二個(gè)整數(shù)表示這種包裝的價(jià)格。保證所有的7個(gè)數(shù)都是不超過(guò)10000的正整數(shù)。
輸出
輸出一行一個(gè)整數(shù),表示P老師最少需要花費(fèi)的錢。
樣例輸入 Copy
57
2 2
50 30
30 27
樣例輸出 Copy
54
提示
鉛筆的三種包裝分別是:
·2支裝,價(jià)格為2;
·50支裝,價(jià)格為30;
·30支裝,價(jià)格為27。
P老師需要購(gòu)買至少_57支鉛筆。
如果她選擇購(gòu)買第一種包裝,那么她需要購(gòu)買29份,共計(jì)2×29=5 8支,需要花費(fèi)的錢為2×29=58。
實(shí)際上,P老師會(huì)選擇購(gòu)買第三種包裝,這樣需要買2份。雖然最后買到的鉛筆數(shù)量更多了,為30×2=60支,但花費(fèi)卻減少為27×2=_54,比第一種少。
對(duì)于第二種包裝,雖然每支鉛筆的價(jià)格是最低的,但要夠發(fā)必須買2份,實(shí)際的花費(fèi)達(dá)到了30×2=60,因此P老師也不會(huì)選擇。
所以最后輸出的答案是54。

#include<bits/stdc++.h> using namespace std; typedef long long ll; const int maxn=1e5+9; int main() {int n,a,b,sum=0;int max1=10000000;cin>>n;for(int i=0;i<3;i++){cin>>a>>b;if(n%a!=0){sum=(n/a+1)*b;if(sum<max1)max1=sum;}else{sum=(n/a)*b;if(sum<max1)max1=sum;}}cout<<max1<<'\n';return 0; }

注意本題不是貪心題哦!

問(wèn)題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.

#include<bits/stdc++.h> using namespace std; typedef long long ll; const int N=1e5; char a[N],b[N]; int main() {scanf("%s%s",a,b);/數(shù)太大要用char數(shù)組做int len1=strlen(a);int len2=strlen(b);if(len1<len2) cout<<"LESS";else if(len1>len2) cout<<"GREATER";else if(len1==len2){for(int i=0;i<len1;i++){if(a[i]>b[i]){cout<<"GREATER";break;}else if(a[i]<b[i]){cout<<"LESS";break;}else if(a[i]==b[i]){cout<<"EQUAL";break;}}}return 0; }

問(wèn)題19:回文日期

題目描述
在日常生活中,通過(guò)年、月、日這三個(gè)要素可以表示出一個(gè)唯一確定的日期。
牛牛習(xí)慣用8位數(shù)字表示一個(gè)日期,其中,前4位代表年份,接下來(lái)2位代表月份,最后2位代表日期。顯然:一個(gè)日期只有一種表示方法,而兩個(gè)不同的日期的表示方法不會(huì)相同。
牛牛認(rèn)為,一個(gè)日期是回文的,當(dāng)且僅當(dāng)表示這個(gè)日期的8位數(shù)字是回文的。現(xiàn)在,牛牛想知道:在他指定的兩個(gè)日期之間(包含這兩個(gè)日期本身),有多少個(gè)真實(shí)存在的日期是回文的。
【提示】
一個(gè)8位數(shù)字是回文的,當(dāng)且僅當(dāng)對(duì)于所有的i(1<i<8)從左向右數(shù)的第i個(gè)數(shù)字和第9-i個(gè)數(shù)字(即從右向左數(shù)的第i個(gè)數(shù)字)是相同的。
例如:
·對(duì)于2016年11月19日,用8位數(shù)字20161119表示,它不是回文的。
·對(duì)于2010年1月2日,用8位數(shù)字20100102表示,它是回文的。
·對(duì)于2010年10月2日,用8位數(shù)字20101002表示,它不是回文的。
每一年中都有12個(gè)月份: 其中,1, 3, 5, 7, 8, 10, 12月每個(gè)月有31天;4, 6, 9, 11月每個(gè)月有30天;而對(duì)于2月,閏年時(shí)有29天,平年時(shí)有28天。 一個(gè)年份是閏年當(dāng)且僅當(dāng)它滿足下列兩種情況其中的一種: 1.這個(gè)年份是4的整數(shù)倍,但不是100的整數(shù)倍; 2.這個(gè)年份是400的整數(shù)倍。
例如:
·以下幾個(gè)年份都是閏年:2000 , 2012 , 2016。
·以下幾個(gè)年份是平年:1900, 2011、2014。

輸入
輸入包括兩行,每行包括一個(gè)8位數(shù)字。
第一行表示牛牛指定的起始日期dates,第二行表示牛牛指定的終止日期date2。保證dates和date2都是真實(shí)存在的日期,且年份部分一定為4位數(shù)字,且首位數(shù)字不為0;保證dates一定不晚于date2。
輸出
輸出一行,包含一個(gè)整數(shù),表示在date1和date2之間,有多少個(gè)日期是回文的。
樣例輸入 Copy
20110101
20111231
樣例輸出 Copy
1
提示
對(duì)于樣例1,符合條件的日期是20111102。

#pragma GCC optimize(2) #pragma G++ optimize(2) #pragma GCC optimize(3) #pragma G++ optimize(3) #include<bits/stdc++.h> using namespace std; typedef long long ll; const int maxn=1e5+9; int s[13]={0,31,30,31,30,31,30,31,31,30,31,30,31}; int main() {int n,m,i,j,ans=0,sum;scanf("%d%d",&n,&m);for(i=1;i<=12;i++){for(j=1;j<=s[i];j++){int tem=(j%10)*1000+(j/10)*100+(i%10)*10+(i/10);sum=tem*10000+i*100+j;if(sum<n||sum>m)continue;ans++;} } cout<<ans<<'\n';return 0; }

總結(jié)

以上是生活随笔為你收集整理的中石油(某些代码)的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。

如果覺(jué)得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。