
1、判断三个数值的大小并排序。
#include
#include
#include
#include
int main()
{
int a = 0;
int b = 0;
int c = 0;
scanf("%d%d%d", &a, &b, &c);
//算法实现
//a中放最大值
//b次之
//c中放最小值
if (a < b)
{
int tmp = a;
a = b;
b = tmp;
}
if (a < c)
{
int tmp = a;
a = c;
c = tmp;
}
if (b < c)
{
int tmp = b;
b = c;
c = tmp;
}
printf("%d %d %d", a, b, c);
return 0;
}
打印3的倍数的数:写一个代码打印1-100之间所有3的倍数
#include
#include
#include
#include
int main()
{
int i = 0;
for (i = 1; i <= 100; i++)
{
if (i % 3 == 0)
printf("%d ", i);
}
return 0;
}
最大公约数:给定两个数,求这两个数的最大公约数。
#include
#include
#include
#include
int main()
{
int m = 24;
int n = 18;
int r = 0;
scanf("%d%d", &m, &n);
while (r = m%n)
{
m = n;
n = r;
}
printf("%d ", n);
return 0;
}
找出1000-2000年之间的闰年。
#include
#include
#include
#include
int main()
{
int year = 0;
int count = 0;
for (year = 1000; year <= 2000; year++)
{
if (year % 4 == 0 && year % 100 != 0)
{
printf("%d ", year);
count++;
}
else if(year%400 == 0)
{
printf("%d ", year);
count++;
}
}
printf("\ncount = %d\n", count);
return 0;
}
1004 1008 1012 1016 1020 1024 1028 1032 1036 1040 1044 1048 1052 1056 1060 1064 1068 1072 1076 1080 1084 1088 1092 1096 1104 1108 1112 1116 1120 1124 1128 1132 1136 1140 1144 1148 1152 1156 1160 1164 1168 1172 1176 1180 1184 1188 1192 1196 1200 1204 1208 1212 1216 1220 1224 1228 1232 1236 1240 1244 1248 1252 1256 1260 1264 1268 1272 1276 1280 1284 1288 1292 1296 1304 1308 1312 1316 1320 1324 1328 1332 1336 1340 1344 1348 1352 1356 1360 1364 1368 1372 1376 1380 1384 1388 1392 1396 1404 1408 1412 1416 1420 1424 1428 1432 1436 1440 1444 1448 1452 1456 1460 1464 1468 1472 1476 1480 1484 1488 1492 1496 1504 1508 1512 1516 1520 1524 1528 1532 1536 1540 1544 1548 1552 1556 1560 1564 1568 1572 1576 1580 1584 1588 1592 1596 1600 1604 1608 1612 1616 1620 1624 1628 1632 1636 1640 1644 1648 1652 1656 1660 1664 1668 1672 1676 1680 1684 1688 1692 1696 1704 1708 1712 1716 1720 1724 1728 1732 1736 1740 1744 1748 1752 1756 1760 1764 1768 1772 1776 1780 1784 1788 1792 1796 1804 1808 1812 1816 1820 1824 1828 1832 1836 1840 1844 1848 1852 1856 1860 1864 1868 1872 1876 1880 1884 1888 1892 1896 1904 1908 1912 1916 1920 1924 1928 1932 1936 1940 1944 1948 1952 1956 1960 1964 1968 1972 1976 1980 1984 1988 1992 1996 2000
count = 243
另外一种写法
#include
#include
#include
#include
int main()
{
int year = 0;
int count = 0;
for (year = 1000; year <= 2000; year++)
{
/*if (year % 4 == 0 && year % 100 != 0)
{
printf("%d ", year);
count++;
}
else if(year%400 == 0)
{
printf("%d ", year);
count++;
}*/
if ((year % 4 == 0 && year % 100 != 0) || (year % 400 == 0))
{
printf("%d ", year);
count++;
}
}
printf("\ncount = %d\n", count);
return 0;
}
打印100-200之间的素数。
第一种
#include
#include
#include
#include
int main()
{
int i = 0;
int count = 0;
for (i = 100; i <= 200; i++)
{
//判断i是否为素数
//素数判断规则
//1、试除法
//13 2-12
//产生2->i-1
int j = 0;
for (j = 2; j < i; j++)
{
if (i%j == 0)
{
break;
}
}
if (j == i)
{
count++;
printf("%d ", i);
}
}
printf("\ncount = %d\n", count);
return 0;
}
101 103 107 109 113 127 131 137 139 149 151 157 163 167 173 179 181 191 193 197 199
count = 21
第二种
#include
#include
#include
#include
#include
int main()
{
int i = 0;
int count = 0;
//sqrt - 开平方的数学库函数
for (i = 100; i <= 200; i++)
{
//判断i是否为素数
//素数判断规则
//1、试除法
//13 2-12
//产生2->i-1
int j = 0;
for (j = 2; j <= sqrt(i); j++)
{
if (i%j == 0)
{
break;
}
}
if (j>sqrt(i))
{
count++;
printf("%d ", i);
}
}
printf("\ncount = %d\n", count);
return 0;
第三种
#include
#include
#include
#include
#include
int main()
{
int i = 0;
int count = 0;
//sqrt - 开平方的数学库函数
for (i = 101; i <= 200; i+=2)
{
//判断i是否为素数
//素数判断规则
//1、试除法
//13 2-12
//产生2->i-1
int j = 0;
for (j = 2; j <= sqrt(i); j++)
{
if (i%j == 0)
{
break;
}
}
if (j>sqrt(i))
{
count++;
printf("%d ", i);
}
}
printf("\ncount = %d\n", count);
return 0;
}
编写程序数一下 1到100 的所有整数中出现多少个数字9?
#include
int main()
{
int i = 0;
int count = 0;
for (i = 1; i <= 100; i++)
{
if (i % 10 == 9)
count++;
if (i / 10 == 9)
count++;
}
printf("count = %d\n", count);
return 0;
}
count = 20
计算1/1-1/2+1/3-1/4+1/5…+1/99-1/100的值,打印出结果。
#include
int main()
{
int i = 0;
float sum = 0.0;
int flag = 1;
for (i = 1; i <= 100; i++)
{
sum += flag*1.0 / i;
flag = -flag;
}
printf("%lf\n ", sum);
return 0;
}
0.688172
打印九九乘法表。
#include
int main()
{
int i = 0;
for (i = 1; i <= 9; i++)
{
//打印一行
int j = 1;
for (j = 1; j <= i; j++)
{
printf("%d*%d=%-2d ", i, j, i*j);
}
printf("\n");
}
return 0;
}
1*1=1
2*1=2 2*2=4
3*1=3 3*2=6 3*3=9
4*1=4 4*2=8 4*3=12 4*4=16
5*1=5 5*2=10 5*3=15 5*4=20 5*5=25
6*1=6 6*2=12 6*3=18 6*4=24 6*5=30 6*6=36
7*1=7 7*2=14 7*3=21 7*4=28 7*5=35 7*6=42 7*7=49
8*1=8 8*2=16 8*3=24 8*4=32 8*5=40 8*6=48 8*7=56 8*8=64
9*1=9 9*2=18 9*3=27 9*4=36 9*5=45 9*6=54 9*7=63 9*8=72 9*9=81
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)