復(fù)習(xí)回顧c語言刷題Day01-創(chuàng)新互聯(lián)

n的階乘

#include

網(wǎng)站建設(shè)哪家好,找創(chuàng)新互聯(lián)建站!專注于網(wǎng)頁設(shè)計、網(wǎng)站建設(shè)、微信開發(fā)、小程序開發(fā)、集團企業(yè)網(wǎng)站建設(shè)等服務(wù)項目。為回饋新老客戶創(chuàng)新互聯(lián)還提供了吉林免費建站歡迎大家使用!

using namespace std;

int fact (int n){
?int res = 1;
?for(int i = 1; i<= n; i++){
??? ?res *= i;
?}
?return res;
}

int main(){
?int n;
?cin >>n;
?
?cout<< fact(n)<< endl;
?
?return 0;
}

x和y的大值

#include

using namespace std;

int max (int x, int y){
?if(x >y)return x;
?else return y;
}

int main(){
?int x, y;
?cin >>x >>y;
?
?cout<< max(x, y)<< endl;
?
?return 0;
}

求解a和b的大公約數(shù)

#include

using namespace std;

int gcd (int a, int b){
?for(int i =1000; i; i-- ){
??? ?if(a % i == 0 && b % i == 0 )
??? ?return i;
??? ?}
??? ?return 0;
?}

int main(){
?int a, b;
?cin >>a >>b;
?
?cout<< gcd(a, b)<< endl;
?
?return 0;
}

交換a和b的值

#include

using namespace std;

void swap(int &x, int &y){
?if(x == y)return ;
?int t = x;
?x = y;
?y = t;
}
int main(){
?int x, y;
?cin >>x >>y;
?
?swap(x, y);
?
?cout<< x<< " "<< y<< endl;
?
?return 0;
}

打印數(shù)字

輸入一個長度為?n?的數(shù)組?a?和一個整數(shù)?size,請你編寫一個函數(shù),?void print(int a[], int size), 打印數(shù)組?a?中的前?size?個數(shù)。

輸入格式

第一行包含兩個整數(shù)?n和?size。

第二行包含?n?個整數(shù)?a[i],表示整個數(shù)組。

輸出格式

共一行,包含?size個整數(shù),表示數(shù)組的前?size個數(shù)。

數(shù)據(jù)范圍

1≤n≤1000
1≤size≤n

#include

using namespace std;

const int N = 1000;

void print(int a[], int size){
?for(int i = 0; i< size; i++){
??? ?cout<< a[i]<< " " ;
?}
?cout<< endl;
}
int main(){
?int n, size;
?int a[N];
?
?cin >>n >>size;
?
?for(int i = 0; i< n; i++)cin >>a[i];?
?
?print(a, size);
?
?return 0;
}

打印矩陣

#include

using namespace std;

void print2D(int a[][100], int row, int col){
?for(int i = 0; i< row; i++){
??? ?for(int j = 0 ; j< col; j++){
??? ??? ?cout<< a[i][j]<< " ";
??? ?}
??? ?cout<< endl;
?}
}
int main(){
?int row, col;
?
?int a[100][100];
?
?cin >>row >>col;
?for(int i = 0; i< row; i++){
??? ?for(int j = 0; j< col; j++){
??? ??? ?cin >>a[i][j];
??? ?}
?}?
?print2D(a, row, col);
?
?return 0;
}

你是否還在尋找穩(wěn)定的海外服務(wù)器提供商?創(chuàng)新互聯(lián)www.cdcxhl.cn海外機房具備T級流量清洗系統(tǒng)配攻擊溯源,準(zhǔn)確流量調(diào)度確保服務(wù)器高可用性,企業(yè)級服務(wù)器適合批量采購,新人活動首月15元起,快前往官網(wǎng)查看詳情吧

網(wǎng)頁名稱:復(fù)習(xí)回顧c語言刷題Day01-創(chuàng)新互聯(lián)
網(wǎng)頁網(wǎng)址:http://bm7419.com/article4/didcie.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供微信小程序、定制開發(fā)網(wǎng)站建設(shè)網(wǎng)頁設(shè)計公司、電子商務(wù)、網(wǎng)站排名

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)

網(wǎng)站優(yōu)化排名