HDU1013-Digital Roots-數論(樹根),digitalroots

來源:互聯網
上載者:User

HDU1013-Digital Roots-數論(樹根),digitalroots

題目連結:http://acm.hdu.edu.cn/showproblem.php?pid=1013

題意:

就是把數的每個位元相加,看是否小於10,是就輸出,否則繼續。

分析:

本題不難,讓我們做了是為了讓我們知道一個結論。一個數(n)的樹根k=(n-1)%9-1;

知道這個這題就是水題了。

不知道也沒什麼關係,也可以水過。(輸入的數可能很大)

數論代碼:

#include<iostream>#include<cstdio>#include<string>#include<cstring>using namespace std;int main(){    char s[1005];    while(scanf("%s",s)&&s[0]!='0'){        int n=0;        for(int i=0;i<strlen(s);i++) n+=s[i]-'0';        printf("%d\n",(n-1)%9+1);    }    return 0;}

普通代碼:

#include<stdio.h>#include<string.h>int root(int n){int r=0;while(n>0){r+=n%10;n/=10;}if(r>9) root(r);else return r;}int main(){char st[1005];int n,len;while(scanf("%s",st)&&st[0]!='0'){         n=0; len=strlen(st); for(int i=0;i<len;i++)  n+=st[i]-'0'; printf("%d\n",root(n));}return 0;}

#include<stdio.h>#include<string.h>int root(int n){int r=0;while(n>0){r+=n%10;n/=10;}if(r>9) root(r);else return r;}int main(){char st[1005];int n,len;while(scanf("%s",st)&&st[0]!='0'){         n=0; len=strlen(st); for(int i=0;i<len;i++)  n+=st[i]-'0'; printf("%d\n",root(n));}return 0;}


 

 

著作權聲明:本文為博主原創文章,未經博主允許不得轉載。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.