1.解二元一次方程

#include <iostream> #include <math.h> using namespace std; int main() { float a,b,c,x1,x2,disc; cin >>a>>b>>c; if(fabs(a) <= 1e-6) cout << "不是二元一次方程"<<endl; else { disc = b*b-4*a*c; if(disc < 0) cout <<"方程没有实根"<<endl; else { float q = sqrt(disc)/(2.0*a); float p = -b/(2.0*a); if(disc == 0) cout <<"方程有两个相等根:"<<"x1=x2="<<p+q<<endl; else cout <<"方程有两个不等根:"<<"x1="<<p+q<<" x2="<<p-q<<endl; } } system("pause"); return 0; }
执行结果:
2.判断闰年

#include <iostream> #include <math.h> using namespace std; int main() { int year; cin >>year; if(year%400 ==0) cout <<"是闰年"<<endl; else { if(year%100 != 0 && year%4 == 0) cout <<"是闰年"<<endl; else cout <<"不是闰年"<<endl; } system("pause"); return 0; }
3.输入年月日去计算是该年中的第几天

#include <iostream> #include <math.h> using namespace std; int main() { int year,month,date,sum=0; bool Is_Leap_Year = false; cin >>year>>month>>date; if((year%400==0)||(year%4==0&&year%100!=0)) Is_Leap_Year = true; switch(--month) { case 11:date+=30; case 10:date+=31; case 9:date+=30; case 8:date+=31; case 7:date+=31; case 6:date+=30; case 5:date+=31; case 4:date+=30; case 3:date+=31; case 2:date+=28; case 1:date+=31; } if((month <= 1) || !Is_Leap_Year) cout <<date<<endl; else cout <<date+1<<endl; system("pause"); return 0; }
4.火柴游戏
一共21根火柴,两人轮流取,每人可取1-4根,不可不取,不可多取,谁取到最后一根谁输,假设A先取,B后取,如何编程让B赢?

#include <iostream> #include <time.h> using namespace std; int main() { int num = 21,a,b,turn=1; bool is_End = false; while(!is_End) { cout <<"Round "<<turn<<":"<<endl; cout <<"matches number:"<<num<<endl; if(num!=1) { a = rand()%4 + 1; num -= a; cout <<"Now A get "<<a<<"matches!"<<endl; b = 5 - a; num -= b; cout <<"Now B get "<<b<<"matches!"<<endl<<endl; } else { a = num; num -= 1; cout <<"Now A get "<<a<<"matches!"<<endl; cout <<"Now matches number is "<<num<<"!B win!"<<endl; is_End = true; } turn++; } system("pause"); return 0; }
执行结果:
5.对于任意给定的大于1的一个正整数,如果它是一个偶数请将其除以2,若是奇数请将其乘以3加1,对其运算结果,如果它不是1,则重复上述操作经过,最后总会得到1.

#include <iostream> using namespace std; int main() { int n; cin >>n; if(n > 0) { while(n!=1) { if(n%2==0) n /= 2; else n = 3*n + 1; } cout <<n<<endl; } else cout <<"n is negaive numebr."<<endl; system("pause"); return 0; }
6.求圆周率
用π/4=1-1/3+1/5-1/7+...公式求π的近似值,直到某一项的绝对值小于10^-6次方为止.

#include <iostream> #include <math.h> using namespace std; int main() { float div = 1.0f,sum=0.0f,b=1.0; bool is_change = true; while(fabs(div) > 10e-6) { b += 2.0; div = 1/b; if(is_change) { //cout <<div<<b<<" "<<is_change<<endl; sum += div; is_change = false; } else { //cout <<b<<" "<<is_change<<endl; sum -= div; is_change = true; } } cout <<4*sum<<endl; system("pause"); return 0; }
7.求最小公倍数和最大公约数
输入两个正整数a和b,利用辗转相除法求出这两个数的最小公倍数和最大公约数

#include <iostream> using namespace std; int main() { int a,b,mod,t; cin >>a>>b; t = b; mod = a%b; while(mod) { a = b; b = mod; mod = a%b; } cout <<"gcd:"<<b<<endl; cout <<"lcm:"<<a*t/b<<endl; system("pause"); return 0; }
8.Sn=a+aa+aaa+aaaa+...,这里的a、n是一个数字,例a=3,n=4,则S4=3+33+333+3333.

#include <iostream> #include <math.h> using namespace std; int main() { long a,n,sum=0,p,temp=0; cin >>a>>n; while(n) { p = n; while(p >= 0) { temp += a*pow(10,p-1); p--; } sum += temp; temp = 0; n--; } cout <<sum<<endl; system("pause"); return 0; }
9.古埃及分数
参考页面:https://zh.wikipedia.org/wiki/%E5%8F%A4%E5%9F%83%E5%8F%8A%E5%88%86%E6%95%B8

#include <iostream> using namespace std; int main() { //a表示分子,b表示分母 long int a = 0,b = 0,c = 0; cin >>a>>b; while(1) { if(b%a != 0) //不能整除 { c = b/a +1; } else //能够整除 { c = b/a; a = 1; } if(a == 1) { cout <<"1/"<<c; break; } else { cout <<"1/"<<c<<"+"; } a = a*c-b; b = b*c; if(a == 3) { cout <<"1/"<<b/2<<"+"<<"1/"<<b; break; } } system("pause"); return 0; }
10.简易计算系统

#include <iostream> #include <math.h> #include <iomanip> using namespace std; int main() { //简易小学生算术系统 int choice; float a,b; system("cls"); cout <<"简易小学生算术系统"<<endl; cout <<" 1.加法"<<endl; cout <<" 2.减法"<<endl; cout <<" 3.乘法"<<endl; cout <<" 4.除法"<<endl; cout <<" 5.退出"<<endl; cout <<"请选择<1-5>:"<<endl; cin >>choice; if(choice != 5) { cout <<"请输入变数a,b:"; cin >>a>>b; switch(choice) { case 1: cout <<a<<"+"<<b<<"="<<a+b<<endl; break; case 2: cout <<a<<"-"<<b<<"="<<a-b<<endl; break; case 3: cout <<a<<"*"<<b<<"="<<a*b<<endl; break; case 4: if(a) { cout <<a<<"/"<<b<<"="<<a/b<<endl; break; } else cout <<"a的值为"<<a<<"无法进行除法运算"<<endl; } } else{ cout <<"谢谢使用!"<<endl; } system("pause"); return 0; }
11.计算1^2+2^2+3^2+...50^2的值

#include <iostream> using namespace std; int main() { long int a,sum = 0; for(a = 1;a <= 50;a++) sum += a*a; cout <<"1^2+2^2+3^2+...+50^2="<<sum<<endl; system("pause"); return 0; }
12.打印ASCII15-127的字符

#include <iostream> using namespace std; int main() { char c; int lineBreak=0; cout <<" ASCII15-127的字符"<<endl; for(c = 15;c <= 127;c++,lineBreak++) { if(!(lineBreak%10)) cout <<endl; cout <<c<<" "; } system("pause"); return 0; }
13.求2-10000的同构数

#include <iostream> #include <iomanip> using namespace std; int get_length(int x) //计算x的位数 { int length = 0; while(x) { x /= 10; length++; } return length; } bool is_IsomorphicNumber(int x,int x_length,int y) //判断是否为同构数 { int i = 0; while(i <= x_length-1) { if(x%10 != y%10) return false; x /= 10; y /= 10; i++; } return true; } int main() { cout <<"2-10000的同构数:"<<endl; int num,lineBreak = 0; for(num = 2;num <= 10000;num++) { int squre_num = num*num; int num_length = get_length(num); if(is_IsomorphicNumber(num,num_length,squre_num)) { if(!(lineBreak%3)) cout <<endl; cout <<setw(8)<<num; lineBreak++; } } cout <<endl; system("pause"); return 0; }
14.计算e的值前一百项e=1+1/1!+1/2!+1/3!+...1/99!

#include <iostream> #include <iomanip> using namespace std; int main() { double tmp = 1,n=1,t,ans=1; for(n = 1;n<100;n++) { t = (1/n)*(tmp); tmp = t; ans += tmp; } cout <<ans<<endl; system("pause"); return 0; }
15.斐波那契数列兔子问题

#include <iostream> #include <iomanip> using namespace std; int main() { int f1=1,f2=1,t,ans=2; for(int i = 2;i<=40;i++) { t = f1+f2; f1 = f2; f2 = t; ans += t; } cout <<ans<<endl; }
16.求分子序列和:2/1,3/2,5/3,8/5,13/8,21/13,...,求出这个数列前20项的和

#include <iostream> #include <iomanip> using namespace std; int main() { float a=2.0, b=1.0, ans=0.0, t; for(int i = 1;i <= 20;i++) { ans = ans + a/b; t = b; b = a; a = a + t; } cout <<ans<<endl; system("pause"); return 0; }
17.六个人A、B、C、D、E、F尽可能多的挑人去执行任务,但有限制条件:①A和B两人至少去一人②A和D不能一起去③A、E和F三人中要派两人去④B和C都去或都不去⑤C和D两人中去一个⑥若D不去,则E也不去

#include <iostream> #include <iomanip> using namespace std; int main() { for(int a = 1;a>=0;a--) for(int b = 1;b>=0;b--) for(int c = 1;c>=0;c--) for(int d = 1;d>=0;d--) for(int e = 1;e>=0;e--) for(int f = 1;f>=0;f--) if((a+b>=1)&&(a+d!=2)&&(a+e+f==2)&&(b+c==0||b+c==2)&&(c+d==1)&&(d+e==0||d==1)) { cout <<"a:"<<a<<endl; cout <<"b:"<<b<<endl; cout <<"c:"<<c<<endl; cout <<"d:"<<d<<endl; cout <<"e:"<<e<<endl; cout <<"f:"<<f<<endl; } system("pause"); return 0; }
18.打印菱形(一)

#include <iostream> #include <iomanip> using namespace std; int main() { for(int i=-3;i<=3;i++) { int k = abs(i); for(int j = k;j > 0;j--) cout <<" "; for(int j = 1;j <= 7 - 2*k;j++) cout <<"*"; cout <<endl; } system("pause"); return 0; }
19.打印菱形(二)

#include <iostream> #include <iomanip> using namespace std; int main() { for(int i=-3,t=1;i<=3;i++,t++) { int k = abs(i); for(int j = k;j > 0;j--) cout <<" "; for(int j = 1;j <= 7 - 2*k;j++) { if(t==1 || t==7) cout <<"4"; else if(t== 2 || t==6) cout <<"3"; else if(t== 3 || t ==5) cout <<"2"; else if(t==4) cout <<"1"; } cout <<endl; } system("pause"); return 0; }
20.打印菱形(三)

#include<iostream> #include<stdlib.h> using namespace std; int main(){ for(int i = -3; i <= 3; i++){ int k = abs(i); for(int j = 0; j < k; j++){ cout<<" "; } for(int j = 0; j < 7-2*k; j++ ){ cout<<abs(3-k-j)+1; } cout<<endl; } system("pause"); return 0; }
21.判断10-500的素数

#include <iostream> #include <math.h> #include <iomanip> using namespace std; int main() { bool flag = true; int lineBreak = 0; for(int i = 10;i <= 500;i++) { int k = sqrt(i); for(int j = 2;j <= k;j++) { if(i%2 == 0) { flag = false; break; } if(i%j == 0) { flag = false; break; } } if(flag) { if(!(lineBreak%5)) cout <<endl; cout <<setw(5)<<i; lineBreak++; } flag = true; } system("pause"); return 0; }
22.找1-10000以内的完美数

#include <iostream> #include <math.h> #include <iomanip> using namespace std; int main() { for(int num = 1;num <=10000;num++) { if(num%2) //完美數不会是奇数,排除掉 continue; int sum = 1; int k = sqrt(num); //把数开根号 for(int i = 2;i<= k;i++) { if(num % i == 0) sum = sum + i + num / i; } if(sum == num) cout <<num<<"是完美數!"<<endl; } system("pause"); return 0; }
23.冒泡排序

#include <iostream> #include <time.h> using namespace std; int a[10]; int main() { srand((unsigned)time(NULL)); for(int i = 0;i <= 10;i++) a[i] = rand()%100; cout <<"原阵列:"<<endl; for(int i = 0;i <= 10;i++) cout <<a[i]<<endl; cout <<endl; //冒泡排序法大到小 for(int i = 0;i <= 9;i++) { for(int j = 0;j <= 9 - i;j++) { if(a[j] > a[j + 1]) { int t = a[j]; a[j] = a[j + 1]; a[j + 1] = t; } } } cout <<"排序后:"<<endl; for(int i = 0;i <= 10;i++) cout <<a[i]<<endl; cout <<endl; system("pause"); return 0; }
24.杨辉三角

#include <iostream> #include <time.h> #include <iomanip> using namespace std; #define N 10 int main() { int a[N][N] = {0}; for(int i = 0;i < N;i++) { a[i][i] = 1; a[i][0] = 1; } for(int i = 2;i < N;i++) { for(int j = 1;j < i;j++) { a[i][j] = a[i - 1][j - 1] + a[i - 1][j]; } } for(int i= 0;i < N;i++) { for(int j = 0;j < N;j++) { cout <<setw(5)<<a[i][j]<<" "; } cout <<endl; } system("pause"); return 0; }
25.1-9数字排列成3*3矩阵,行列、对角线不重复,试输出所有可能性
方法一:穷举法

#include <iostream> using namespace std; int main() { for(int i = 123;i <= 987;i++) { for(int j = 123;j <= 987;j++) { for(int k = 123;k <= 987;k++) { int a1 = i/100%10,a2 = i/10%10,a3 = i%10; int b1 = j/100%10,b2 = j/10%10,b3 = j%10; int c1 = k/100%10,c2 = k/10%10,c3 = k%10; if((a1!=a2)&&(a1!=a3)&&(a1!=b1)&&(a1!=b2)&&(a1!=b3)&&(a1!=c1)&&(a1!=c2)&&(a1!=c3)&& (a2!=a3)&&(a2!=b1)&&(a2!=b2)&&(a2!=b3)&&(a2!=c1)&&(a2!=c2)&&(a2!=c3)&&(a3!=b1)&& (a3!=b2)&&(a3!=c1)&&(a3!=c2)&&(a3!=c3)&&(b1!=b2)&&(b1!=b3)&&(b1!=c1)&&(b1!=c2)&& (b1!=c3)&&(b2!=b3)&&(b2!=c1)&&(b2!=c2)&&(b2!=c3)&&(b3!=c1)&&(b3!=c2)&&(b3!=c3)&& (c1!=c2)&&(c1!=c3)&&(c2!=c3) &&(a1+a2+a3==15)&&(b1+b2+b3==15)&&(c1+c2+c3==15)&& (a1+b1+c1==15)&&(a2+b2+c2==15)&& (a3+b3+c3==15)&&(a1+b2+c3==15)&&(a3+b2+c1==15)) cout <<a1<<" "<<a2<<" "<<a3<<" "<<b1<<" "<<b2<<" "<<b3<<" "<<c1<<" "<<c2<<" "<<c3<<endl; } } } system("pause"); return 0;
方法二:筛选法

#include <iostream> using namespace std; int main() { int collect[50],n = 0; for(int i = 123;i <= 987;i++) { int a = i/100%10; int b = i/10%10; int c = i%10; if((a+b+c)==15&&(a!=b)&&(a!=c)&&(b!=c)&&(a!=0)&&(b!=0)&&(c!=0)) { collect[n] = i; n++; } } for(int i = 0;i < n;i++) { for(int j = i + 1;j < n;j++) { for(int k = i + 1;k < n;k++) { int a1=collect[i]/100%10,b1=collect[i]/10%10,c1=collect[i]%10; int a2=collect[j]/100%10,b2=collect[j]/10%10,c2=collect[j]%10; int a3=collect[k]/100%10,b3=collect[k]/10%10,c3=collect[k]%10; if((a1+a2+a3)==15&&(b1+b2+b3)==15&&(c1+c2+c3)==15&&(a1*a2*a3*b1*b2*b3*c1*c2*c3)==362880) { //考虑到对角线根据数i,j,k排列组合后有不同的样子 if((a1+b2+c3==15)&&(c1+b2+a3==15)) cout <<collect[i]<<endl<<collect[j]<<endl<<collect[k]<<endl<<endl; if((a2+b3+c1==15)&&(c2+b3+a1==15)) cout <<collect[j]<<endl<<collect[k]<<endl<<collect[i]<<endl<<endl; if((a1+b3+c2==15)&&(c1+b3+a2==15)) cout <<collect[i]<<endl<<collect[k]<<endl<<collect[j]<<endl<<endl; if((a2+b1+c3==15)&&(c2+b1+a1==15)) cout <<collect[j]<<endl<<collect[i]<<endl<<collect[k]<<endl<<endl; if((a3+b1+c2==15)&&(c3+b1+a2==15)) cout <<collect[k]<<endl<<collect[i]<<endl<<collect[j]<<endl<<endl; if((a3+b2+c1==15)&&(c3+b2+a1==15)) cout <<collect[k]<<endl<<collect[j]<<endl<<collect[i]<<endl<<endl; } } } } system("pause"); return 0; }
方法三:

#include <iostream> using namespace std; int a[3][3],b[3][3]; int main() { int x = 0,y = 1,x_,y_; a[0][1] = 1; for(int i = 2;i <= 9;i++) { x_ = (x + 2)%3; y_ = (y + 1)%3; if(a[x_][y_] == 0) { a[x_][y_] = i; x = x_; y = y_; } else { x = (x + 1)%3; a[x][y] = i; } } for(int q = 0;q <= 3;q++) { cout <<"第"<<2*q+1<<"个神奇矩阵:"<<endl; for(int i = 0;i < 3;i++) { for(int j = 0;j < 3;j++) { cout <<a[i][j]<<" "; } cout <<endl; } cout <<endl; cout <<"第"<<2*q+2<<"个神奇矩阵:"<<endl; for(int i = 2;i >=0;i--) { for(int j = 0;j < 3;j++) { cout <<a[i][j]<<" "; } cout <<endl; } cout <<endl; //矩阵翻转并赋值 for(int i = 0;i < 3;i++) for(int j = 0;j < 3;j++) { b[j][2 - i] = a[i][j]; } for(int i = 0;i < 3;i++) for(int j = 0;j < 3;j++) { a[i][j] = b[i][j]; } } system("pause"); return 0; }
26.蛇形矩阵

#include <iostream> #include <iomanip> using namespace std; int main() { int n; cin >>n; //n > 1- n < 100 //set n*n array const int row = n; const int col = n; int **a = new int*[row]; for(int i = 0;i < row;i++) a[i] = new int[col]; //set zero for(int i = 0;i < row;i++) for(int j = 0;j <col;j++) a[i][j] = 0; int x,y; int value = n*n; //do something x = n - 1; y = n - 1; while(value != 0) { for(int y_ = y;y_ > -1;y_--) { if(a[x][y_] == NULL) { a[x][y_] = value; value--; y = y_; } } x--; for(int x_ = x;x_ > -1;x_--) { if(a[x_][y] == NULL) { a[x_][y] = value; value--; x = x_; } } y++; for(int y_ = y;y_ < n;y_++) { if(a[x][y_] == NULL) { a[x][y_] = value; value--; y = y_; } } x++; for(int x_ = x;x_ < n;x_++) { if(a[x_][y] == NULL) { a[x_][y] = value; value--; x = x_; } } y--; } //print result int sum = 0; for(int i = 0;i < n;i++) { for(int j = 0;j <n;j++) { cout <<setw(5)<<a[i][j]<<" "; if(i==j || (i + j) == n - 1) sum += a[i][j]; } cout <<endl; } cout <<sum<<endl; system("pause"); return 0; }
27.打印1000以内水仙花数

#include <iostream> using namespace std; int main() { for(int i = 100;i < 1000;i++) //水仙花数必须是n>=3的数 { int c = i%10; int b = i/10%10; int a = i/100%10; if(i == (a*a*a+b*b*b+c*c*c)) cout <<i<<" "; } system("pause"); return 0; }
28.猴子吃桃

#include <iostream> using namespace std; int main() { int day = 10,peach = 1; while(day-- != 1) { peach = 2*(peach+1); cout <<"day"<<day<<":"<<endl; cout <<peach<<endl<<endl; } cout <<peach; system("pause"); return 0; }
29.魔法石分五次出售,第一次卖全部的一半加二分之一个,第二次卖出余下的三分之一加三分之一个,第三次卖出余下的四分之一加四分之一个,第四次卖出余下的五分之一加五分之一个,最后卖出十一个,求原来魔法石的数量?

#include <iostream> using namespace std; int main() { int magicStone = 11; int n = 5; cout <<"n = "<<n<<" magicStone:"<<magicStone<<endl; while(n--!=1) { magicStone = ((n+1)*magicStone+1)/n; cout <<"n = "<<n<<" magicStone:"<<magicStone<<endl; } system("pause"); return 0; }
30.排名次,需考虑同名次的情况

#include <iostream> using namespace std; int main() { int *a,N; cin >>N; a = new int[N]; for(int i = 0;i < N;i++) cin >>a[i]; //do something for(int i = 0;i < N;i++) { int r = 1; for(int j = 0;j < N;j++) { if(i == j) continue; if(a[i] < a[j]) r++; } cout <<a[i]<<" "<<r<<endl; } system("pause"); return 0; }