Digital Roots (题目虽水,解法不可水)

本文介绍了一种计算数字根的方法,即通过不断累加整数的各位数字直至结果为单一位数的过程,并提供了一个简单的C语言程序实现。

Digital Roots

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 42315    Accepted Submission(s): 13085


Problem Description
The digital root of a positive integer is found by summing the digits of the integer. If the resulting value is a single digit then that digit is the digital root. If the resulting value contains two or more digits, those digits are summed and the process is repeated. This is continued as long as necessary to obtain a single digit.

For example, consider the positive integer 24. Adding the 2 and the 4 yields a value of 6. Since 6 is a single digit, 6 is the digital root of 24. Now consider the positive integer 39. Adding the 3 and the 9 yields 12. Since 12 is not a single digit, the process must be repeated. Adding the 1 and the 2 yeilds 3, a single digit and also the digital root of 39.
 

Input
The input file will contain a list of positive integers, one per line. The end of the input will be indicated by an integer value of zero.
 

Output
For each integer in the input, output its digital root on a separate line of the output.
 

Sample Input
  
24 39 0
 

Sample Output
  
6 3

#include <stdio.h>

int main()
{
    int sum=0;
    char a;

    while(a=getchar())//数据很大,用字符处理
    {
        if(a=='\n')
        {
            if(!sum) return 0;
            printf("%d\n",sum%9?sum%9:9); //把所有位数的数字相加,然后 mod 9 可得
            sum=0;
        }
        else if(a>='0' && a<='9')
        {
            sum+=a-'0';
        }
    }

    return 0;
}


在Java中,GC Roots 是垃圾收集器进行对象可达性分析的起点。只有符合特定条件的对象才能作为 GC Roots,而一些不具备“活跃引用”特性的对象则不能作为 GC Roots。 以下几类对象**不能作为 GC Roots**: - **局部变量(非引用)**:仅在方法内部定义的局部变量,如果没有被显式赋值给某个静态字段、实例字段或传递到其他长期存活的对象中,则不会被视为 GC Root。例如,一个仅存在于栈帧中的临时整型变量 `int i = 0;` 并不构成 GC Root [^3]。 - **弱引用(WeakReference)所引用的对象**:使用 `java.lang.ref.WeakReference` 包装的对象,在下一次 GC 时即使内存充足也会被回收,因此它们不能作为 GC Roots [^2]。 - **软引用(SoftReference)所引用的对象(在内存不足时)**:虽然软引用对象在内存充足时可以保持存活,但在内存不足时仍会被回收,因此它们通常也不适合作为 GC Roots [^2]。 - **虚引用(PhantomReference)所引用的对象**:虚引用无法通过其获取对象本身,主要用于跟踪对象被回收的状态,因此它也不能作为 GC Root [^2]。 - **未被 `static final` 修饰的常量池引用**:只有 `static final` 修饰的常量才会被加载到方法区(元空间),并可能成为 GC Root。如果一个常量没有被 `static final` 修饰,则它不会被保留在运行时常量池中,因此不能作为 GC Root [^4]。 - **临时创建且未被任何活跃线程访问的对象**:例如在方法中临时创建的对象,如果未被外部引用(如局部变量、静态字段等),也不会成为 GC Root [^5]。 ### 示例代码说明 下面是一个简单示例,展示哪些对象**不会**成为 GC Roots: ```java public class NonGCRootsExample { public static void main(String[] args) { // 局部基本类型变量,不能作为GC Root int tempValue = 10; // 局部对象变量,未被外部引用,将被回收 Object localObject = new Object(); localObject = null; // 主动置空,加速回收 // 创建一个弱引用对象,它不能作为GC Root WeakReference<Object> weakRef = new WeakReference<>(new Object()); System.gc(); // 触发GC } } ``` 上述代码中,`tempValue`、`localObject`(在设为 `null` 后)、以及 `weakRef` 所引用的对象都不会成为 GC Roots
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值