#include <stdio.h> void count_bit(int x) { int count = 0; while(x) { x = x & x - 1; count++; } printf("%d", count); } int main() { int a = 0; scanf("%d", &a); count_bit(a); return 0; }
void print(int n) { int i = 0; printf("奇數位:\n"); for (i = 62; i != 0; i -= 2) { printf("%d ", (n >> i) & 1); } printf("\n"); printf("偶數位:\n"); for (i = 63; i != 1; i -= 2) { printf("%d ", (n >> i) & 1); } } #include <stdio.h> int main() { int m = 0; scanf("%d", &m); print(m); return 0; }
void print_table(int m) { int i = 0; for (i = 0; i <= m; i++) { int j = 0; for ( j = 0; j <= i; j++) { printf("%-2d*%-2d=%-5d ", i, j, i * j); } printf("\n"); } } #include <stdio.h> int main() { int m = 0; scanf("%d", &m); print_table(m); return 0; }
int my_strlen(char* str) { int i = 0; int count = 0; while (*str!='\0')//有在這裏出了錯,字符串結束標誌位'\0'; { str++; count++; } return count; } void reverse_string(char arr[]) { int len = my_strlen(arr); char tmp = arr[0]; arr[0] = arr[len-1]; arr[len - 1] = '\0'; if (my_strlen(arr + 1) >= 2) { reverse_string(arr + 1); } arr[len - 1] = tmp; } #include <stdio.h> int main() { char arr[] = "abcdefg"; reverse_string(arr); printf("%s\n", arr); return 0; }
int Digitsum(unsigned int n) { //if (n > 0) // return n % 10 + Digitsum(n / 10); //n = n / 10; if (n > 9) { return n % 10 + Digitsum(n / 10); } else { return n; } } #include <stdio.h> int main() { unsigned int num = 0; scanf("%d", &num); int ret = Digitsum(num); printf("ret = %d\n", ret); return 0; }
double Pow(double n, double k) { if (k < 0) { return (1.0 / (Pow(n, -k))); } else if (k == 0) { return 1; } else { return n * Pow(n, k - 1); } //if (k < -1) // return (1.0 / ( n*(n, -k))); ////else return 1 / n; //if (k > 1) // return n * Pow(n, k - 1); //else // return n; } #include <stdio.h> int main() { double k = 0; double n = 0; printf("輸入n和k,求得n的k次方:\n"); scanf("%lf%lf", &n, &k); double ret = Pow(n, k); printf("ret = %lf", ret); return 0; }
其中有些錯誤,註釋部分 ,在後面改好了git