计蒜客/51Nod题目
題目
換位置(1)
鏈接https://nanti.jisuanke.com/t/T1971
思路:兩兩交換次序使得數(shù)組逆序,第一個(gè)到最后位置需要n-1步,第二個(gè)到倒數(shù)第二個(gè)需要n-2步,第三個(gè)到倒數(shù)第三個(gè)需要n-3步,即計(jì)算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; }題目
每一位相乘
你拿到一個(gè)整數(shù),卻忍不住想把每個(gè)位數(shù)都乘在一起。例如看到 356356 就會(huì)想要知道 3 \times 5 \times 63×5×6 的值為何。
快寫個(gè)程式幫幫為了乘數(shù)字而快發(fā)瘋的自己吧!
輸入格式
一開始有一個(gè)數(shù)字 T(1 ≤T≤100)\le T \le 100)≤T≤100)T(1≤T≤100),表示共有幾組測試數(shù)據(jù)。
接下來有 TT 個(gè)數(shù)字n(0≤n<2147483648)n (0 \le n < 2147483648)n(0≤n<2147483648) n(0≤n<2147483648)。
輸出格式
輸出可以拯救自己的結(jié)果。
輸出時(shí)每行末尾的多余空格,不影響答案正確性
分析
核心是取出每一位,并且相乘,多一個(gè)變量來控制
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;//取個(gè)位上數(shù)字if(tmp1==0){//其間如果有0result=0;break;}result*=tmp1;//相乘n/=10;//n減少位數(shù)tmp/=10;//循環(huán)變量}cout<<result<<endl;}}爬山
題目
蒜頭君和朋友們?nèi)ヅ老闵?#xff0c;為美麗的景色所陶醉,想合影留念。如果他們站成一排,男生全部在左(從拍照者的角度),并按照從矮到高的順序從左到右排,女生全部在右,并按照從高到矮的順序從左到右排,請(qǐng)問他們合影的效果是什么樣的(所有人的身高都不同)?
輸入格式
第一行是人數(shù) n(2 \le n \le 40n(2≤n≤40,且至少有 11 個(gè)男生和 11 個(gè)女生)。
后面緊跟 nn 行,每行輸入一個(gè)人的性別(男male或女female)和身高(范圍在 [0,2][0,2] 內(nèi)的浮點(diǎn)數(shù),單位米),兩個(gè)數(shù)據(jù)之間以空格分隔。
輸出格式
nn 個(gè)浮點(diǎn)數(shù),模擬站好隊(duì)后,拍照者眼中從左到右每個(gè)人的身高。每個(gè)浮點(diǎn)數(shù)需保留到小數(shù)點(diǎn)后 22 位,相鄰兩個(gè)數(shù)之間用單個(gè)空格隔開。
輸出時(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
題意:男生從小到大排序,女生從大到小排序,然后合并
代碼
補(bǔ)充知識(shí)
string類 函數(shù)
比較(compare)
語法:
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")來判斷時(shí)候含有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ù)的個(gè)數(shù)
超時(shí)代碼
題目
求1的個(gè)數(shù)
https://www.51nod.com/Challenge/Problem.html#problemId=1009
1009 數(shù)字1的數(shù)量
1.0 秒 131,072.0 KB 80 分 初學(xué)者5級(jí)題
給定一個(gè)十進(jìn)制正整數(shù)N,寫下從1開始,到N的所有正數(shù),計(jì)算出其中出現(xiàn)所有1的個(gè)數(shù)。
例如:n = 12,包含了5個(gè)1。1,10,12共包含3個(gè)1,11包含2個(gè)1,總共5個(gè)1。
輸入
輸入N(1 <= N <= 10^9)
輸出
輸出包含1的個(gè)數(shù)
輸入樣例
12
輸出樣例
5
總結(jié)
以上是生活随笔為你收集整理的计蒜客/51Nod题目的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 2021年开奶茶店怎样 创业之前一定要多
- 下一篇: 可用额度和当前余额