本文主要是介绍打印乘法表,求最大数,中位数,100以内素数,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
一、打印乘方表
void Print_Square(int n)//乘方表
{for (int i = 1; i < n; ++i){for (int j = 1; j <= i; ++j){printf("%2d *%2d=%2d ", j, i, i * j);}printf("\n");}
}
int main()
{int n = 10;Print_Square(n);}
二、求三个数中的中位数
1、
int MinInt(int a, int b, int c)
{if (a > b){if (a > c){if (b > c){return b;}else {return c;}}else{return a;}}else{if (b > c){if (a > c){return a;}else{return c;}}else{return b;}}
}
int main()
{int a, b, c;int min = 0;scanf_s("%d %d %d", &a, &b, &c);min = MinInt(a, b, c);printf("%d", min);return 0;}
2、简洁版
int MinInt(int a, int b, int c)
{return a > b ? (a > c ? (b > c ? b : c) : a) : (b > c ? (a > c ? a : c) : b);
}
int main()
{int a, b, c;int min = 0;scanf_s("%d %d %d", &a, &b, &c);min = MinInt(a, b, c);printf("%d", min);return 0;}
三、求三个数中的最大数
int MaxInt(int x, int y)
{int max = x > y ? x : y;return max;
}
int main()//求三个数的最大数
{int a, b, c;int max = 0;scanf_s("%d %d %d", &a, &b, &c);max = MaxInt(a, MaxInt(b, c));printf("%d", max);return 0;
}
四、100以内素数
//计算100以内的素数
int main()
{int n = 0;for (int i = 1; i <= 100; ++i){if (i == 2){printf("%02d ", i);++n;}for(int j = 2;j < i;++j){if (i % j == 0){break;}else if (j == (i - 1)){printf("%02d ", i);++n;if (n % 5 == 0){printf("\n");}break;} } }
}
这篇关于打印乘法表,求最大数,中位数,100以内素数的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!