年龄巧合分离数,报时助手字符串数组,大数加法
标题:年龄巧合
? ??? 这是一个4位整数,请通过浏览器提交答案,不要填写任何多余的内容(比如,他表弟的出生年份,或是他们的年龄等等) ? #include <iostream> using namespace std; int main() { int a,b,c,d; for(int i=1900; i<=2014; i++) { a=i/1000; b=i/100%10; c=i/10%10; d=i%10; if(a+b+c+d==2014-i) cout<<i<<endl; } return 0; }
思路:将h和m分别调用 #include <iostream> using namespace std; string en[]= {"zero","one","two","three","four","five","six","seven","eight","nine","ten","eleven","twelve","thirteen","fourteen","fifteen","sixteen","seventeen","eighteen","nineteen","twenty","thirty","forty","fifty" }; string r="o'clock"; int h,m; void numtoen(int n) { if(n<=20) cout<<en[n]<<" "; else{ int gao=n/10; int di=n%10; cout<<en[gao+18]<<" "; if(di!=0) { cout<<en[gao+18]<<" "<<en[di]<<" "; } } } void output(int h,int m) { if(m==0) { numtoen(h); cout<<r; cout<<endl; } else{ numtoen(h); numtoen(m); cout<<endl; } } int main() { cin>>h>>m; output(h,m); return 0; }
大数加法
#include <iostream> #include<cstdio> #include<string.h> using namespace std; char a[102],b[102]; int sum[103]; int main() { scanf("%s%s",a,b); int i,j; int lena=strlen(a); int lenb=strlen(b); for(i=0; i<lena; i++) sum[i]=a[lena-1-i]-'0'; for(i=0; i<lenb; i++) sum[i]+=b[lenb-1-i]-'0';///求出了个位,十位,百位和 for(j=0; j<104; j++) { if(sum[j]>9) { sum[j+1]+=sum[j]/10; sum[j]%=10; } } i=104; while(sum[i]==0)///用while而不是if i--; for(int k=i; k>=0; k--) cout<<sum[k]; return 0; } (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |