#include <stdio.h> int main() { int ans[500]; int ans_count = 0; for (int i = 1; i <= 1000; i++) { bool check = true; int sum = 0; for (int j = 1; j < i; j++) if (i % j == 0) sum += j; if (sum == i) { ans[ans_count++] = i; printf("%d\n", i); } } return 0;
#include <stdio.h> int main() { double height = 100.0; double journey = 100.0; for (int i = 1; i < 10; i++) { journey += height; height /= 2.0; } height /= 2.0; printf("journey = %lf\nheight = %lf\n", journey, height); return 0; }
#include <stdio.h> int print(int a, char b) { char c; if (a == 1) c = 'x'; else if (a == 2) c = 'y'; else c = 'z'; printf("%c-%c\n", b, c); } int main() { int a, b, c; // 1:x 2:y 3:z for (a = 1; a <= 3; a++) for (b = 1; b <=3; b++) if (a != b) { c = 6 - a - b; if (a == 1 || c == 1 || c == 3) continue; print(a, 'a'); print(b, 'b'); print(c, 'c'); } return 0; }
#include <stdio.h> int main() { int age[6]; age[1] = 10; age[2] = age[1] + 2; age[3] = age[2] + 2; age[4] = age[3] + 2; age[5] = age[4] + 2; printf("age=%d\n", age[5]); return 0; }
#include <stdio.h> /* 就当是正整数排序吧 ,输入若干个数,以-1结尾 * 输出的话从小到大排序好了 * 选择的是插入排序咯,因为链表插入快嘛,要利用起来哟 */ struct My_Link { int val; My_Link* next; }; int main() { My_Link* head = new My_Link; head->val = -1; head->next = NULL; // input data and sort int a; scanf("%d", &a); while (a >= 0) { My_Link* new_node = new My_Link; new_node->val = a; new_node->next = NULL; My_Link* now = head->next; My_Link* last = head; while (now != NULL && now->val < a) { last = now; now = now->next; } new_node->next = last->next; last->next = new_node; scanf("%d", &a); } // print data My_Link* now_node = head->next; while (now_node != NULL) { printf("%d\n", now_node->val); now_node = now_node -> next; } // recover memory while (head != NULL) { now_node = head; head = head->next; delete now_node; } return 0; }
#include <stdio.h> #include <string.h> void s_reverse_n(char* s, int n) { for (int i = 0; i < n / 2; i++) { char p = s[i]; s[i] = s[n - i - 1]; s[n - i - 1] = p; } } int main() { char a[50]; strcpy(a, "Hello world."); s_reverse_n(a, strlen(a)); printf("%s\n", a); return 0; }
#include <stdio.h> int main() { double profit; printf("profit = $ "); scanf("%lf", &profit); double bonus = 0.0; if (profit <= 100000) bonus = profit * 0.1; else if (profit <= 200000) bonus = 10000 + (profit - 100000) * 0.075; else if (profit <= 400000) bonus = 17500 + (profit - 200000) * 0.05; else if (profit <= 600000) bonus = 27500 + (profit - 400000) * 0.03; else if (profit <= 1000000) bonus = 33500 + (profit - 600000) * 0.015; else bonus = 39500 + (profit - 1000000) * 0.01; printf("bonus = $ %lf", bonus); return 0; }
#include <stdio.h> int main() { int ans = 1; // 9 times for (int i = 0; i < 9; i++) { ans += 1; ans *= 2; } printf("%d\n", ans); return 0; }
#include <stdio.h> int main() { int a[50]; int n; int max_i, min_i; printf("Input N = "); scanf("%d", &n); printf("Input %d numbers : ", n); for (int i = 0; i < n; i++) scanf("%d", &a[i]); max_i = 0; for (int i = 1; i < n; i++) if (a[i] > a[max_i]) max_i = i; int tmp = a[0]; a[0] = a[max_i]; a[max_i] = tmp; min_i = 0; for (int i = 1; i < n; i++) if (a[i] < a[min_i]) min_i = i; tmp = a[0]; tmp = a[n - 1]; a[n - 1] = a[min_i]; a[min_i] = tmp; for (int i = 0; i < n; i++) printf("%d ", a[i]); printf("\n"); return 0; }
#include <stdio.h> #include <stdlib.h> #include <string.h> #include <time.h> /* 以下实现的是RSA算法的阉割版 * 当然看着代码是可以破解的,因为把解密用的密匙也写在这了,实际中,P和Q是可以抛弃的,使用N、e进行加密,使用N、d进行解密 */ const unsigned long long P = 12301; const unsigned long long Q = 3217; unsigned long long gcd(unsigned long long a, unsigned long long b) { if (b == 0) return a; else return gcd(b, a % b); } unsigned long long x, y; void extend_Eulid(unsigned long long a, unsigned long long b) { if(b == 0) { x = 1; y = 0; } else { extend_Eulid(b, a%b); unsigned long long temp = x; x = y; y = temp - a / b * y; } } void get_key(unsigned long long P, unsigned long long Q, unsigned long long &N, unsigned long long &e, unsigned long long &d) { N = P * Q; unsigned long long R1 = P - 1; unsigned long long R2 = Q - 1; unsigned long long R = R1 * R2; unsigned long long one = 1; e = (P + Q) * ((long long) rand()) / 65535; while (gcd(e, R) != 1) e = e + one; extend_Eulid(e, R); d = x % R; } // return a^b (mod c) unsigned long long power(unsigned long long a, unsigned long long b, unsigned long long c) { unsigned long long two = 2; unsigned long long res = 1; while (b > 0) { if (b % two == 1) res = (res * a) % c; a = (a * a) % c; b = b / two; } return res; } void encode(unsigned long long code[], int& code_len, char s[], int len, unsigned long long N, unsigned long long e) { unsigned long long M = 0; unsigned long long ONE_CHAR = 128; code_len = 0; int c = 0; for (int i = 0; i < len; i++) { M = M * ONE_CHAR + ((unsigned long long) s[i]); c++; if (c % 3 == 0) { code[code_len++] = M; M = 0; c = 0; } } if (c != 0) code[code_len++] = M; for (int i = 0; i < code_len; i++) { code[i] = power(code[i], e, N); } } void decode(unsigned long long code[], int code_len, char s[], int &len, unsigned long long N, unsigned long long d) { unsigned long long ONE_CHAR = 128; len = 0; int last_len = 0; for (int i = 0; i < code_len; i++) { unsigned long long M = power(code[i], d, N); while (M > 0) { s[len++] = (unsigned char) (M % ONE_CHAR); M = M / ONE_CHAR; } char q = s[last_len]; s[last_len] = s[len - 1]; s[len - 1] = q; last_len = len; } } int main() { srand(time(0)); unsigned long long N, e, d; get_key(P, Q, N, e, d); printf("N=%I64d\ne=%I64d\nd=%I64d\n", N, e, d); printf("-----------------------------\n"); printf("Please input a string (length <= 20):\n"); char s[20]; gets(s); unsigned long long c[20]; int c_len = 0; encode(c, c_len, s, strlen(s), N, e); printf("After coding m = "); for (int i = 0; i < c_len; i++) printf("%I64d ", c[i]); printf("\n"); printf("After decoding s = "); int len; char t[20]; decode(c, c_len, t, len, N, d); for (int i = 0; i < len; i++) printf("%c", t[i]); printf("\n"); return 0; }











