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

歡迎訪問 生活随笔!

生活随笔

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

编程问答

计蒜客/51Nod题目

發(fā)布時間:2025/4/5 编程问答 31 豆豆
生活随笔 收集整理的這篇文章主要介紹了 计蒜客/51Nod题目 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

題目
換位置(1)
鏈接https://nanti.jisuanke.com/t/T1971

思路:兩兩交換次序使得數(shù)組逆序,第一個到最后位置需要n-1步,第二個到倒數(shù)第二個需要n-2步,第三個到倒數(shù)第三個需要n-3步,即計算1+2+…+(n-2)+(n-1)= n(n?1)2\frac{n(n-1)}{2}2n(n?1)?

#include<iostream> using namespace std;int main(){int n;cin>>n;cout<<n*(n-1)/2<<endl; }

題目
每一位相乘
你拿到一個整數(shù),卻忍不住想把每個位數(shù)都乘在一起。例如看到 356356 就會想要知道 3 \times 5 \times 63×5×6 的值為何。

快寫個程式幫幫為了乘數(shù)字而快發(fā)瘋的自己吧!

輸入格式
一開始有一個數(shù)字 T(1 ≤T≤100)\le T \le 100)T100)T(1≤T≤100),表示共有幾組測試數(shù)據(jù)。

接下來有 TT 個數(shù)字n(0≤n<2147483648)n (0 \le n < 2147483648)n(0n<2147483648) n(0≤n<2147483648)。

輸出格式
輸出可以拯救自己的結(jié)果。

輸出時每行末尾的多余空格,不影響答案正確性

分析
核心是取出每一位,并且相乘,多一個變量來控制

while(tmp){tmp1=n%10;//取個位上數(shù)字result*=tmp1;//相乘n/=10;//n減少位數(shù)tmp/=10;//循環(huán)變量}

AC代碼

#include<iostream> #include<cstdio> using namespace std; int T; long long tmp,n,tmp1; int main(){cin>>T;while(T--){cin>>n;if(n==0){cout<<0<<endl;continue;}long long result=1;tmp=n; while(tmp){tmp1=n%10;//取個位上數(shù)字if(tmp1==0){//其間如果有0result=0;break;}result*=tmp1;//相乘n/=10;//n減少位數(shù)tmp/=10;//循環(huán)變量}cout<<result<<endl;}}

爬山
題目
蒜頭君和朋友們?nèi)ヅ老闵?#xff0c;為美麗的景色所陶醉,想合影留念。如果他們站成一排,男生全部在左(從拍照者的角度),并按照從矮到高的順序從左到右排,女生全部在右,并按照從高到矮的順序從左到右排,請問他們合影的效果是什么樣的(所有人的身高都不同)?

輸入格式
第一行是人數(shù) n(2 \le n \le 40n(2≤n≤40,且至少有 11 個男生和 11 個女生)。

后面緊跟 nn 行,每行輸入一個人的性別(男male或女female)和身高(范圍在 [0,2][0,2] 內(nèi)的浮點數(shù),單位米),兩個數(shù)據(jù)之間以空格分隔。

輸出格式
nn 個浮點數(shù),模擬站好隊后,拍照者眼中從左到右每個人的身高。每個浮點數(shù)需保留到小數(shù)點后 22 位,相鄰兩個數(shù)之間用單個空格隔開。

輸出時每行末尾的多余空格,不影響答案正確性

樣例輸入
6
male 1.72
male 1.78
female 1.61
male 1.65
female 1.70
female 1.56
樣例輸出
1.65 1.72 1.78 1.70 1.61 1.56
鏈接https://vjudge.net/contest/370363#problem/E
題意:男生從小到大排序,女生從大到小排序,然后合并
代碼

#include<iostream> #include<cstdio> #include<string> #include<iomanip> #include<vector> #include<algorithm> using namespace std;int n; string s; float h; vector<float> m,f;int main(){cin>>n;for(int i=0;i<n;i++){cin>>s>>h;if(s.compare("female")==0)f.push_back(h);else m.push_back(h); }sort(m.begin(),m.end());sort(f.begin(),f.end(),greater<float>());for(int i=0;i<f.size();i++){m.push_back(f[i]);}for(int i=0;i<m.size();i++){printf("%.2f ",m[i]); //cout<<setprecision(3)<<m[i]<<" ";}}

補充知識
string類 函數(shù)
比較(compare)
語法:

int compare( const basic_string &str );int compare( const char *str );int compare( size_type index, size_type length, const basic_string &str );int compare( size_type index, size_type length, const basic_string &str, size_type index2,size_type length2 );int compare( size_type index, size_type length, const char *str, size_type length2 );

compare()函數(shù)以多種方式比較本字符串和str,返回:

返回值 情況
小于零 this < str
零 this == str
大于零 this > str

不同的函數(shù):

1.比較自己和str,
2.比較自己的子串和str,子串以index索引開始,長度為length
3.比較自己的子串和str的子串,其中index2和length2引用str,index和length引用自己
4.比較自己的子串和str的子串,其中str的子串以索引0開始,長度為length2,自己的子串以index開始,長度為length

本題使用s.compare("female")==0來判斷s是否是字符串female
或者使用s.find("female")來判斷時候含有female子串

find函數(shù)
find()函數(shù):
返回str在字符串中第一次出現(xiàn)的位置(從index開始查找)。如果沒找到則返回string::npos,

題目
Given a number N, you are asked to count the number of integers between A and B inclusive which are relatively prime to N.
Two integers are said to be co-prime or relatively prime if they have no common positive divisors other than 1 or, equivalently, if their greatest common divisor is 1. The number 1 is relatively prime to every integer.
Input
The first line on input contains T (0 < T <= 100) the number of test cases, each of the next T lines contains three integers A, B, N where (1 <= A <= B <= 10 15) and (1 <=N <= 10 9).
Output
For each test case, print the number of integers between A and B inclusive which are relatively prime to N. Follow the output format below.
Sample Input
2
1 10 2
3 15 5
Sample Output
Case #1: 5
Case #2: 10

Hint
In the first test case, the five integers in range [1,10] which are relatively prime to 2 are {1,3,5,7,9}.
題目大意:給出[a,b]區(qū)間內(nèi)和N互質(zhì)的數(shù)的個數(shù)
超時代碼

#include<iostream> #include<cstdio> #include<string> #include<iomanip> #include<vector> #include<algorithm> using namespace std;int T; long long a,b,n;int main(){cin>>T;while(T--){cin>>a>>b>>n;if(b<n){cout<<0<<endl;continue;}int count=0;for(long long i=a;i<=b;i++){if(__gcd(i,n)==1)count++;}cout<<count<<endl;}}

題目
求1的個數(shù)
https://www.51nod.com/Challenge/Problem.html#problemId=1009

1009 數(shù)字1的數(shù)量
1.0 秒 131,072.0 KB 80 分 初學(xué)者5級題
給定一個十進制正整數(shù)N,寫下從1開始,到N的所有正數(shù),計算出其中出現(xiàn)所有1的個數(shù)。

例如:n = 12,包含了5個1。1,10,12共包含3個1,11包含2個1,總共5個1。

輸入
輸入N(1 <= N <= 10^9)
輸出
輸出包含1的個數(shù)
輸入樣例
12
輸出樣例
5

總結(jié)

以上是生活随笔為你收集整理的计蒜客/51Nod题目的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

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