hdu 1013
来源:互联网 发布:mac 重装 u盘 编辑:程序博客网 时间:2024/06/14 20:06
http://acm.hdu.edu.cn/showproblem.php?pid=1013
题意简单:就是把一个数取每一位相加,如果相加后ans>=10,则重复前面步骤,直到ans<10;
一开始果断int 高兴提交wa,比如这样
#include<iostream>#include<cstring>#include<cstdio>using namespace std;int a[105];int f1(int n){ int t; int ans=0; while(n) { t=n%10; n=n/10; ans+=t; } return ans;}int f2(int ans){ if(ans<10) return ans; else ans=f1(ans);}int main(){ int n; int res; while(~scanf("%d",&n)&&n) { n=f1(n); res=f2(n); printf("%d\n",res); } return 0;}
然后想一下估计n太大不行,那就字符串吧,于是乎就过了
附上正确代码:
#include<iostream>#include<cstring>#include<cstdio>using namespace std;char a[1005];int main(){ int i,ans; while(~scanf("%s",a)) { if(a[0]=='0') break; ans=0; for(i=0;a[i]!='\0';i++) { ans+=a[i]-'0'; if(ans>9) { ans=ans%10+ans/10; } } printf("%d\n",ans); } return 0;}
0 0
- hdu 1013
- hdu 1013
- hdu 1013
- HDU 1013
- hdu 1013
- hdu 1013
- hdu 1013
- HDU-1013
- hdu 1013
- HDU 1013
- HDU 1013
- HDU 1013
- hdu 1013
- hdu 1013
- HDU-1013
- HDU 1013
- hdu 1013
- HDU 1013
- leetcode122 Best Time to Buy and Sell Stock II
- CSS3 Animation 帧动画
- POJ 2449 Remmarguts' Date(第k短路+spfa+A*搜索)
- python 对文件做类别标签
- HDOJ 2010 水仙花数
- hdu 1013
- yum 快速搭建lnmp环境
- 水生火热的互联网金融
- Java 图像 处理
- vc++打开文件夹时,指定初始路径
- animate.css强大的动效库
- 2015-11-06-定
- D Hello World
- 【dfs】hdu 5546 Ancient Go