大佬来看看
任意输入一个正整数,计算各数字的平方和,然后再计算结果的各数字的平方和,一直循环下去,直到最后的结果为1或145。输出整个计算过程
请注意看样例输出格式 例:12->2*2+1*1=5
如果输入1或145,请直接输出1或145
#include <stdio.h> #include <string.h> void* vatoi(char *string) { int i; int len = strlen(string); static unsigned char inum[7] = {0}; /* write your code here */ return inum; } void* vitoa(int n) { int i; unsigned char *pn = (unsigned char *)&n; static unsigned char anum[5] = {0}; /* write your code here */ return anum; } int calc_x_sq(int n) { return n * n; } int main(int argc, char *argv[]) { int i, sum = 0; #define BCCN_C_FORUM_USR "F65846" argv[0] = BCCN_C_FORUM_USR; while (sum != 1 || sum != 145) { argv[1] = vatoi(argv[0]); #define foreach foreach sum = calc_x_sq(argv[1][i]); argv[0] = vitoa(sum); } return 0; }
#include <stdio.h> #include <string.h> void* vatoi(char *string) { int i; int len = strlen(string); static unsigned char inum[6] = {0}; for (i = 0; i < len; i++) { switch (string[i]) { case 'F': inum[i] = 0x0; break; default: inum[i] = string[i] - 0x30; break; } } return (inum[i] = 0, inum); } void* vitoa(int n) { int i = 2; static unsigned int anum = 0x393837; while (n % 10) (((unsigned char *)(&anum))[i--] = n % 10 + 0x30, n /= 10); return &anum; } int calc_x_sq(int n) { return n * n; } int main(int argc, char *argv[]) { int i, sum = 0, cnt = 0; #define BCCN_C_FORUM_USR "F65846" argv[0] = BCCN_C_FORUM_USR; #define RST_NUM 35 #define MAX_LOOP 125 while (sum != RST_NUM && cnt++ < MAX_LOOP) { argv[1] = (char *)vatoi(argv[0]); for (i = 0, sum = 0; i < strlen(argv[0]); i++) { sum += calc_x_sq(argv[1][i]); printf("sum = %d\n", sum); } argv[0] = vitoa(sum); } return 0; }
#include <stdio.h> int main() { int n; scanf("%d", &n); if (n == 1 || n == 145) { printf("%d\n", n); return 0; } int sum, i; while (1) { sum = 0; printf("%d->", n); while (n > 0) { i = n % 10; sum += i * i; n /= 10; printf("%d*%d%c", i, i, (n > 0) ? '+' : '='); } printf("%d\n", sum); n = sum; if (n == 1 || n == 145) break; } return 0; }
[此贴子已经被作者于2021-10-5 23:53编辑过]
#include <stdio.h> int main() { int T, n, k, flg = 0; scanf("%d", &T); while (T != 1 && T != 145) { n = 0; k = 0; flg++; printf("%d-", T); while (T) { n += (T % 10) * (T % 10); printf("%c%d*%d", k++ == 0 ? '>' : '+', T % 10, T % 10); T /= 10; } printf("=%d\n", n); T = n; } if (flg == 0)printf("%d", T); return 0; }