LeetCode:Happy Numbers
Unknown
Problem :
https://leetcode.com/problems/happy-number/#include<bits/stdc++.h> using namespace std; class Solution { public: bool isHappy(int n) { map<int,int>a; while(a[n]!=1) { a[n]=1; int sum=0,res=0; while(n) { res=n%10; sum+=(res*res); n/=10; } if(sum==1) return true; else { n=sum; } } return false; } }; int main() { Solution s; cout<<s.isHappy(19)<<endl; cout<<s.isHappy(301)<<endl; }
10:15
Leet Code
SPOJ:ABCDEF
Unknown
Problem :
http://www.spoj.com/problems/ABCDEF/#include<cstdio> #include<cstring> #include<iostream> #include<algorithm> using namespace std; #define ll long long #define rl(x) scanf("%lld",&x) ll a1[105]; ll arr1[2000000]; int main() { ll n,temp1=0,v,ans=0; rl(n); for(int i=0;i<n;i++) rl(a1[i]); for(int a=0;a<n;a++) if(a1[a]) for(int b=0;b<n;b++) for(int c=0;c<n;c++) arr1[temp1++]=(a1[a]*(a1[b]+a1[c])); sort(arr1,arr1+temp1); for(int d=0;d<n;d++) for(int e=0;e<n;e++) for(int f=0;f<n;f++) { v=a1[d]*a1[e]+a1[f]; ans+=(upper_bound(arr1,arr1+temp1,v))-(lower_bound(arr1,arr1+temp1,v)); } cout<<ans<<endl; }