|
[讨论]DEV C++中初学者编程 共同学习者顶起来
再回来一下 顶起来 |
|
[讨论]DEV C++中初学者编程 共同学习者顶起来
一个假期又过 新的一期来临 上来继续 |
|
[讨论]DEV C++中初学者编程 共同学习者顶起来
# include "stdio.h" main() { FILE *fp; fp=fopen("in.txt","r"); char N[12]; int i=0; if(fp==NULL) {printf("Cannot open the file, press any key to continue...");goto dd;} for(i=0;i<12;i++) { N[i]=fgetc(fp); printf("%c",N[i]); if(feof(fp)) goto dd; } FILE *fo; fo=fopen("out.txt","w"); for(i=0;i<12;i++) { fputc(N[i],fo); printf("%c",N[i]); } dd:; fclose(fp); getchar(); } |
|
[讨论]DEV C++中初学者编程 共同学习者顶起来
小家伙 欠。。。。 freopen fopen 两个函数都可用来打开文件,也可以指定打开文件的属性, 不过看了例子后,你们会觉得freopen好用点儿。 准备工作:in.txt 包含:3 5 7 2 1 6 out.txt为空文本文件 与file.c放在同一目录下。 例: # include "stdio.h" main() { FILE *fp,fo; fp=freopen("in.txt","r",stdin); freopen("out.txt","w",stdout); char N[12]; int i=0; if(fp==NULL) {printf("Cannot open the file, press any key to continue...");goto dd;} for(i=0;i<12;i++) { N[i]=fgetc(fp); printf("%c",N[i]); if(feof(fp)) goto dd; } printf("test%c",N[0]); dd:; fclose(fp); getchar(); } |
|
[讨论]DEV C++中初学者编程 共同学习者顶起来
上课了 今天了解文件 |
|
[讨论]DEV C++中初学者编程 共同学习者顶起来
#include "stdio.h" #include "string.h" main() { int n,i,a=0; char n1[]="123456789abcdefgh ",n2[100]="1"; char *p1=n1; while(*p1!='\0') {p1++;a++;} printf("%d\n",a=a-1); scanf("%d\n",&n); if(n<a) { p1=&n1[n-1]; for(i=n;i<=a;i++){n2[i-n]=*p1;p1++;} } else printf("Your n too big!\n"); printf(n1); printf("\n"); printf(n2); fflush(stdin); getchar(); } 谁能帮忙解释一下: 为什么scanf只一句 运行时却要输入两次??? |
|
[讨论]DEV C++中初学者编程 共同学习者顶起来
我们这里仅仅是初学者的练习讨论场 |
|
[讨论]DEV C++中初学者编程 共同学习者顶起来
首先用辗转求余法求出两数最大公约数,最小公倍数=(第一个数)*(第二个数)/(最大公约数) int gcd(int a, int b)//辗转求余 { while(b != 0) { int t = a; a = b; b = t % b; } return a; } |
|
[讨论]DEV C++中初学者编程 共同学习者顶起来
关于if语句中的判断表达式 在if语句中,我们常遇到这样的情况: if(-1) if(3) a=1,b=0; if(a+b) 等 我们要知道其判断的结果是0 还是1 给出验证 #include "stdio.h" main() { if(-1) printf("Y"); fflush(stdin); getchar(); } 输出为Y 这说明:if语句结果是:只要不是0,那就是1. 验证可能比较片面, 有不同意见请验证后指正. |
|
[讨论]DEV C++中初学者编程 共同学习者顶起来
四舍五入 谁做一个模版 |
|
[讨论]DEV C++中初学者编程 共同学习者顶起来
新学期来了 此贴又要开始活动了 加油 同学们 |
|
[讨论]DEV C++中初学者编程 共同学习者顶起来
/* 冒泡算法 就是在每次将一行中最大的数移到最后 并从下次中去除 多次后 就得到从小到大的数组 */ #include "stdio.h" main() { int a[10]; int x; for(x=0;x<10;x++) { scanf("%d",&a[x]); } for(x=0;x<10;x++) { printf("%d",a[x]); } fflush(stdin); getchar(); int i=0,j=10,t,s=0; for(j=10;j>0;j--) { for(i=0;i<j;i++) { if(a[i]>a[i+1]) t=a[i],a[i]=a[i+1],a[i+1]=t; /* 交换 */ } /* 去掉尾数最大数 但数组中最后一个最大数已保存不动 */ } for(i=0;i<10;i++) printf("%d ",a[i]); fflush(stdin); getchar(); } |
|
[讨论]DEV C++中初学者编程 共同学习者顶起来
/* 冒泡算法 就是在每次将一行中最大的数移到最后 并从下次中去除 多次后 就得到从小到大的数组 */ #include "stdio.h" main() { int a[10]={1,0,4,8,12,65,-76,100,-45,123}; int i=0,j=10,t,s=0; for(j=10;j>0;j--) { for(i=0;i<j;i++) { if(a[i]>a[i+1]) t=a[i],a[i]=a[i+1],a[i+1]=t; /* 交换 */ } /* 去掉尾数最大数 但数组中最后一个最大数已保存不动 */ } for(i=0;i<10;i++) printf("%d ",a[i]); getchar(); } |
|
[讨论]DEV C++中初学者编程 共同学习者顶起来
#include "stdio.h" main() { int i,j,n; scanf("%d",&n); for(i=1;i<=n;i++) { for(j=1;j<=n-i;j++) printf(" "); for(j=1;j<=2*i-1;j++) printf("*"); printf("\n"); } for(i=1;i<=n-1;i++) { for(j=1;j<=i;j++) printf(" "); for(j=1;j<=(2*(n-1)-1)-(2*i-1)+1;j++) printf("*"); printf("\n"); } getchar(); getchar(); getchar(); } |
|
[讨论]DEV C++中初学者编程 共同学习者顶起来
1、书写对齐方式 应如下所示, 你对照自己的比较一下:花括号及每行起头为什么要这样。 #include "stdio.h" main() { int i,j,n; scanf("%d",&n); for(i=1;i<=n;i++) { for(j=1;j<=n-j;j++) printf(" "); for(j=1;j<=2*n-1;j++) printf("*"); printf("\n"); } for(i=1;i<=n;i++) { for(j=1;j<=i-j;j++) printf(" "); for(j=1;j<=2*n+1-2*i;j++) printf("*"); } fflush(stdin); getchar(); } 2、至于你要求的第一行一个* 每行依次增加的菱形图案,你的程序没达到要求。 打印图形: * *** ***** *** * 算法上可以分两步去打,先打印正立的三角形,再打印倒立的三角形。 你的算法中n表示打印的行数,我这里改成打印的正立的行数如图为3,倒立的行数为2 /* 打印图形: * *** ***** *** * */ #include "stdio.h" main() { int i,j,n; scanf("%d",&n); /* 打印正立图形 */ for(j=1;j<=n;j++) { for(i=n;i>j;i--) /*打印每行的空格*/ { printf(" "); } for(i=1;i<=2*j-1;i++) /*打印每行的星号 */ { printf("*"); } printf("\n"); /* 打完一行换行 */ } /* 打印倒立图形 */ for(j=n-1;j>=1;j--) { for(i=j;i<=n-1;i++) /*打印每行的空格*/ { printf(" "); } for(i=1;i<=2*j-1;i++) /*打印每行的星号 */ { printf("*"); } printf("\n"); /* 打完一行换行 */ } fflush(stdin); getchar(); } |
|
[讨论]DEV C++中初学者编程 共同学习者顶起来
/* 数组 一组有序数据的集合 整型 字符型 实数型 A=3 C=“A” D=2.356 例如一组数: 5 6 7 8 9 5 7 6 8 9 由于顺序不同而成不同数组 一维 二维 三维 数组 数组的定义: 类型 数组变量名[n] 其中n必需是整数 数组内元素属同一类型 有相同的数组名 和一组下标。 例如: int a[5] 表示 a[0] a[1] a[2] a[3] a[4] 一共五个普通变量。 a[5]={1,2,3,4,5} 则有 a[0]=1 a[1]=2 a[2]=3 a[3]=4 a[4]=5 一维数组定义时 下标只有一个 A[2] 二维数组定义时 下标有二个 A[2][3] 三维数组定义时 下标有三个 A[2][3][3] 一维数组的初始化:即对一维数组赋值,给定初值 a[5]={1,2,3,4,5} */ /* #include "stdio.h" main() { int i,a[10]; for(i=0;i<=9;i++) a[i]=i; for(i=9;i>=0;i--) printf("%d",a[i]); printf("\n"); getchar(); } */ #include "stdio.h" #include "stdio.h" main() { int a[20]={1,2},i,s; s=a[0]+a[1]; for(i=2;i<20;i++) { a[i]=a[i-1]+a[i-2]; s=s+a[i];} printf("s=%d\n",s); getchar(); } |
|
[讨论]DEV C++中初学者编程 共同学习者顶起来
#include "stdio.h" main() { int i,x,y,s; x=1,y=2,s=0; for(i=1;i<=10;i++) { s=s+x+y,x=x+y,y=x+y; } printf("s=%d",s); getchar(); } |
|
[讨论]DEV C++中初学者编程 共同学习者顶起来
for循环 for循环的一般形式为: for(<初始化>; <条件表过式>; <增量>) 语句; 初始化 总是一个赋值语句, 它用来给循环控制变量赋初值; 条件表达式是一个关系表达式, 它决定什么时候退出循环; 增量:定义循环控制变量每循环一次后按什么方式变化。 这三个部分之间用";"分开。 例如: for(i=1; i<=10; i++) 语句; 上例中先给i赋初值1, 判断i是否小于等于10, 若是则执行语句, 之后值增加1。再重新判断, 直到条件为假, 即i>10时, 结束循环。 注意: 1. for循环中语句可以为语句体, 但要用"{"和"}"将参加循环的语句括起来。 2. for循环中的"初始化"、"条件表达式"和"增量"都是选择项, 即可以缺省, 但";"不能缺省。省略了初始化, 表示不对循环控制变量赋初值。 省略了条件表达式, 则不做其它处理时便成为死循环。省略了增量, 则不对循环控制变量进行操作, 这时可在语句体中加入修改循环控制变量的语句。 3. for循环可以有多层嵌套。 |
|
[讨论]DEV C++中初学者编程 共同学习者顶起来
while语句实现当型循环 while(表达式) { 循环体 } 累加程序: #include "stdio.h" main() { int s=0,i=1; while(i<=100) /* 当只循环体只有一句时 花括号可以省略 */ { s=s+i; i++; } printf("s=%d",s); } |
操作理由
RANk
{{ user_info.golds == '' ? 0 : user_info.golds }}
雪币
{{ experience }}
课程经验
{{ score }}
学习收益
{{study_duration_fmt}}
学习时长
基本信息
荣誉称号:
{{ honorary_title }}
能力排名:
No.{{ rank_num }}
等 级:
LV{{ rank_lv-100 }}
活跃值:
在线值:
浏览人数:{{ visits }}
最近活跃:{{ last_active_time }}
注册时间:{{ user_info.create_date_jsonfmt }}
勋章
兑换勋章
证书
证书查询 >
能力值