学数据结构与算法前的热身,没想到热了一天,杯具。
2-1.pngvoid daffodil()
{
/** 输出 100~999 中的所有水仙花数。
若 3 位数 ABC 满足 ABC = A*A*A + B*B*B + C*C*C,则称其为水仙花数。
例如 153 = 1*1*1 + 5*5*5 + 3*3*3, 所以 153 是水仙花数。
*/
for ( int i = 100; i <= 999; i++) {
// 取出每位数
int A = i / 100;
int B = i / 10 % 10;
int C = i % 10;
if ( i == A*A*A + B*B*B + C*C*C )
printf("%d 是水仙花数\n", i);
}
}
2-2.png
void hanxin()
{
int a, b, c;
int findNum = -1;
scanf("%d%d%d", &a, &b, &c);
for ( int i = 10; i <= 100; i++ ) {
int lessA = i % 3;
int lessB = i % 5;
int lessC = i % 7;
if ( a == lessA && b == lessB && c == lessC )
{
findNum = i;
break;
}
}
if ( findNum == -1 )
printf("No answer.\n");
else
printf("%d\n", findNum);
}
2-3.png
void triangle()
{
int n, maxCount = 1;
scanf("%d", &n);
for ( int i = 1; i < n; i++ ) {
maxCount += 2;
}
for ( int i = 0; i < n; i++ ) {
for ( int k = i; k > 0; k-- ) {
printf(" ");
}
for ( int j = maxCount ; j > 0; j-- ) {
// 1 3 5 7 9
printf("#");
}
maxCount -= 2;
printf("\n");
}
}
2-4.png
void subsequence()
{
int n, m;
while ( scanf("%d%d", &n, &m) ) {
if ( n == 0 && m == 0 )
break;
double sum = 0;
for ( int i = n; i <= m; i++ )
sum += ( 1.0 / i ) * ( 1.0 / i );
/**
当 n = 65536 ,m = 655360 时:
sum += 1.0 / (i * i); 报 inf;
把 sum 声明成 long double 还是有问题;
而把 i 声明成 long i 后就没事。(没搞懂有什么联系);
*/
printf("%.5f\n", sum);
}
}
2-5.png
void decimal2()
{
// 习题 2-5
int a, b, c, e = 1;
while ( scanf("%d%d%d", &a, &b, &c) ) {
if ( a == 0 && b == 0 && c == 0 )
break;
// a/b
double num = a / (double)b;
// 把要精确的小数往前移动
for ( int i = 0; i < c; i++ )
e *= 10;
// 判断 4 舍 5 入
int newNum = (int)floor(num * e * 10);
if ( newNum % 10 >= 5 )
newNum = newNum / 10 + 1;
else
newNum /= 10;
// 计算小数
int decimals = newNum % e;
printf("%d.%d\n", (int)num, decimals);
}
}
2-6.png
// 计算有多少位
int countOfDigitWithNum(long num)
{
int count = 1;
while ( num >= 10 ) {
count++;
if ( (num /= 10) < 10 )
break;
}
return count;
}
// 判断每位上是否有重复数字
bool isDigitRepeatBetween(int a, int b, bool checkSelf)
{
// 算出位数
int aCount = countOfDigitWithNum(a);
int bCount = countOfDigitWithNum(b);
// 肯定会重复的直接返回
if ( aCount > 10 || bCount > 10 || aCount + bCount > 10 )
return true;
// 比较每位数是否重复
int eA = 1;
for ( int i = 0; i < aCount; i++ ) {
if ( i > 0 ) eA *= 10;
int digitA = (a / eA) % 10;
// 是否检测 a 自身重复
if ( checkSelf ) {
int e = eA;
for ( int j = 0; j < aCount - 1; j++ ) {
e *= 10;
// 与前一位比较
int digit = (a / e) % 10;
if ( digitA == digit )
return true;
}
}
int eB = 1;
for ( int j = 0; j < bCount; j++ ) {
if ( j > 0 ) eB *= 10;
int digitB = (b / eB) % 10;
if ( digitA == digitB )
return true;
// 开始比较 a b
// 是否检测 b 自身重复
if ( checkSelf ) {
int e = eB;
for ( int j = 0; j < bCount - 1; j++ ) {
e *= 10;
// 与前一位比较
int digit = (b / e) % 10;
if ( digitB == digit )
return true;
}
}
}
}
return false;
}
void permutation()
{
int a, b, c;
// 三位数且不能重复,由于 0 的倍数为 0,所以从 123 开始。
for ( int i = 123; i < 987; i++ ) {
a = i;
b = 2 * a;
c = 3 * a;
// 超范围退出循环
if ( c > 987 )
break;
// 互相之间重复略过
if ( isDigitRepeatBetween(a, b, true) ||
isDigitRepeatBetween(a, c, true) ||
isDigitRepeatBetween(b, c, true) )
continue;
printf("%d %d %d\n", a, b, c);
}
}
网友评论