标题:大佬来看看
只看楼主
F65846
Rank: 1
等 级:新手上路
帖 子:10
专家分:0
注 册:2021-9-19
结帖率:0
 问题点数:0 回复次数:5 
大佬来看看
任意输入一个正整数,计算各数字的平方和,然后再计算结果的各数字的平方和,一直循环下去,直到最后的结果为1或145。
输出整个计算过程
请注意看样例输出格式   例:12->2*2+1*1=5
如果输入1或145,请直接输出1或145
搜索更多相关主题的帖子: 数字 平方 计算 输出 结果 
2021-10-04 17:05
自由而无用
Rank: 9Rank: 9Rank: 9
等 级:贵宾
威 望:14
帖 子:61
专家分:1456
注 册:2021-8-9
得分:0 
/https://blog.

pseudocode
程序代码:
#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;
}
2021-10-04 18:36
自由而无用
Rank: 9Rank: 9Rank: 9
等 级:贵宾
威 望:14
帖 子:61
专家分:1456
注 册:2021-8-9
得分:0 
//online parser: https://www.bccn.net/run/
程序代码:
#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;
}


output sample:

sum = 0
sum = 36
sum = 61
sum = 125
sum = 141
sum = 177
sum = 1
sum = 50
sum = 99
sum = 1
sum = 82
sum = 163
sum = 1
sum = 37
sum = 46
sum = 1
sum = 17
sum = 53
sum = 1
sum = 26
sum = 35
2021-10-04 20:32
F65846
Rank: 1
等 级:新手上路
帖 子:10
专家分:0
注 册:2021-9-19
得分:0 


2021-10-05 22:00
apull
Rank: 16Rank: 16Rank: 16Rank: 16
来 自:三体星系
等 级:版主
威 望:185
帖 子:1404
专家分:8479
注 册:2010-3-16
得分: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编辑过]

2021-10-05 23:36
F65846
Rank: 1
等 级:新手上路
帖 子:10
专家分:0
注 册:2021-9-19
得分:0 
回复 5楼 apull
printf("%d*%d%c", i, i, (n > 0) ? '+' : '=');这串是什么意思
2021-10-06 11:05
apull
Rank: 16Rank: 16Rank: 16Rank: 16
来 自:三体星系
等 级:版主
威 望:185
帖 子:1404
专家分:8479
注 册:2010-3-16
得分:0 
根据n输出+或者=
2021-10-06 11:06
不会游泳的虾
Rank: 7Rank: 7Rank: 7
等 级:贵宾
威 望:24
帖 子:101
专家分:643
注 册:2021-7-1
得分:0 
供参考:
程序代码:
#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;
}
2021-10-19 15:56



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-507054-1-1.html




关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.820645 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved