top1编程
← 返回题目
题解

【入门】数字之和

1 条题解

  • 0
    @ 2026-7-28 22:44:51
    #include<iostream>
    using namespace std;
    int main(){
    	int N,s=0;
    	cin>>N;
    	//方法一:分解各个位数上的数字
    	//方法二: 短除法 
    	while(N!=0) {  //循环条件 
    		//cout<<s; //输出该位上的数字 
    		s=s+N%10;      //各个位数上的和相加 
    		N=N/10; 
    			
    	}
    	cout<<s;
    	return 0;
    }
    
    • 1