算法
qq_30272539
这个作者很懒,什么都没留下…
展开
-
47、打印杨辉三角
#include<iostream>#include<iomanip>using namespace std;int main(){ int a[11][11] = {0}; for (int i = 1; i < 11; i++) { a[i][i] = 1; //表示的是斜边全部是1 a[1][i] = 1;//第一列全部是 1; }...原创 2018-08-11 15:41:45 · 104 阅读 · 0 评论 -
34、fputs ,fgets , gets ,puts 函数的区别
1、fgets 函数和 fput 函数不同之处,在于fgets 读取到换行符号不会将其省去。而gets读到换行符号会退出,并将其省掉。2、fputs和puts不同之处,是fputs 并不打印换行符号。3、fgets(a,n,in) ;其中 a 是一个char 型 数组用于存放读取的字符串。n表示读取 1-n个字符。in 表示是文件读入指针。4、fgets 函数会在读入数据的末尾加入...原创 2018-08-01 10:39:08 · 450 阅读 · 0 评论 -
33、fread读取文件。
#include<iostream>#include<stdlib.h>using namespace std;int main(){ char a[100]; FILE * in= fopen("a.in","rb"); FILE * out= fopen("a.out","wb"); int n=fread(a,1,100,in);//原创 2018-08-01 08:09:51 · 1702 阅读 · 0 评论 -
32、fstream操作文件(c++)
#include<iostream>#include<fstream>using namespace std;int main(){ ifstream fin("in.txt"); ofstream fout("out.txt"); int a,b,c; fin>>a>>b>>c; fout原创 2018-08-01 08:09:42 · 140 阅读 · 0 评论 -
31、FLIE* 操作文件
#include<iostream>#include<cstdlib>using namespace std;int main(){ FILE * in=fopen("a.txt","r");//指针指向输入文件 FILE* out=fopen("b.txt","w");//指针指向输出文件 int temp[100]; int原创 2018-08-01 08:09:24 · 173 阅读 · 0 评论 -
30、利用 freopen 方式读写文件
/** 1、利用 freopen 方式读写文件 */ #include<iostream>#include<cstdlib>using namespace std;int a[1000];void readAndWrite(){ int i; freopen("sum.in","r",stdin); freopen("sum...原创 2018-08-01 08:09:11 · 1355 阅读 · 0 评论 -
29、n元钱的找零钱问题
#include<iostream>using namespace std;int main(){ int n,a,b,sum; //其中 a 为 10 元的个数 b 为 5 元的个数 cin>>n; //要找的零钱。 if(n%10>5){ //a 表示的是 等差数列的项。 a=n/10; ...原创 2018-08-01 08:09:02 · 246 阅读 · 0 评论 -
28、找零钱问题1
#include<iostream>using namespace std;/** 100=10x+5y+z; x>=1 && x<=9 y>=1 &&y<=18 z>=1 && z<=85 */ int main(){ for(int x=1;x&原创 2018-08-01 08:08:51 · 197 阅读 · 0 评论 -
27、完美数
#include<iostream>using namespace std;int main(){ int k0,k1,k2,k3,k4,k5,k6,k7,k8,k9; int i,j,n,s; for(j=2;j<=1000;j++){ n = 0; //因子保存的位置 ...原创 2018-07-31 14:51:09 · 193 阅读 · 0 评论 -
26、防护罩问题
//防护罩#include <iostream>#include <math.h> //须加此头文件 using namespace std;int main(){ int N=0,i,temp; double r,l; cin>>r; l=floor(r);//获得组数 for(i=1;i<=l;i++) { ...原创 2018-07-31 14:19:48 · 420 阅读 · 0 评论 -
35、getc 和putc 表示的都是读取一个字符。
#include<iostream>using namespace std;int main(){ FILE * in=fopen("i1.txt","rb"); FILE * out=fopen("i3.txt","wb"); char ch; while((ch=getc(in))!=EOF){ //当遇到文件的末尾 会 显示 EOF ...原创 2018-08-01 11:27:27 · 271 阅读 · 0 评论 -
36、读取一行包含空格的数据
#include<iostream>#include<fstream>using namespace std;void str(){ //读取一行数据放入字符串中 string str; getline(cin,str); cout<<str; system("pause"); } void str2(){ //读取一行...原创 2018-08-01 18:13:51 · 386 阅读 · 0 评论 -
37、计算用的时间
#include<iostream>#include<Windows.h> using namespace std;int main(){ clock_t time; time=clock(); cout<<time<<endl; Sleep(1000); cout<<clock(...原创 2018-08-01 18:27:21 · 112 阅读 · 0 评论 -
46、求矩阵的最大值
#include<iostream>using namespace std;int main(){ int a[3][4] = { { 1, 3, 4, 6 }, { 2, 4, 6, 8 }, { 1, 3, 4, 5 } }; int max = a[0][0]; int row = 0; int colnum = 0; for (int i = 0; i...原创 2018-08-11 15:25:41 · 556 阅读 · 0 评论 -
45、矩阵的行列互换
#include<iostream>using namespace std;int main(){ int a[2][3] = { { 1, 2, 3 }, { 3, 4, 5 } }; int b[3][2]; cout << "a 数组的元素值" << endl; for (int i = 0; i < 2; i++){ ...原创 2018-08-11 11:27:16 · 3461 阅读 · 0 评论 -
44、求分数的精确值
#include<iostream>using namespace std;/*1、求分数的精确值? 问题描述:精确计算两个100之间的整数相除。 如果是有限小数,直接输出。如果是无限循环,则输出循环的起始和结束位置。解题思路:1.利用一个数组 quotient[]存放商的数组。2、然后将余数乘以10,然后计算商的下一位。重复操作。3、当余数某次为 0 的...原创 2018-08-11 11:16:43 · 460 阅读 · 0 评论 -
43、神秘图案
问题描述:神秘图案 外圈 有 10个 按钮,编号为 1-10 ; 破解神秘图案的方法:第一次按第1个按钮; 第二次按第 3个按钮;第三次按第6个按钮 直到按了1000 下,最后没有按的那个按钮。 思路:我们可以定义一个数组 有 10个元素 ;数组中的每一个元素,相当于一个按钮。初始化时,均为0. 表示没有按这个按钮。 然后循环1000次,凡是按过的按钮 标记为 1;最后把没...原创 2018-08-11 10:18:08 · 481 阅读 · 0 评论 -
42、统计不同数据的个数
#include<iostream>using namespace std;int main(){ FILE * in = fopen("in.txt", "rb"); FILE * out = fopen("out.txt", "wb"); int temp = 0; int b[21] = { 0 }; int sum = 0; while (!feof(in...原创 2018-08-11 09:53:53 · 1201 阅读 · 0 评论 -
41、冒泡排序改进版
#include<iostream>using namespace std;#define _CRT_SECURE_NO_WARNINGS int main(){ freopen("sort.in","r",stdin); freopen("sort.out", "w", stdout); int n = 0; cin >> n; int a[100原创 2018-08-11 09:28:16 · 224 阅读 · 0 评论 -
40、求fibonacci 数列
#include<iostream>using namespace std;int main(){ int f[20]={1,2}; for(int i=2;i<20;i++){ f[i]=f[i-1]+f[i-2]; } for(int j=0;j<20;j++){ cout<&l...原创 2018-08-02 21:36:46 · 168 阅读 · 0 评论 -
39、求中间数的四种方法
#include<iostream>#include<cstdlib>#include<fstream> using namespace std;int main2(){ int a,b,c; freopen("min.in","rb",stdin); freopen("mid.out","wb",stdout原创 2018-08-02 21:36:27 · 5937 阅读 · 0 评论 -
38、产品随机数的方法
#include<iostream>using namespace std;# define n 100int a[101];void p(){ srand((unsigned)time(NULL)); //获取随机数种子 rand()产品 0-32767 之间的随机数 for(int i=0;i<=n;i++){ a[i]=rand()%10...原创 2018-08-02 21:36:00 · 168 阅读 · 0 评论 -
25、判断一个数是不是质数
#include<iostream>#include<math.h>using namespace std;int main(){ int num; cin>>num; int i; int k=(int)sqrt(num); for(i=2;i<=k;i++){ if(num%i==0)...原创 2018-07-31 11:22:13 · 534 阅读 · 0 评论 -
24、打印菱形
#include<iostream>#include<math.h>using namespace std;void print(){ for(int i=-3;i<=3;i++){ int k=abs(i); for(int j=0;j<k;j++){ //表示的是打印空格 ...原创 2018-07-31 11:10:50 · 92 阅读 · 0 评论 -
23、执行任务
#include<iostream>using namespace std;/** 1、a和b两个人至少去一个。 a+b>=1 2、a和d不能够一起去。 a+d!=2 3、a e f 三个人要派两个人去 a+e+f=2 4、bc 都去或者都不去。 b+c==2||b+c==0 5、cd 至少去一个。 c+d=1...原创 2018-07-31 10:34:44 · 1602 阅读 · 0 评论 -
10、求圆周率
#include<iostream>#include<math.h>using namespace std;int main(){ float n=1.0; //表示的是分母 float t=1.0,ans=0; //t为循环需要加的项 ans=表示的是整个表达式的值 int s=1; ...原创 2018-07-25 12:23:38 · 238 阅读 · 0 评论 -
9、整除猜想。
#include<iostream>using namespace std;int main(){ int num; cin>>num; while(num>1){ if(num%2==0){ //如果是偶数 就除以 2 num=num/2; }e...原创 2018-07-25 12:00:52 · 283 阅读 · 0 评论 -
8、火柴游戏
#include<iostream>using namespace std;int main(){ int num=21; int man; while(num>=1){ cout<<"男人取出的火柴数"; cin>>man; if(man>=1&&man<=4)..原创 2018-07-24 23:21:24 · 579 阅读 · 0 评论 -
7、加密操作
#include<iostream>using namespace std;int main(){ char c; while((c=getchar()!='\n')){ if(c>='a'&&c<='z'||c>='A'&&c<='Z'){ c=c+4;...原创 2018-07-24 23:11:41 · 98 阅读 · 0 评论 -
6、求15 的阶乘
#include<iostream>using namespace std;int main(){ //1.计算阶乘 int sum=1; int i=1; while(i<=15){ sum*=i; i++; } cout<<sum<<endl; system...原创 2018-07-24 22:56:55 · 4736 阅读 · 0 评论 -
5、判断今天是一年中的多少天
#include<iostream>using namespace std;int main(){ int day,month,year,sum,leap; cout<<"请输入年,月,日\n"; cin>>year>>month>>day; switch(month){ case 1:sum=0;...原创 2018-07-24 22:50:04 · 258 阅读 · 0 评论 -
4、判断是不是闰年
//求判断是不是闰年 #include <iostream>#include <iomanip>using namespace std;int main(){ int year; cin>>year; if((year%400==0)||(year%100!=0&&year%4==0)) cout<<...原创 2018-07-23 23:30:42 · 165 阅读 · 0 评论 -
3、求保留两位有效小数,四舍五入。
//小数的四舍五入 #include <iostream>#include <iomanip>using namespace std;int main(){ double x; cout<<"请输入一个双精度数"; cin>>x; x=(int)(x*100+0.5);//去到小数部分 x/=100; cout...原创 2018-07-23 23:04:48 · 342 阅读 · 0 评论 -
2、求三角形的面积算法
我们已经知道三角形的三条边a,b,c;利用海伦公式,我们可以求出它的面积。 p=(a+b+c)/2 S面积=sqrt(p*(p-a)*(p-b)*(p-c)) 就可以求得。//求三角形面积#include <iostream>#include <math.h>//调用平方根函数需要数学库头文件using namespace std;int main(...原创 2018-07-23 22:55:25 · 1371 阅读 · 0 评论 -
11、求两个数的最大公约数和最小公倍数。
#include<iostream>#include<math.h>using namespace std;int main(){ int a,b; cin>>a>>b; int m=a; int n=b; int c; if(a<b){ int temp=a; ...原创 2018-07-25 12:36:16 · 252 阅读 · 0 评论 -
12、求S=a+aa+aaa+aaaa+……
//求S=a+aa+aaa+aaaa+……#include <iostream>using namespace std;int main(){ int a,n,count=1,sn=0,tn=0; cin>>a>>n; for(int i=0;i<n;i++) { tn=tn+a; //表示的是第i项 sn...原创 2018-07-25 12:51:57 · 645 阅读 · 0 评论 -
13、埃及分数和
#include<iostream>using namespace std;int main(){ long int a,b,c; cin>>a>>b;//输入分子分母 while(1){ if(b%a!=0){ //如果分子不能够被分母整除 c=b/a+1; //分解出一个分...原创 2018-07-30 21:01:37 · 260 阅读 · 0 评论 -
22、打印九九乘法表
#include<iostream>using namespace std;/** 打印九九乘法表 */int main(){ for(int i=1;i<=9;i++){ for(int j=1;j<=i;j++){ cout<<j<<"*"<<i<<原创 2018-07-31 10:02:21 · 158 阅读 · 0 评论 -
21、计算圆的面积
#include<iostream>using namespace std;/** 计算面积大于100 的半径为 1-10 的圆。 */int main(){ float ares; for(int i=1;i<=10;i++){ ares=3.14*i*i; if(ares>100){...原创 2018-07-31 09:55:09 · 390 阅读 · 0 评论 -
20、求分子系列和问题
#include<iostream>using namespace std;/** 求 2/1 ,3/2 ,5/3 8/5 ... 之和. */int main(){ float a=2,b=1,ans=0; for(int i=1;i<=20;i++){ ans=ans+a/b; a=a+b; b=...原创 2018-07-31 09:47:00 · 98 阅读 · 0 评论