CPP----C++练习100题
CPP----Q100
1.從鍵盤讀入2個整數(shù),分別代表一個長方形的長和寬,請計算長方形的周長和面積; #include <bits/stdc++.h> using namespace std; int main(){int chang,kuan;cin>>chang>>kuan;cout<<(chang+kuan)*2<<endl;cout<<(chang*kuan)<<endl;return 0; }
2.已知一個圓的半徑,求解該圓的面積和周長
#include <bits/stdc++.h> using namespace std; int main(){const double pi = 3.1415926; double circle;cin>>circle; // cout<<pi*(circle*circle)<<endl; // cout<<(pi*circle)*2<<endl;printf("%.2lf\n",pi*circle*circle);printf("%.2lf",pi*circle*2);return 0; }3.有一塊n \times nn×n(n≥5n≥5,且 nn 是奇數(shù))的紅玫瑰花圃,由 n \times nn×n 個小正方形花圃組成,現(xiàn)要求在花圃中最中間的一行、最中間的一列以及 44 個頂點(diǎn)處種植粉色玫瑰,請問粉玫瑰占地面積占整個玫瑰花圃總面積的百分比是多少?
#include <bits/stdc++.h> using namespace std; int main(){int n;cin >> n;int s = n*n;int pink_area = 2 * n + 3; // cout << s << pink_area<<endl;printf("%.1f%%",pink_area*1.0/s*100);return 0; }4.編一程序,將攝氏溫度換為華氏溫度。公式為:f=9/5*c+32f=9/5?c+32 。其中 ff 為華氏溫度, cc 是攝氏溫度。
#include <bits/stdc++.h> using namespace std; int main(){int n;cin >> n;double f = 9*1.0/5* n + 32;printf("%.2f",f);return 0; }5.任意讀入一個四位整數(shù),顛倒后輸出。
#include <bits/stdc++.h> using namespace std; int main(){int num,g,s,b,q;cin >> num;g = num%10;s = num/10%10;b = num/100%10;q = num/1000;printf("%d",1000*g+100*s+10*b+q);return 0; }6.對于一個任意的三位自然數(shù) xx ,編程計算其各個數(shù)位上的數(shù)字之和 SS 。
#include <bits/stdc++.h> using namespace std; int main(){int num,g,s,b;cin >> num;g = num%10;s = num/10%10;b = num/100%10;printf("%d",g+s+b);return 0; }7.某軍事單位用 44 位整數(shù)來傳遞信息,傳遞之前要求先對這個 44 位數(shù)進(jìn)行加密。加密的方式是每一位都先加上 55 然后對 1010 取余數(shù),再將得到的新數(shù)顛倒過來。
#include <bits/stdc++.h> using namespace std; int main(){int num,g,s,b,q;cin >> num;g = (num%10+5)%10;s = (num/10%10+5)%10;b = (num/100%10+5)%10;q = (num/1000+5)%10; printf("%d",1000*g+100*s+10*b+q);return 0; }8.輸入一個整數(shù),判斷是否為偶數(shù)。是輸出 y e s ,否則輸出n o。
#include <bits/stdc++.h> using namespace std; int main(){int n;cin >> n;if (n % 2 == 0)printf("y e s");elseprintf("n o");return 0; }9 有 AA,BB 兩個不相等的數(shù),請將其中較大數(shù)打印出來。
#include <bits/stdc++.h> using namespace std; int main(){int m,n;cin >> m >> n;if (m >= n)cout<< m <<endl;elsecout << n << endl;return 0; }10 請從鍵盤讀入一個三位整數(shù),判斷這個三位整數(shù)是否是對稱數(shù)(對稱數(shù)指的是,這個數(shù)正過來和倒過來是同一個數(shù),比如:121、686、808121、686、808 等數(shù)都是對稱數(shù)),如果是對稱數(shù),則輸出“Y” ,否則輸出“N” 。(請注意字母的大小寫)。
#include <bits/stdc++.h> using namespace std; int main(){int m;cin >> m;int g = m %10;int s = m/10%10;int b = m/100; // cout << g << endl << b<< endl; // if (m == 100*g+s*10+b)if (g == b)cout<< "Y";elsecout << "N";return 0; }11 請從鍵盤讀入一個六位整數(shù),判斷這個六位整數(shù)是否是對稱數(shù)(對稱數(shù)指的是,這個數(shù)正過來和倒過來是同一個數(shù)。
#include <bits/stdc++.h> using namespace std; int main(){int m;cin >> m;int g = m %10;int s = m/10%10;int b = m/100%10;int q = m/1000%10;int w = m/10000%10;int sw = m/100000; // cout << g << endl << b<< endl; // if (m == 100*g+s*10+b)if (g == sw and s == w and b == q)cout<< "Y";elsecout << "N";return 0; }12 超市有一門公共電話,收費(fèi)標(biāo)準(zhǔn)是,如果通話時間在 1010 分鐘內(nèi),那么按照 1.5元/1.5元/ 分鐘收費(fèi),如果通話時間超過 1010 分鐘(含 1010 分鐘)按照 1.2元/1.2元/ 分鐘收費(fèi)
#include <bits/stdc++.h> using namespace std; int main(){int m;cin >> m;if (m < 10)printf("%.1lf",(m*1.5));elseprintf("%.1lf",(m*1.2));return 0; }13 輸入三個整數(shù),表示3條線段的長度,判斷這三條線段能否構(gòu)成三角形。能構(gòu)成就輸出 Yes ,否則輸出No
#include <bits/stdc++.h> using namespace std; int main(){int m,n,x;cin >> m >> n>>x;if (m + x > n and m+n >x and n+x>m)cout<< "Yes" <<endl;elsecout << "No" << endl;return 0; }14 已知電水箱的容量為 n 升( n≤10L ),同學(xué)們帶的杯子平均容量為 xx 毫升( x在100~300 之間),請問燒一箱開水,最多能倒多少杯(不足 1 杯算 1 杯)。
#include <bits/stdc++.h> using namespace std; int main(){int n,x;cin >> n >> x; // cout << n*1000*1.0/x;printf("%.0lf",ceil(n*1000.0/x));return 0; }15 已知有三個不等的數(shù),將其中的最大數(shù)找出來。
#include <bits/stdc++.h> using namespace std; int main(){int a,b,c;cin >> a >> b >> c;int max = a;if (max < b) max =b;if (max < c) max =c;cout << max << endl;return 0; }16 已知有四個不等的數(shù),將其中的最大數(shù)找出來。
#include <bits/stdc++.h> #include <climits.h> using namespace std; int main(){int q,w,e,r,max = INT_MIN;cin >> q>>w>>e>>r;if(max<q) max =q;if(max<w) max =w;if(max<e) max =e;if(max<r) max =r;cout<<max<<endl;return 0; }17 在社會實(shí)踐活動中有三項(xiàng)任務(wù)分別是:種樹、采茶、送水。依據(jù)小組人數(shù)及男生、女生人數(shù)決定小組的接受任務(wù),人數(shù)小于 10 人的小組負(fù)責(zé)送水(輸出 water),人數(shù)大于等于 10 人且男生多于女生的小組負(fù)責(zé)種樹(輸出 tree),人數(shù)大于等于 10 人且男生不多于女生的小組負(fù)責(zé)采茶(輸出tea)。輸入小組男生人數(shù)、女生人數(shù),輸出小組接受的任務(wù)。
#include <bits/stdc++.h> using namespace std; int main(){int b,g,t=0;cin >> b>>g;int all = b + g;if (all < 10) cout<<"water"<<endl;else if (all >= 10 and b > g) cout << "tree" <<endl;else if (all >= 10 and b <= g) cout <<"tea" <<endl;return 0; }18 輸入年份,判斷是否為閏年。如果是,則輸出 yes ,否則輸出 no 。
#include <bits/stdc++.h> using namespace std; int main(){int year;cin >> year;if ((year%4==0 and year%100!=0) or (year%400==0)) cout<<"yes"<<endl;else cout << "no" <<endl;return 0; }19 從鍵盤讀入一個整數(shù) n ,請循環(huán)輸出 1~n 之間所有的整數(shù),每行輸出 1 個
#include <bits/stdc++.h> using namespace std; int main(){int n;cin >> n;for(int i=1; i<n+1; i++) cout<< i <<endl;return 0; }20 請輸出所有的 3 位對稱數(shù),對稱數(shù)指的是一個整數(shù) n正過來和倒過來是一樣的。
#include <bits/stdc++.h> using namespace std; int main(){for (int i=100; i<1000; i++){int b = i/100;int s = i/10%10;int g = i%10;if(b == g) cout<< i <<endl;}return 0; }21 有一個數(shù)列,該數(shù)列的前 4 個數(shù)是: 1 4 7 10 ;
請從鍵盤讀入一個正整數(shù) n ,請通過觀察前 4 項(xiàng)的規(guī)律,輸出1~n 之間所有滿足該規(guī)律的數(shù)字。
22 輸出 1~n 中含有數(shù)字 3 或者含有數(shù)字 5 ,且因數(shù)有 2 (即能被 2 整除)的所有整數(shù)。( n<1000)
#include <bits/stdc++.h> using namespace std; //int main(){ // int n; // cin >> n; // for(int i=1; i<=n; i++){ // int g = i %10; // int s = i/10%10; // int b = i/100%10; // cout << b << endl; // if (g%2==0 and(s==3 or s ==5 or b ==3 or b==5)) // cout << i <<endl; // } //}int main(){int n ,t;cin >> n;for (int i = 1; i <= n; i++){bool flag = false;t =i;while(t!=0){if(t%10==3 || t%10==5){flag = true;break;}t /= 10;}if(flag && i %2 == 0) cout << i << endl;}return 0; }23 所謂水仙花數(shù),就是指各位數(shù)字立方之和等于該數(shù)的數(shù);a3 稱為 a 的立方,即等于 a×a×a 的值。
#include <bits/stdc++.h> using namespace std; int main(){for(int i=100; i<=999; i++){int g = i %10;int s = i/10%10;int b = i/100%10;if(i == (g*g*g + s*s*s +b*b*b))cout << i <<endl;} }24 問 555555 的約數(shù)中最大的三位數(shù)是多少?
#include <bits/stdc++.h> using namespace std; int main(){int max;for(int i=100; i<=999; i++){if(555555%i == 0) max = i;}cout << max <<endl;return 0; }25 找出所有 3 位的既是回文數(shù),又是偶數(shù)的數(shù)
#include <bits/stdc++.h> using namespace std; int main(){for(int i=100; i<=999; i++){int g = i%10;int b = i/100%10;if(i%2 == 0 && g ==b) cout << i <<endl;}return 0; }26 如果連續(xù) 2 位的差值相等,就是等差數(shù)
#include <bits/stdc++.h> using namespace std; int main(){int m,n;cin >> m >> n;for(int i = m; i <= n; i++){int g = i%10;int s = i/10%10;int b = i/100%10;int q = i/1000%10;if (q == 0){if(s-g==b-s) cout << i << endl;}else{if (q-b==b-s && b-s==s-g) cout << i <<endl;}} }27 請輸出 1~n 中至少能夠被 2、3、5、7 中兩個及兩個以上的數(shù)整除的數(shù)?
#include <bits/stdc++.h> using namespace std; int main(){int n;cin >> n;for(int i = 1; i <= n; i++){int flag = 0;if(i%2==0) flag++;if(i%3==0) flag++;if(i%5==0) flag++;if(i%7==0) flag++;if(flag>=2) cout << i<< endl; } }28 編程求 1+1/2+1/3+?+1/n1+1/2+1/3+?+1/n 。
#include <bits/stdc++.h> using namespace std; int main(){int n;double sum=0;cin >> n;for(int i = 1; i <= n; i++){sum += 1.0/i;}printf("%.3lf\n",sum); }29 請問 100~n中連續(xù)遞增或者連續(xù)遞減的 3位數(shù)有總和是多少,有多少個?
#include <bits/stdc++.h> using namespace std; int main(){int n,flag=0,sum=0;cin >> n ;for(int i = 100; i <= n; i++){int g = i%10;int s = i/10%10;int b = i/100;if(g <s && s <b || g>s && s>b){sum+=i;flag++;} }cout << sum << endl << flag; }30 請打印n行的數(shù)字倒直角三角形。
#include <bits/stdc++.h> using namespace std; int main(){int n;cin >> n ;for(int i = 1; i <= n; i++){for(int j =1; j <= n-i+1; j++){cout << n-i+1;}cout << endl;}return 0; }31 輸入一個整數(shù)打印平行四邊形。
#include <bits/stdc++.h> using namespace std; int main(){int n;cin >> n; //里面的東西要流出去for (int i =1; i<= n; i++){for (int j=1; j<=i-1; j++) cout<<" ";for (int k=1; k<=n; k++) cout<<"*";cout <<endl;} return 0; }32 輸入一個整數(shù)打印數(shù)字三角。
#include <bits/stdc++.h> using namespace std; int main(){int n;cin >> n;for (int i =1; i<= n; i++){for (int j=1; j<=n-i; j++) cout<<" ";for (int k=1; k<=2*i-1; k++) cout<<k;for (int j=1; j<=n-i; j++) cout<<" ";cout <<endl;} return 0; }33 輸入數(shù)字形成數(shù)字矩形
#include <bits/stdc++.h> using namespace std; int main(){int n;cin >> n;for (int i =1; i<= n; i++){for (int j=1; j<=n; j++) cout<<j;cout <<endl;} return 0; }34 打印n行的空心正方形。
#include <bits/stdc++.h> using namespace std; int main(){int n;cin >> n;for (int i =1; i<= n; i++){if(i==1 || i==n){for(int j=1; j<=n; j++) cout<<"*";cout << endl;}else{cout<<"*";for(int j=1;j<=n-2;j++){cout<<" "; }cout<<"*" <<endl;}} return 0; }35 打印乘法表
#include <bits/stdc++.h> using namespace std; int main(){int n;cin >> n;for (int i =1; i<= n; i++){for (int j=1; j<=i; j++) cout<< i<<"*"<<j<<"="<<j*i<<" ";cout <<endl;} return 0; }36 輸入一個正整數(shù)n,求1!-2!+3!-4!+……+N!的結(jié)果。
#include <bits/stdc++.h> using namespace std; int main(){int n,sum=0,s = 1;cin >> n;for (int i=1; i <= n; i++){s *= i;if(i%2==0) sum += s*(-1);else sum += s;}cout << sum <<endl;return 0; }37 韓信有一對士兵,他想知道有多少人,他就讓士兵報數(shù),如果按照 1 到 5 報數(shù),最末一個士兵報的數(shù)為 1 。按照 1 到 6 報數(shù),最末一個士兵報的數(shù)為 5 。按照 1 到 7 報數(shù),最末一個士兵報的數(shù)為 4 。最后再按 1 到11 報數(shù),最末一個士兵報的數(shù)為 10 。請問韓信這隊(duì)士兵最少有多少人?
#include <bits/stdc++.h> using namespace std; int main(){for (int i=1;; i++){if(i%5==1 and i%6==5 and i%7==4 and i%11==10){cout << i << endl;break;} }return 0; }38 小球從100米高處自由落下,著地后又彈回高度的一半再落下。經(jīng)過多少次落地后,小球彈起的高度才會低于0.5 米?
#include <bits/stdc++.h> using namespace std; int main(){double s =100.0;int times = 0;while (s >= 0.5){s *= 0.5;times++; }cout << times <<endl;return 0; }39 任給一個自然數(shù),若為偶數(shù)則除以 2 ,若為奇數(shù)則乘 3 加 1 ,得到一個新的自然數(shù)后按上面的法則繼續(xù)演算。若干次后得到的結(jié)果必為 1 。
#include <bits/stdc++.h> using namespace std; int main(){int n,times = 0;cin >> n;while (n != 1){if (n%2==0) n /= 2;else n = n*3+1;times++;}cout << times <<endl;return 0; }40 編寫一個程序,從接收到的數(shù)字 n 中獲取這 2 個數(shù)字信息
#include <bits/stdc++.h> using namespace std; int main(){int n,times = 0;cin >> n;int sum = 0;while (n!= 0){if (n % 10 % 2 == 0) sum += n%10;n /= 10;times++;}cout << times <<" " << sum;return 0; }41 試計算在區(qū)間1到n的所有整數(shù)中,數(shù)字x(0≤x≤9)共出現(xiàn)了多少次
#include <bits/stdc++.h> using namespace std; int main(){int n,x,times = 0;cin >> n >>x;for(int i=1; i<=n; i++){int temp = i;while (temp!= 0){if (temp % 10 == x){times++; }temp /= 10;}}cout << times <<endl;return 0; }42 給出一個正整數(shù) n ( 1≤n≤10000 ),求出 1,2,…,n之中(包括 1 和 n )的回文數(shù)的個數(shù)
#include <bits/stdc++.h> using namespace std; int main(){int n,times = 0;cin >> n ;for(int i=1; i<=n; i++){int s=0,temp = i;while (temp!= 0){int t = temp%10;s = s*10+t; temp /= 10;}if (s == i) times++;}cout << times << endl;return 0; }43 輸入一個正整數(shù),判斷它是否為質(zhì)數(shù),如是質(zhì)數(shù)則輸出Yes,否則輸出這個數(shù)的大于 1 的最小的約數(shù)。
#include <bits/stdc++.h> using namespace std; int main(){int n;cin >> n ;if (n==2) cout << "Yes" <<endl;for(int i=2; i<n; i++){if (n % i == 0){cout << i <<endl;break;}else if (i==n-1)cout << "Yes" <<endl;}return 0; } **``` 44 請編程計算出 1~N 之間(包括 N )的全部同構(gòu)數(shù)有多少個?**```cpp #include <bits/stdc++.h> using namespace std;int main(){int n,nums=0;cin >> n ;for(int i=1; i<=n; i++){int temp=i*i,x =1;while(temp!=0){x *=10;int s = i*i % x ;temp /= 10;if (s == i) nums++; }}cout << nums <<endl;return 0; }45 任意輸入一正整數(shù) N ,求出它的所有質(zhì)因子。
#include <bits/stdc++.h> using namespace std;int main(){int n,i=2;cin >> n ;while(n!=1){if(n%i==0){cout << i <<endl;n /= i;}else{i++;}}return 0; }46 從鍵盤輸入整數(shù)L,統(tǒng)計出邊長為整數(shù),周長為 L 的不是等邊三角形的個數(shù)
#include <bits/stdc++.h> using namespace std; int main(){int L,times=0;cin >> L;for (int i=1; i < L; i++){for(int j=i; j<L; j++){for(int k=j; k<L; k++){if (!(i==j && j==k) && (i+j+k==L) && (i+j >k)) {//cout <<i <<" " <<j <<" " <<k <<endl;times++;}}}}cout << times << endl; return 0; }47 輸入 n( 1≤n≤5000 )個正整數(shù),每個數(shù)都在 1到20000 之間;要求對這 n 個數(shù)中的奇數(shù)和偶數(shù)分別求和。
#include <bits/stdc++.h> using namespace std; const int maxn = 5010; int a[maxn]; int main(){int n;cin >>n;int o=0,j=0;for (int i=1; i <= n; i++){cin >> a[i];if (a[i] % 2==0) o+=a[i];else j+=a[i];}cout << j <<endl <<o <<endl; return 0; }48 陶陶摘蘋果
包括兩行數(shù)據(jù)。
第一行包含 10 個 100 到 200之間(包括 100和 200 )的整數(shù)(以厘米為單位)分別表示 10 個蘋果到地面的高度,兩個相鄰的整數(shù)之間用一個空格隔開。第二行只包括一個 100 到 120 之間(包含 100和 120 )的整數(shù)(以厘米為單位),表示陶陶把手伸直的時候能夠達(dá)到的最大高度。
49 任意輸入 n 個整數(shù),把它們的最大值,最小值求出來。
#include <bits/stdc++.h> using namespace std; const int maxn = 100; int n,MAX=INT_MIN,MIN=INT_MAX; int a[maxn]; int main() {cin >> n;for (int i=1; i <= n; i++){cin >> a[i];}for(int i=1; i <= n; i++){MAX = max(MAX, a[i]);MIN = min(MIN, a[i]);}cout << MAX <<" " <<MIN<<endl;return 0; }50 編一個程序,計算 1~n 范圍內(nèi)素數(shù)的個數(shù)。
#include <bits/stdc++.h> using namespace std; int x; bool is_prime(int n){if(n<=1) return false;for(int i=2;i<sqrt(n);i++){if(n%i==0) return false;}return true; } int main() {while(true){cin >>x;if(x==0) break;int total = 0;for (int i=1; i <= x; i++){if (is_prime(i)){total++;}}cout << total<<endl; }return 0; }51 從鍵盤輸入一個整數(shù) N ,輸入 N (5≤N≤20)個元素,輸入一個整數(shù),判斷這個數(shù)組中最后一次出現(xiàn)該數(shù)的位置,如果沒有請輸出?1。
#include <bits/stdc++.h> using namespace std; const int maxn=100; int n,x; bool flag=false; int a[maxn]; int main(){cin >> n;for (int i=1;i <= n; i++){cin >> a[i];}cin >> x;for (int i=n;i>=1; i--){if(a[i] == x ){flag =true;cout << i << endl; break;}}if (flag == false){cout << -1 <<endl;}return 0;}52 數(shù)組元素的移動,把數(shù)組的第 x 個位置的元素先保存起來,然后把 x+1 到 n 的元素,依次往前移一位,最后原來的第 x 個位置的元素放在最后。
#include <bits/stdc++.h> using namespace std; const int maxn = 100; int n,x,temp; int a[maxn]; int main(){cin >>n;for (int i=1; i<=n; i++) cin >> a[i];cin >> x;temp = a[1];for(int i=1; i<=n-1; i++){a[i] = a[i+1];}a[n] = temp;for(int i=1; i <= n; i++) cout << a[i] << " ";return 0;}53 在一個數(shù)組的第 x 個位置插入一個新的數(shù)y。
#include <bits/stdc++.h> using namespace std; const int maxn = 100; int n,x,y; int a[maxn]; int main(){cin >>n;for (int i=1; i<=n; i++) cin >> a[i];cin >> x;cin >> y;for(int i=n+1; i>=x+1; i--){a[i] = a[i-1];}a[x] = y;for(int i=1; i <= n+1; i++) cout << a[i] << " ";return 0; }54 把一個數(shù)組的第 x 個位置的元素刪除掉。
#include <bits/stdc++.h> using namespace std; const int maxn = 100; int n,x,y; int a[maxn]; int main(){cin >>n;for (int i=1; i<=n; i++) cin >> a[i];cin >> x;for(int i=x; i<=n; i++){a[i] = a[i+1];}for(int i=1; i <= n-1; i++) cout << a[i] << " ";return 0; }55 對數(shù)組元素從大到小排序
#include <bits/stdc++.h> using namespace std; const int maxn = 100; int n; int a[maxn]; int main(){cin >>n;for (int i=1; i<=n; i++) cin >> a[i];for(int i=1; i<=n; i++){for(int j =1; j<=n-i;j++){if(a[j] > a[j+1]){swap(a[j], a[j+1]);}}}for(int i=1; i <= n; i++) cout << a[i] << " ";return 0; }56 輸入 n 個不超過 30000 的整數(shù)(n≤10)。然后求出每個數(shù)的數(shù)字和,再按每個數(shù)的數(shù)字和由小到大排列輸出。
#include <bits/stdc++.h> using namespace std; const int maxn = 100; int n,x; int a[maxn]; int main(){cin >> n;for (int i=1;i<=n;i++){int total =0;cin >> x;while(x!=0){total += x%10;x/=10;}a[i] = total;}sort(a+1, a+n+1);for(int i =1; i<=n; i++) cout << a[i] <<" ";return 0; }57 給定一個長度為 N ( 0<n≤10000)的序列,保證每一個序列中的數(shù)字 a[i] 是正整數(shù) ,編程要求求出整個序列中第 k 大的數(shù)字減去第 k 小的數(shù)字的值 m ,并判斷 m 是否為質(zhì)數(shù)。(0<k≤n)
#include <bits/stdc++.h> using namespace std; const int maxn = 10000; int n,k; int a[maxn]; bool is_prime(int x){if(x<1) return false;for(int i=2; i<=sqrt(x); i++){if(x%i==0) return false;}return true; } int main(){cin >> n >> k;for (int i=1;i<=n;i++) cin >>a[i];sort(a+1,a+n+1);int p1 = a[n-k+1];int p2 = a[k];int m = p1-p2;if (is_prime(m)) cout << "YES" <<endl;else cout << "NO" << endl;cout << m <<endl;return 0; }58 給你 N 個數(shù)(N≤100),每個數(shù)都在(0~1000)之間,其中由很多重復(fù)的數(shù)字,請將重復(fù)的數(shù)字只保留一個,并將剩下的數(shù)由小到大排序并輸出。
#include <bits/stdc++.h> using namespace std; const int maxn = 1010; int n,x,cnt; int a[maxn]; int main(){scanf("%d", &n);for(int i=1; i<=n; i++){scanf("%d",&x);a[x]=1;}for (int i=1;i<=maxn;i++){if(a[i] == 1) cnt++;}for(int i=1;i<=maxn;i++){if(a[i]==1){printf("%d\n",i); }} }59 小明從一副撲克牌中(沒有大小王, J 認(rèn)為是數(shù)字 11 , Q 是 12 , K 是 13 , A 是 1 )抽出 2 張牌求和,請問能夠組合出多少個不相等的數(shù),按照由小到大輸出這些數(shù)。
#include <bits/stdc++.h> using namespace std;60 給出一個正整數(shù) n ( 2≤n≤1000000 ),求連續(xù)非素數(shù)的最大長度
在這里插入代碼片61 有一對兔子,從出生后第 3 個月起每個月都生一對兔子,一對小兔子長到第三個月后每個月又生一對兔子,假如兔子都不死。問第 n 個月( n≤50 )的兔子總數(shù)為多少對?
在這里插入代碼片62 求 S=1+2+4+7+11+16…的值剛好大于等于5000 時 S 的值。
在這里插入代碼片63 漢諾塔的問題大家都已經(jīng)很熟悉了,有三個柱子,每個柱子上有一些大小不一的金片,要把金片從 A 柱移動到 C 柱,可以借助 B 柱,請問 n 個金片的情況下,需要最少移動多少次?
在這里插入代碼片64 郭遠(yuǎn)有一天走到了一片蘋果林,里面每顆樹上都結(jié)有不同數(shù)目的蘋果,郭遠(yuǎn)身上只能拿同一棵樹上的蘋果,他每到一棵果樹前都會把自己身上的蘋果扔掉并摘下他所在樹上的蘋果并帶走(假設(shè)郭遠(yuǎn)會走過每一棵蘋果樹),問在郭遠(yuǎn)摘蘋果的整個過程中,他身上攜帶的最多蘋果數(shù)與最小蘋果數(shù)的差是多少?
在這里插入代碼片65 在一個 n 行 m 列的矩陣王國中,生活著一些整數(shù),其中一些是素數(shù),一些不是素數(shù)。如果一個素數(shù)的上下左右、左上、右上、左下、右下相鄰的數(shù)中都沒有素數(shù),我們就認(rèn)為這是一個孤獨(dú)的素數(shù)。
在這里插入代碼片66
掃雷游戲是一款十分經(jīng)典的單機(jī)小游戲。在 n 行 m 列的雷區(qū)中有一些格子含有地雷(稱之為地雷格),其他格子不含地雷(稱之為非地雷格)。玩家翻開一個非地雷格時,該格將會出現(xiàn)一個數(shù)字——提示周圍格子中有多少個是地雷格。游戲的目標(biāo)是在不翻出任何地雷格的條件下,找出所有的非地雷格。
現(xiàn)在給出 n 行 m 列的雷區(qū)中的地雷分布,要求計算出每個非地雷格周圍的地雷格數(shù)。注:一個格子的周圍格子包括其上、下、左、右、左上、右上、左下、右下八個方向上與之直接相鄰的格子。
67 爬梯子
在這里插入代碼片總結(jié)
以上是生活随笔為你收集整理的CPP----C++练习100题的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 谈谈浏览器中富文本编辑器的技术演进
- 下一篇: Java之List系列--ArrayLi