1.The Biggest Water Problem
思路:直接暴力,但是后来发现标签是递归;
暴力代码
#include <bits/stdc++.h>
using namespace std;
const int N=1e6;
int n,m;
void solve()
{
cin>>n;
while(n>9)
{
int ans=0;
while(n>0)
{
ans+=n%10;
n/=10;
}
n=ans;
}
cout<<n<<endl;
}
int main()
{
solve();
return 0;
}
递归代码
#include <bits/stdc++.h>
using namespace std;
const int N=1e6;
int n;
int ans(int n)
{
if(n<10) return n;
else
{
int sum=0;
while(n>0)
{
sum+=n%10;
n/=10;
}
return ans(sum);
}
}
int main()
{
cin>>n;
cout<<ans(n)<<endl;
return 0;
}