diff --git a/pset7/3-getDigit.c b/pset7/3-getDigit.c index e566478..46ed434 100644 --- a/pset7/3-getDigit.c +++ b/pset7/3-getDigit.c @@ -1,16 +1,25 @@ -#include +// #include +// +// int getDigit(long long n); +// +// int main() { +// long long n; +// int len; +// +// scanf("%lld", &n); +// len = getDigit(n); +// if (len > 1) +// printf("The integer %lld has %d digits.\n", n, len); +// else +// printf("The integer %lld has %d digit.\n", n, 1); +// return 0; +// } -int getDigit(long long n); - -int main() { - long long n; - int len; - - scanf("%lld", &n); - len = getDigit(n); - if (len > 1) - printf("The integer %lld has %d digits.\n", n, len); - else - printf("The integer %lld has %d digit.\n", n, 1); - return 0; +int getDigit(long long n) { + int len = 0; + while (n > 0) { + n /= 10; + len++; + } + return len; } diff --git a/pset7/4-reverseNum.c b/pset7/4-reverseNum.c new file mode 100644 index 0000000..eb8649b --- /dev/null +++ b/pset7/4-reverseNum.c @@ -0,0 +1,21 @@ +// #include +// +// int reverseNum(int num); +// +// int main() { +// int num; +// +// scanf("%d", &num); +// printf("The reverse form of number %d is %d.\n", num, reverseNum(num)); +// +// return 0; +// } + +int reverseNum(int num) { + int ans = 0; + while (num > 0) { + ans = ans * 10 + num % 10; + num /= 10; + } + return ans; +} diff --git a/pset7/5-myPow.c b/pset7/5-myPow.c new file mode 100644 index 0000000..ebff7c5 --- /dev/null +++ b/pset7/5-myPow.c @@ -0,0 +1,20 @@ +// #include +// +// int mypow(int, int); +// +// int main() { +// int x, n; +// +// scanf("%d%d", &x, &n); +// printf("%d\n", mypow(x, n)); +// +// return 0; +// } + +int mypow(int a, int b) { + int pow = 1; + for (int i = 0; i < b; i++) { + pow *= a; + } + return pow; +} diff --git a/pset7/6-triangle.c b/pset7/6-triangle.c new file mode 100644 index 0000000..a85230f --- /dev/null +++ b/pset7/6-triangle.c @@ -0,0 +1,59 @@ +// #include +// +// int judgeTriangle(int, int, int); +// +// int main() { +// int a, b, c; +// +// scanf("%d%d%d", &a, &b, &c); +// +// switch (judgeTriangle(a, b, c)) { +// case -1: +// printf("It is not a triangle.\n"); +// break; +// case 0: +// printf("It is a scalenous triangle.\n"); +// break; +// case 1: +// printf("It is a right-angled triangle.\n"); +// break; +// case 2: +// printf("It is an isosceles triangle.\n"); +// break; +// case 3: +// printf("It is a equilateral triangle.\n"); +// break; +// } +// +// return 0; +// } + +int judgeTriangle(int a, int b, int c) { + // First we bubble sort them. + int tmp; + if (a > b) { + tmp = a; + a = b; + b = tmp; + } + if (b > c) { + tmp = b; + b = c; + c = tmp; + } + + // If this is a triangle + if (a + b <= c) { + return -1; // Not a triangle + } else if (b == c || a == b || a == c) { + if (a == b && b == c) { + return 3; // Equilateral + } else { + return 2; // Isosceles + } + } else if (a * a + b * b == c * c) { + return 1; // Right triangle + } else { + return 0; + } +} diff --git a/pset7/7-perfect.c b/pset7/7-perfect.c new file mode 100644 index 0000000..fae0576 --- /dev/null +++ b/pset7/7-perfect.c @@ -0,0 +1,45 @@ +// #include +// +// //判断一个数是否为完全数的函数 +// int isPerfect(int); +// +// //打印完全数的函数 +// void printPerfact(int); +// +// int main() { +// int i, a, b, count; +// +// scanf("%d%d", &a, &b); +// count = 0; // a,b两数间完全数的数量,初始化为0 +// for (i = a; i <= b; i++) { +// if (isPerfect(i)) //如果是完全数 +// { +// printPerfact(i); //打印该完全数 +// count++; //计数器加1 +// } +// } +// printf("The total number is %d.\n", count); //输出a,b两数间完全数的数量 +// return 0; +// } + +//判断一个数是否为完全数的函数 +int isPerfect(int n) { + int sum = 0; + for (int i = 1; i <= n / 2; i++) { + if (n % i == 0) { + sum += i; + } + } + return (sum == n) ? 1 : 0; +} + +//打印完全数的函数 +void printPerfact(int n) { + printf("%i=1", n); + for (int i = 2; i <= n / 2; i++) { + if (n % i == 0) { + printf("+%i", i); + } + } + printf("\n"); +}