C++的一些简单示例
来源:互联网 发布:按键精灵淘宝秒杀脚本 编辑:程序博客网 时间:2024/06/05 14:00
1、使用选择排序法对一组数据进行排序
#include <iostream>using namespace std;int main(){ int array[4]; int min; int swap; int i = 0; cout<<"please input the values!"<<endl;//使用for循环输入数据到数组中/* for(int i=0;i<4;i++){ cin >> array[i]; }*///使用while循环输入数据到数组中 while(cin.get() != '\n' && i<4){ cin >> array[i]; i++; } for(int k=0;k<4;k++){ min = k; for(int j=k+1;j<4;j++){ if(array[min] > array[j]){ min = j; } } if(min != k){ swap = array[k]; array[k] = array[min]; array[min] = swap; } } for(int t=0;t<4;t++){ cout << array[t] << " "; } cout << endl; return 0;}
2、从后往前判断两个数组相同数据的个数
#include <iostream>using namespace std;int main(){ int a1[] = {0,5,78,45,9}; int a2[] = {78,45,9}; int len1 = (sizeof(a1))/(sizeof(int)); int len2 = (sizeof(a2))/(sizeof(int)); int count = 0; while(len1 > 0 && len2 > 0){ if(a1[len1-1] == a2[len2-1]){ count++; } len1--; len2--; } cout << "从后往前a1数组和a2数组相同的数据有:" << count << "个" << endl; return 0;}
3、对一组字符按从小到大的顺序排序
#include <iostream>using namespace std;int main(){ char array[] = {'y','o','a','s','t','u','e','n','m','f'}; int len = (sizeof(array))/(sizeof(char)); int min; char ch; for(int i=0;i<len;i++){ min = i; for(int j=i+1;j<len;j++){ if(array[j] < array[min]){ min = j; } } if(i != min){ ch = array[i]; array[i] = array[min]; array[min] = ch; } } for(int k=0;k<len;k++){ cout << array[k] << " "; } cout << endl; return 0;}
4、求10000以内的完数
#include <iostream>using namespace std;int main(){ for(int i=2;i<=10000;i=i+2){ int sum = 1; for(int j=2;j<=i/2;j++){ if(i%j == 0){ sum = sum + j; } } if(sum == i){ cout << i << " "; } } cout << endl; return 0;}
5、求三位数对称素数
#include <iostream>#include <cmath>using namespace std;bool isPrime(int n);int main(){ int count = 0; for(int i=100;i<=999;i++){ if(i%10 == i/100 && isPrime(i)){ cout << i << " "; count++; } if(count == 10){ cout << endl; count = 0; } } return 0;}bool isPrime(int n){ for(int i=2;i<=sqrt(n*1.0);i++){ if(n%i == 0){ return false; } return true; }}
6、求两个数的最大公约数和最小公倍数
#include <iostream>using namespace std;int main(){ int a,b,c; int temp; cout << "请输入两个整数!" << endl; cin >> a >> b; int a1 = a; int b1 = b; if(a == 0 || b == 0){ cout << "错误,请重新输入数据!" << endl; exit(0); } if(a<b){ temp = a; a = b; b = temp; } while(b){ c = a%b; a = b; b = c; } cout << "两个数的最大公约数是:" << a << endl; cout << "两个数的最小公倍数是:" << (a1*b1)/a << endl; return 0;}
7、矩阵的倒置
#include <iostream>using namespace std;int main(){ int array1[][3] = { { 1, 2, 3 }, { 4, 5, 6 }, { 7, 8, 9 } }; int array2[3][3]; cout << "倒置前:" << endl; for(int i=0;i<3;i++){ for(int j=0;j<3;j++){ cout << array1[i][j] << " "; array2[j][i] = array1[i][j]; } cout << endl; } cout << "倒置后:" << endl; for(int m=0;m<3;m++){ for(int n=0;n<3;n++){ cout << array2[m][n] << " "; } cout << endl; } return 0;}
8、判断一个字符串是否是回文数
#include <iostream>using namespace std;int main(){ char array[10]; cout << "请输入字符串!" << endl; gets(array); int flag = 0; int len = strlen(array); for(int i=0;i<=len/2;i++){ if(array[i] != array[len-1-i]){ flag = 1; break; } } if(flag == 0){ cout << "这个字符串是回文数!" << endl; }else{ cout << "这个字符串不是回文数!" << endl; } return 0;}
0 0
- Python的一些简单示例
- C++的一些简单示例
- c#webservice的简单示例
- c#webservice的简单示例
- c#webservice的简单示例
- C#webservice的简单示例
- c#webservice的简单示例
- c#webservice的简单示例
- c#webservice的简单示例
- c#webservice的简单示例
- c#webservice的简单示例
- Python的一些简单示例(2)
- Java的一些简单示例(1)
- Java的一些简单示例(2)
- C++的一些简单示例(2)
- C++的一些简单示例(3)
- C++的一些简单示例(4)
- C++的一些简单示例(5)
- android studio 的 Butter Knife:8.0.1的完整正确导入方法
- 浅谈Thrift内部实现原理
- 6. ZigZag Conversion
- android view 原理 -- measure 分析与应用
- c++实验6
- C++的一些简单示例
- C++学习笔记:转换构造函数与类型转换函数
- C# 压缩和解压文件
- 代码混淆 proguard相关配置过程记录
- 面试
- BP神经网络
- MyEclipse2015 Stable2.0 安装与破解
- https 出现host name not match 问题
- app后端架构