面试试题

[cpp]  view plain copy
  1. /************************************************************************/  
  2. /* A+B 
  3.  
  4. Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) 
  5. Total Submission(s): 604    Accepted Submission(s): 380 
  6.  
  7.  
  8. Problem Description 
  9. 给定两个整数A和B,其表示形式是:从个位开始,每三位数用逗号","隔开。 
  10. 现在请计算A+B的结果,并以正常形式输出。 
  11.  
  12.  
  13. Input 
  14. 输入包含多组数据数据,每组数据占一行,由两个整数A和B组成(-10^9 < A,B < 10^9)。 
  15.  
  16.  
  17. Output 
  18. 请计算A+B的结果,并以正常形式输出,每组数据占一行。 
  19.  
  20.  
  21. Sample Input 
  22. -234,567,890 123,456,789 
  23. 1,234 2,345,678 
  24.  
  25.  
  26. Sample Output 
  27. -111111101 
  28. 2346912 
  29.  
  30.  
  31. Source 
  32. 浙大计算机研究生复试上机考试-2010年 
  33. */  
  34. /************************************************************************/  
  35.   
  36.   
  37.   
  38. #include"iostream"  
  39. #include "string.h"  
  40. #include "math.h"  
  41. using namespace std;  
  42.   
  43. int trans(char *input)  
  44. {  
  45.     //printf("%s",input);  
  46.     int res=0;  
  47.     int i,j;  
  48.     for (i=0;i<strlen(input);i++)  
  49.     {  
  50.         if(input[i]>='0'&&input[i]<='9')  
  51.         {  
  52.             res*=10;  
  53.             res+=(input[i]-'0');  
  54.         }  
  55.     }  
  56.     if(input[0]=='-')  
  57.         res=-res;  
  58.     return res;  
  59. }  
  60.   
  61. int main()  
  62. {  
  63.     char *inp1,*inp2;  
  64.     inp1=(char*)malloc(15*sizeof(char));  
  65.     inp2=(char*)malloc(15*sizeof(char));  
  66.     while(scanf("%s",inp1)!=EOF)  
  67.     {  
  68.         scanf("%s",inp2);  
  69.   
  70.         int a=trans(inp1);  
  71.         int b=trans(inp2);  
  72.         printf("%d\n",a+b);  
  73.     }  
  74. }  


 

 

 

[cpp]  view plain copy
  1. /************************************************************************/  
  2. /*                                   ZOJ问题 
  3.  
  4. Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) 
  5. Total Submission(s): 657    Accepted Submission(s): 201 
  6.  
  7.  
  8. Problem Description 
  9. 对给定的字符串(只包含'z','o','j'三种字符),判断他是否能AC。 
  10.  
  11. 是否AC的规则如下: 
  12. 1. zoj能AC; 
  13. 2. 若字符串形式为xzojx,则也能AC,其中x可以是N个'o' 或者为空; 
  14. 3. 若azbjc 能AC,则azbojac也能AC,其中a,b,c为N个'o'或者为空; 
  15.  
  16.  
  17. Input 
  18. 输入包含多组测试用例,每行有一个只包含'z','o','j'三种字符的字符串,字符串长度小于等于1000; 
  19.  
  20.  
  21. Output 
  22. 对于给定的字符串,如果能AC则请输出字符串“Accepted”,否则请输出“Wrong Answer”。 
  23.  
  24.  
  25. Sample Input 
  26. zoj 
  27. ozojo 
  28. ozoojoo 
  29. oozoojoooo 
  30. zooj 
  31. ozojo 
  32. oooozojo 
  33. zojoooo 
  34.  
  35.  
  36. Sample Output 
  37. Accepted 
  38. Accepted 
  39. Accepted 
  40. Accepted 
  41. Accepted 
  42. Accepted 
  43. Wrong Answer 
  44. Wrong Answer 
  45.  
  46.  
  47. Source 
  48. 浙大计算机研究生复试上机考试-2010年 
  49.  
  50.                                    */  
  51. /************************************************************************/  
  52. /*analyse: 
  53. Accept情况:(x----n个o,n>=1) 
  54. 1. xzojx 
  55. 2. (x)z(o*n)j(x*n) 
  56. */  
  57.   
  58.   
  59. #include "iostream"  
  60. #include "string"  
  61. using namespace std;  
  62.   
  63. int main()  
  64. {  
  65.     char* inp;  
  66.     int o1,o2,o3,z,j,i;  
  67.     int flagz,flagj,flag;  
  68.     inp=(char*)malloc(1020*sizeof(char));  
  69.     while(gets(inp))  
  70.     {  
  71.         flagz=flagj=flag=0;  
  72.         for(i=0;i<strlen(inp);i++)  
  73.         {  
  74.             if(inp[i]=='z')  
  75.                 flagz++,z=i;  
  76.             else if(inp[i]=='j')  
  77.                 flagj++,j=i;  
  78.             else if(inp[i]!='o')  
  79.                 flag=2;  
  80.         }  
  81.         if(flagz!=1||flagj!=1||flag!=0)  
  82.         {  
  83.             puts("Wrong Answer");  
  84.             continue;  
  85.         }  
  86.         o1=z;  
  87.         o2=j-z-1;  
  88.         o3=strlen(inp)-j-1;  
  89.         if(o1*o2==o3&&o2>=1)  
  90.             puts("Accepted");  
  91.         else  
  92.             puts("Wrong Answer");         
  93.     }  
  94. }  


 

 

 

[cpp]  view plain copy
  1. /************************************************************************/  
  2. /* 奥运排序问题 
  3.  
  4. Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) 
  5. Total Submission(s): 690    Accepted Submission(s): 167 
  6.  
  7.  
  8. Problem Description 
  9. 按要求,给国家进行排名。 
  10.  
  11.  
  12. Input 
  13. 有多组数据。 
  14. 第一行给出国家数N,要求排名的国家数M,国家号从0到N-1。 
  15. 第二行开始的N行给定国家或地区的奥运金牌数,奖牌数,人口数(百万)。 
  16. 接下来一行给出M个国家号。 
  17.  
  18.  
  19. Output 
  20. 排序有4种方式: 金牌总数 奖牌总数 金牌人口比例 奖牌人口比例  
  21. 对每个国家给出最佳排名排名方式 和 最终排名 
  22. 格式为: 排名:排名方式 
  23. 如果有相同的最终排名,则输出排名方式最小的那种排名,对于排名方式,金牌总数 < 奖牌总数 < 金牌人口比例 < 奖牌人口比例  
  24. 如果有并列排名的情况,即如果出现金牌总数为 100,90,90,80.则排名为1,2,2,4. 
  25. 每组数据后加一个空行。 
  26.  
  27.  
  28. Sample Input 
  29. 4 4 
  30. 4 8 1 
  31. 6 6 2 
  32. 4 8 2 
  33. 2 12 4 
  34. 0 1 2 3 
  35. 4 2 
  36. 8 10 1 
  37. 8 11 2 
  38. 8 12 3 
  39. 8 13 4 
  40. 0 3 
  41.  
  42.  
  43. Sample Output 
  44. 1:3 
  45. 1:1 
  46. 2:1 
  47. 1:2 
  48.  
  49. 1:1 
  50. 1:1 
  51.  
  52.  
  53. Source 
  54. 浙大计算机研究生复试上机考试-2010年 
  55.                                                                      */  
  56. /************************************************************************/  
  57.   
  58.   
  59. #include "iostream"  
  60. using namespace std;  
  61.   
  62. struct Nation  
  63. {  
  64.     int jin;  
  65.     int jiang;  
  66.     int people;  
  67.     int index;  
  68.     int num[6];//num[0,1,2,3]record sort res,num[4]record min sort num, num[5] record min sort mode  
  69. };  
  70.   
  71. int mode;  
  72. int comp(const void *A,const void* B)  
  73. {  
  74.     struct Nation* a=(Nation*)A;  
  75.     struct Nation* b=(Nation*)B;  
  76.   
  77.     switch(mode)  
  78.     {  
  79.     case 1:      
  80.         return b->jin-a->jin;  
  81.     case 2:  
  82.         return b->jiang-a->jiang;  
  83.     case 3:  
  84.         return b->jin*a->people-a->jin*b->people;  
  85.     case 4:  
  86.         return b->jiang*a->people-a->jiang*b->people;  
  87.     }  
  88. }  
  89.   
  90. bool Same(Nation a,Nation b,int mode)  
  91. {  
  92.     switch(mode)  
  93.     {  
  94.     case 1:  
  95.         return a.jin==b.jin;  
  96.     case 2:  
  97.         return a.jiang==b.jiang;  
  98.     case 3:  
  99.         return a.jin*b.people==b.jin*a.people;  
  100.     case 4:  
  101.         return a.jiang*b.people==b.jiang*a.people;  
  102.     }  
  103. }  
  104.   
  105. int main()  
  106. {  
  107.     int n,m;  
  108.     while(scanf("%d%d",&n,&m)!=EOF)  
  109.     {  
  110.         int i,j,k=0,t;  
  111.         Nation* country=(Nation*)malloc(n*sizeof(Nation));  
  112.         Nation* c=(Nation*)malloc((m+10)*sizeof(Nation));  
  113.         Nation* res=(Nation*)malloc((m+10)*sizeof(Nation));  
  114.         for (i=0;i<n;i++)  
  115.         {  
  116.             scanf("%d%d%d",&country[i].jin,&country[i].jiang,&country[i].people);  
  117.         }  
  118.         for (i=0;i<m;i++)  
  119.         {  
  120.             scanf("%d",&t);  
  121.             c[k]=country[t];  
  122.             for(j=0;j<5;j++)  
  123.                 c[k].num[j]=m+1;  
  124.             c[k++].index=i;  
  125.         }  
  126.         for (mode=4;mode>=1;mode--)  
  127.         {  
  128.             qsort(c,k,sizeof(c[0]),comp);  
  129.             c[0].num[mode-1]=1;  
  130.             for(i=1;i<k;i++)  
  131.                 c[i].num[mode-1]=Same(c[i],c[i-1],mode)?c[i-1].num[mode-1]:(i+1);  
  132.         }  
  133.         for (i=0;i<k;i++)  
  134.         {  
  135.             for (mode=4;mode>0;mode--)  
  136.             {  
  137.                 if (c[i].num[mode-1]<=c[i].num[4])  
  138.                 {  
  139.                     c[i].num[4]=c[i].num[mode-1];  
  140.                     c[i].num[5]=mode;  
  141.                 }  
  142.             }  
  143.             res[c[i].index]=c[i];  
  144.         }  
  145.         for(i=0;i<m;i++)  
  146.             printf("%d:%d\n",res[i].num[4],res[i].num[5]);  
  147.         cout<<endl;  
  148.     }  
  149. }  


 

 

 

[cpp]  view plain copy
  1. /************************************************************************/  
  2. /* 最短路径问题 
  3.  
  4. Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) 
  5. Total Submission(s): 2114    Accepted Submission(s): 659 
  6.  
  7.  
  8. Problem Description 
  9. 给你n个点,m条无向边,每条边都有长度d和花费p,给你起点s终点t,要求输出起点到终点的最短距离及其花费,如果最短距离有多条路线,则输出花费最少的。 
  10.  
  11.  
  12. Input 
  13. 输入n,m,点的编号是1~n,然后是m行,每行4个数 a,b,d,p,表示a和b之间有一条边,且其长度为d,花费为p。最后一行是两个数 s,t;起点s,终点。n和m为0时输入结束。 
  14. (1<n<=1000, 0<m<100000, s != t) 
  15.  
  16.  
  17. Output 
  18. 输出 一行有两个数, 最短距离及其花费。 
  19.  
  20.  
  21. Sample Input 
  22. 3 2 
  23. 1 2 5 6 
  24. 2 3 4 5 
  25. 1 3 
  26. 0 0 
  27.  
  28.  
  29. Sample Output 
  30. 9 11 
  31.  
  32.  
  33. Source 
  34. 浙大计算机研究生复试上机考试-2010年 
  35.                                                                      */  
  36. /************************************************************************/  
  37.   
  38. //这个是SPFA法,Dijkstra法见下个代码  
  39. #include"iostream"  
  40. #include"queue"  
  41. #include"vector"  
  42. using namespace std;  
  43.   
  44. #define N 1005  
  45. #define INF 100000000  
  46. int n,m;  
  47.   
  48. struct MAP  
  49. {  
  50.     int node;  
  51.     int dis;  
  52.     int cost;  
  53.     MAP(int a,int d,int p)  
  54.     {  
  55.         node=a,dis=d,cost=p;  
  56.     }  
  57. };  
  58. vector<MAP>map[N];//map[i][j]表与节点i相连的第j条边的信息  
  59. int minres[N][2];//min_dis/cost from s to i  
  60.   
  61. void spfa(int s,int e)  
  62. {  
  63.     queue<int>Q;  
  64.     bool used[N]={false};  
  65.     Q.push(s);  
  66.     used[s]=true;  
  67.     int i;  
  68.     for(i=1;i<=n;i++)  
  69.         minres[i][0]=minres[i][1]=INF;  
  70.     minres[s][0]=minres[s][1]=0;  
  71.     while(!Q.empty())  
  72.     {  
  73.         int now=Q.front();  
  74.         Q.pop();  
  75.         used[now]=false;//  
  76.         for(i=0;i<map[now].size();i++)  
  77.         {  
  78.             int tmpend=map[now][i].node;  
  79.             int dis=map[now][i].dis;  
  80.             int cost=map[now][i].cost;  
  81.   
  82.             if(minres[tmpend][0]>minres[now][0]+dis||  
  83.                 (minres[tmpend][0]==minres[now][0]+dis&&  
  84.                 minres[tmpend][1]>minres[now][1]+cost))  
  85.             {  
  86.                 minres[tmpend][0]=minres[now][0]+dis;  
  87.                 minres[tmpend][1]=minres[now][1]+cost;  
  88.                 if(!used[tmpend])  
  89.                     Q.push(tmpend);  
  90.                 used[tmpend]=true;  
  91.             }  
  92.         }  
  93.     }  
  94. }  
  95.   
  96. int main()  
  97. {  
  98.     while(scanf("%d %d",&n,&m)!=EOF&&!(m==0&&n==0))  
  99.     {  
  100.         int a,b,d,p,i,j;  
  101.         for (i=1;i<=n;i++)  
  102.             map[i].clear();  
  103.         while (m--)  
  104.         {  
  105.             scanf("%d%d%d%d",&a,&b,&d,&p);  
  106.             map[a].push_back(MAP(b,d,p));//注意双向边  
  107.             map[b].push_back(MAP(a,d,p));  
  108.         }  
  109.         scanf("%d%d",&a,&b);  
  110.         spfa(a,b);  
  111.         printf("%d %d\n",minres[b][0],minres[b][1]);  
  112.     }  
  113. }  


 

 

 

 

[cpp]  view plain copy
  1. #include"iostream"  
  2. #include"vector"  
  3. using namespace std;  
  4. #define N 1005  
  5. #define INF 1000000000  
  6. int n,m;  
  7.   
  8. struct Map  
  9. {  
  10.     int point;//another point of edges connect this point  
  11.     int dis;  
  12.     int cost;  
  13.     Map(int x,int y,int z):point(x),dis(y),cost(z){}  
  14. };  
  15. vector<Map>map[N];  
  16. int minres[N][2];  
  17.   
  18. void bfs(int s,int e)  
  19. {  
  20.     int i,j;  
  21.     for(i=1;i<=n;i++)  
  22.         minres[i][0]=minres[i][1]=INF;  
  23.     minres[s][0]=minres[s][1]=0;  
  24.   
  25.     bool used[N]={false};//false--in the end point set  
  26.       
  27.     for(j=0;j<n;j++)//n loops  
  28.     {  
  29.         int mindis,mincost,tmp;  
  30.         mindis=mincost=INF;  
  31.         for(i=1;i<=n;i++)//find a point with min dis and min cost  
  32.         {  
  33.             if(!used[i]&&(minres[i][0]<mindis||minres[i][0]==mindis&&minres[i][1]<mincost))  
  34.             {  
  35.                 mindis=minres[i][0];  
  36.                 mincost=minres[i][1];  
  37.                 tmp=i;  
  38.             }  
  39.         }  
  40.         int p1,d1,c1;  
  41.         //update paths connect to point tmp  
  42.   
  43.         for (i=0;i<map[tmp].size();i++)  
  44.         {  
  45.             p1=map[tmp][i].point;  
  46.             d1=map[tmp][i].dis;  
  47.             c1=map[tmp][i].cost;  
  48.             if((minres[p1][0]>minres[tmp][0]+d1||  
  49.                 minres[p1][0]==minres[tmp][0]+d1&&minres[p1][1]>minres[tmp][1]+c1)&&  
  50.                 !used[p1])  
  51.             {  
  52.                 minres[p1][0]=minres[tmp][0]+d1;  
  53.                 minres[p1][1]=minres[tmp][1]+c1;  
  54.             }  
  55.         }     
  56.         used[tmp]=true;  
  57.     }  
  58. }  
  59.   
  60.   
  61. int main()  
  62. {  
  63.     while(scanf("%d%d",&n,&m)!=EOF&&!(m==0&&n==0))  
  64.     {   
  65.         int a,b,d,p;  
  66.         for (int i=1;i<=n;i++)  
  67.             map[i].clear();///千万注意!  
  68.         while(m--)  
  69.         {  
  70.             scanf("%d%d%d%d",&a,&b,&d,&p);  
  71.             map[a].push_back(Map(b,d,p));  
  72.             map[b].push_back(Map(a,d,p));  
  73.         }  
  74.         scanf("%d%d",&a,&b);  
  75.         bfs(a,b);  
  76.         printf("%d %d\n",minres[b][0],minres[b][1]);  
  77.     }  
  78. }  
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值