标签:style blog io ar color os sp for on
1.打印出如下所示图形 :
*
* *
* * *
代码如下:
for (int i = 0; i < 3; i++) { for (int j = 2; j > i; j--) { printf(" "); } for (int k = 0; k < i + 1; k++) { printf(" *"); } printf("\n"); }
*
* *
* * *
* *
*
代码如下:
for (int i = 0; i < 5; i++) { if (i < 3) { //前三行,即先打印出图形上半部分 for (int j = 0; j < 2- i; j++) { printf(" "); } for (int k = 0 ; k < i + 1 ; k++) { printf("* "); } printf("\n"); } else { //后两行,图形下半部分 for (int j = 0; j < i-2; j++) { printf(" "); } for (int k = 0; k < 5- i; k++) { printf("* "); } printf("\n"); } }
3.从控制台输入一个整数,判断是否是质数.
<span style="font-size:14px;"> int n = 0, m = 0; printf("please input a int number:\n"); scanf("%d", &n); for (m = 2; m < n ; m++) { if (n % m == 0 ){ printf("不是质数:%d",n); break;//结束本层循环,循环结束. } }//循环结束后. if (m == n) { printf("是质数\n"); }</span>
c语言中如何取出随机数:
随机公式:[a,b]范围在a-b之间的随机数
int n3 = arc4random() % ( b - a + 1) + a;
4.随机产生 20 个[10 , 100]的正整数,输出这些数以及他们中的最大数
<span style="font-size:14px;">int max = 0;int temp = 0; for (int j =0; j < 20; j++) { temp = arc4random() % 91 +10; if( max < temp){ max = temp; } } printf("the max is %d\n", max);</span>
<span style="font-size:14px;"> int x = 0;//百位数 int y = 0;//十位数 int z = 0;//个位数 for (int num = 100; num <= 999; num++) { x = num / 100; y = num / 10 % 10; z = num % 10 ; if (num == (x*x*x + y*y*y + z*z*z)) { printf("水仙花数为:%d\n",num); } }</span>
<span style="font-size:14px;"> int a = 0; int b = 0; int yushu = 0; printf("please intput two numbers :\n"); scanf("%d%d", &a, &b); //辗转相除法 int max = 0, min = 0; max = a > b ? a : b;//判断ab大小,把大的值给max.最为被除数 min = a > b ? b : a;//最为除数. yushu = max % min; while (yushu != 0) { max = min ; min = yushu; yushu = max % min ; } printf("最大公约数为:%d\n", min); printf("最大公倍数为:%d\n", (a * b) / min); //普通方法 for (int i = 1; i <= min; i++) { if (max % i == 0 && min % i == 0) { yue=i; } } printf("最大公约数为:%d\n", yue); printf("最小公约数:%d\n",(a*b)/yue); </span>
<span style="font-size:14px;"> int min = 0, temp = 0, n =0; printf("please intput a number N:\n"); scanf("%d", &n); for (int j = 0; j < n; j++) { temp = arc4random() % (15 - 10 + 1) + 10; if (j == 0) { min = temp; } else if( min > temp){ min = temp; } } printf("the min is %d\n", min);</span>
标签:style blog io ar color os sp for on
原文地址:http://blog.csdn.net/zfx5130/article/details/41790051