分类: C/C++
2014-11-27 19:50:30
自己编写的(1)
整型数和括号的作用求模
求模就是取余数 |
/* This is a test program * This program was written by LiuJiaFeng * Modified in 2013 10 16 */
#include int main()
{
int x, y1, y2;
printf("please input the x and y .\n"); scanf("%d",&x); y1 = x%5 ; y2 = x%3 ; printf("yi ci fang cheng : y1 = %d \n",y1); printf("er ci fang cheng : y2 = %d \n",y2); return 0 ; } |
/* This is a test program * This program was written by LiuJiaFeng * Modified in 2013 10 16 */
#include int main()
{
int y1,y2,y3,y4;
y1 = 10472 % 47 ; y2 = 39263 % 62 ; y3 = 98127 % 9 ; y4 = 111111 % 37 ;
printf(" y1 = %d \n",y1); printf(" y2 = %d \n",y2); printf(" y3 = %d \n",y3); printf(" y4= %d \n",y4); getchar(); return 0 ; } |
余数为零,能整出 |
学会用随机数,需要在前面多打#include
说明 |
先打Srand()在后面出现的rand()就会尾随出数 |
|
|
|
#include
#include
#include
{ float a,b,c,t; srand(100); a=rand(); b=rand(); c=rand(); if(a { t=a; a=b; b=t; }
if(a { t=a; a=c; c=t; }
if(b { t=b; b=c; c=t; } printf("%6.2f,%6.2f,%6.2f\n",a,b,c); getchar(); } |
|
|
心得 |
不用自动输a,b,c 只需把srand()括号中的改掉,就会出新数 |
|
Time函数
说明 |
Time函数, 不用自己打数了,但得定义一个#include 每次都不用输数,按ctrl f9 自动运行 自动出数 |
|
|
#include
#include
#include
#include
void main() { float a,b,c,t; Srand(time(NULL)); a=rand(); b=rand(); c=rand(); if(a { t=a; a=b; b=t; }
if(a { t=a; a=c; c=t; }
if(b { t=b; b=c; c=t; } printf("%6.2f,%6.2f,%6.2f\n",a,b,c);
getchar(); } |
|
说明 |
Printf() 这个意思是在屏幕上输入a,b,c Scanf() 往&-这个地址输入浮点数并给它付值
|
|
过程
|
#include
void main() { float a,b,c,t; printf("please enter a,b,c:"); scanf("%f,%f,%f",&a,&b,&c); if(a { t=a; a=b; b=t; }
if(a { t=a; a=c; c=t; }
if(b { t=b; b=c; c=t; } printf("%6.2f,%6.2f,%6.2f\n",a,b,c); getchar(); } |
|
心得 |
|
|
|
|
|