算法之美
来源:互联网 发布:淘宝立即购买灰色 编辑:程序博客网 时间:2024/04/25 21:33
001 HDU1000 A+B Problem
链接:A+B Problem
C++:
# include<iostream>using namespace std;int main(){int a,b;while(cin>>a>>b)cout<<(a+b)<<endl;return 0;}
C:
#include<stdio.h>int main(){int a,b;while(scanf("%d %d",&a,&b) != EOF){printf("%d\n",a + b);}}
002 HDU1001 Sum Problem
链接:Sum Problem
C++:
#include<iostream>using namespace std;int main(){ int i,n,sum; while(cin>>n) { sum = 0; for(i=0;i<=n;i++) { sum+=i; } cout<<sum<<endl<<endl; } return 0;}
003 HDU1002 A+B Problem II
链接:A + B Problem II
C:
#include<stdio.h>#include<string.h>int main(){ int t,i,j,n,m,x,y,k,d,ca; char a[1002],b[1002],c[1002]; scanf("%d",&n); ca = 1; while(n--) { d=0; j=0; scanf("%s %s",a,b); if(ca>1) printf("\n"); printf("Case %d:\n%s + %s = ",ca++,a,b); x=strlen(a); y=strlen(b); if(x==y) { for(i=x-1;i>=0;i--) { t=a[i]-'0'+b[i]-'0'+d; d=t/10; m=t%10; c[j]=m+48;j++; } c[j] = d; if(d) { c[j] = d+48; c[j+1] = 0; } } else if(x>y) { for(i=y-1;i>=0;i--) { t=a[i+x-y]-'0'+b[i]-'0'+d; d=t/10; m=t%10; c[j]=m+48;j++; } for(k=x-y-1;k>=0;k--) { t=d+a[k]-'0'; d=t/10; m=t%10; c[j]=m+48;j++; } c[j] = d; if(d) { c[j] = d+48; c[j+1] = 0; } } else{ for(i=x-1;i>=0;i--) { t=d+a[i]-'0'+b[i+y-x]-'0'; d=t/10; m=t%10; c[j]=48+m;j++; } for(k=y-1-x;k>=0;k--) { t=d+b[k]-'0'; d=t/10; m=t%10; c[j]=m+48;j++; } c[j] = 0; if(d) { c[j] = d+48; c[j+1] = 0; } } for(j=strlen(c)-1;j>=0;j--) { printf("%c",c[j]); } printf("\n"); } return 0;}
C++:
#include<stdio.h> #include<string.h> #include<algorithm> #include<stdlib.h> #define maxn 1005 using namespace std; char ans[maxn]; int anslen; int main() { int cas; char s1[maxn],s2[maxn]; scanf("%d",&cas); for(int i=1;i<=cas;i++) { memset(s1,0,sizeof(s1)); memset(s2,0,sizeof(s2)); scanf("%s%s",s1,s2); printf("Case %d:\n",i); printf("%s + %s = ",s1,s2); // plus START int len1=strlen(s1); int len2=strlen(s2); for(int ios=0;ios<len1;ios++) s1[ios]-='0'; for(int android=0;android<len2;android++) s2[android]-='0'; int mid1=len1>>1; int mid2=len2>>1; //printf("%d %d %d %d\n",len1,len2,mid1,mid2); for(int ios=0;ios<mid1;ios++) swap(s1[ios],s1[len1-1-ios]); for(int android=0;android<mid2;android++) swap(s2[android],s2[len2-1-android]); anslen=max(len1,len2); //printf("\n anslen= %d\n",anslen); int s=0,sum; for(int ios=0;ios<=anslen;ios++) { sum=(s1[ios]+s2[ios]+s); ans[ios]=sum%10; s=sum/10; } // plus END // print answer START //if(ans[anslen+1]==0) while(anslen>0&&(ans[anslen]==0))anslen--; //else printf("%d",ans[anslen+1]); if(anslen==0) { printf("%d\n",ans[0]); //return; } for(int ios=anslen;ios>-1;ios--) { printf("%d",ans[ios]); } printf("\n"); // print answer END if(i!=cas)printf("\n"); } return 0; }
004 HDU1003 Max Sum
链接:Max Sum
C++:
#include<iostream>#include<cstdio>using namespace std;const int kInf (200000000);int main(){ /* freopen("data.in","r",stdin); freopen("data.out","w",stdout); //*/ int T; cin>>T; for(int k=1;k<=T;k++) { int n; int sum(-kInf),now_left(0),now_right(0); int ans_value(-kInf),ans_left(now_left),ans_right(now_right); cin>>n; for(int i=1;i<=n;i++) { int t; cin>>t; if(sum+t<t) { sum=t; now_left=now_right=i; } else if(sum+t==t) { sum=t; now_right++; } else if(sum+t>t) { sum+=t; now_right++; } if(ans_value<sum || (ans_value==sum && ans_left>now_left) || (ans_value==sum && ans_left==now_left && ans_right>now_right)) { ans_value=sum; ans_left=now_left; ans_right=now_right; } } if(k!=1) cout<<endl; cout<<"Case "<<k<<":"<<endl; cout<<ans_value<<" "<<ans_left<<" "<<ans_right<<endl; }return 0;}
005 HDU1004 Let the Balloon Rise
链接:Let the Balloon Rise
#include<iostream>#include<string>const int MAX=1000;using namespace std;int main(){int T,i,j,k;char a[MAX][16]; int b[MAX]; bool map[MAX]; while(1) { cin>>T; getchar(); if(T==0) break; for(i=0;i<T;i++) { scanf("%s",&a[i]); b[i]=0; map[i]=1; } for(j=0;j<T-1;j++) { for(k=j+1;k<T;k++) { if(strcmp(a[j],a[k])==0 && map[k]==map[j]==1) { b[j]++; map[k]=0; } } } int max1=-1; int num; for(i=0;i<T;i++) { if(b[i]>max1) { max1=b[i]; num=i; } } printf("%s\n",a[num]);}return 0;}
006 HDU1005 Number Sequence
链接:Number Sequence
#include <stdio.h>#include <stdlib.h>int f[51];int main () { int a, b, n; while ( scanf ("%d %d %d", &a , &b, &n) != EOF && a != 0 && b != 0 && n != 0 ) { f[1] = f[2] = 1; int i; for (i = 3; i < 51; i ++) { f[i] = (a * f[i - 1] + b * f[i - 2]) % 7; if ( f[i] == 1 && f[i - 1] == 1 ) { break; } } n = n % (i - 2); if (n == 0) printf ("%d\n", f[i - 2]); else printf ("%d\n", f[n]); } return 0;}
C++:
#include <iostream>using namespace std;int main(){ int f[100]; long A,B,n; int r; f[0]=f[1]=1; cin >> A >> B >> n; while(A&&B&&n) { int i=2; r = 7; while(true) { if(i==n) break; f[i] = (A*f[i-1]+B*f[i-2])%7; int j; for(j=i-1;j>0;j--) { if(f[j]==f[i]&&f[j-1]==f[i-1]) break; } if(j==0) i++; else { r = f[(n-1-i)%(i-j)+j]; break; } } if(r==7) cout << f[n-1] << endl; else cout << r << endl; cin >> A >> B >> n; } return 0;}
007 获取系统时间
#include<stdio.h>#include<time.h>#include<mem.h>int main(){ time_t timer; char dateTime[] = "2015-02-13 11:55:66"; struct tm*t_tm; time( & timer); t_tm = localtime( & timer); memset(dateTime, 0, sizeof(dateTime)); printf("%4d-%02d-%02d %02d:%02d:%02d", t_tm -> tm_year + 1900, t_tm -> tm_mon + 1, t_tm -> tm_mday, t_tm -> tm_hour, t_tm -> tm_min, t_tm -> tm_sec); return 0;}
转载请注明出处:周木水的CSDN博客
0 0
- 算法之美
- 算法之美
- 算法之美
- 第一章 算法之美
- 《算法之美》---插入排序
- 《算法之美》---归并排序
- 《算法之美》---堆排序
- 算法之美:动态规划
- 算法之美-冒泡排序
- 【算法】算法之美—Fire Net
- 【算法】算法之美—Crashing Balloon
- 【算法】算法之美—Jugs
- 算法之美——算法复杂性
- 趣学算法系列-算法之美
- 编程之美之实时排名算法
- 数学之美之TF-IDF算法
- 《算法之美》---二叉搜索树
- 《算法之美》---二叉堆及其实现
- 霍夫曼编码压缩
- 蓝桥杯 ALGO-105 黑色星期五(日期)
- WindowsPhone开发如何创建动态启动界面
- 算法——归并排序
- proc_day01
- 算法之美
- iOS系统的一些单例类
- proc_day02
- android开发错误之“ Connection with adb was interrupted”
- 5.8在Mifare S50中,如果第2区的访问控制码为FF 08 70 69,则该区块1和3的访问控制条件是什么
- ImageCache 图片缓存强引用
- proc_day03
- [Leetcode]Symmetric Tree
- Java中字符串对象和基本数据类型包装类