2009-12-5

来源:互联网 发布:人大网络教育学生登录 编辑:程序博客网 时间:2024/04/25 21:21

这是昨天参加院比赛写的题,这是我第一次参加acm的比赛,也是我第二次使用OJ,呵呵,很纠结的3个小时,acm,想说爱你不容易。

Problem A

Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 65536/32768K (Java/Other)
Total Submission(s) : 31   Accepted Submission(s) : 10

Font: Times New Roman | Verdana | Georgia

Font Size:

Problem Description

Given an positive integer A (1 <= A <= 100), output the lowest bit of A.

For example, given A = 26, we can write A in binary form as 11010, so the lowest bit of A is 10, so the output should be 2.

Another example goes like this: given A = 88, we can write A in binary form as 1011000, so the lowest bit of A is 1000, so the output should be 8.

Input

Each line of input contains only an integer A (1 <= A <= 100). A line containing "0" indicates the end of input, and this line is not a part of the input data.

Output

For each A in the input, output a line containing only its lowest bit.

Sample Input

26880

Sample Output

28
我的代码:
//用栈
#include <iostream>
using namespace std;
int main()
{
 int a;
 while (cin>>a)
 {
  if (a==0)
  {
   exit(0);
  }
  else
  {
   int i[7];
   for (int m=0;m<8;m++)
   {
    i[m]=a%2;
    a=a/2;
   }
   m=0;
         while (i[m]!=1)
   {
    m++;
   }
   int x=1;
   for (;m>0;m--)
   {
    x=x*2;
   }
   cout<<x<<endl;
  }
 }
 return 0;
}
结果:
003092442009-12-05 14:49:29Accepted10010 MS272 KBVisual C++cc
心得体会:
当时想的,求进制转换,就用栈嘛,但是,栈是刚刚学习的,要在有限的时间里还编一个跟
书上一样的栈结构,觉得不太可能。所以在思考了一下以后,决定用简单的数组来解决这个问题。
当时又把while(cin>>a)给漏了。。。

Problem Description

给你两个集合,要求{A} + {B}.
注:同一个集合中不会有两个相同的元素.

Input

每组输入数据分为三行,第一行有两个数字n,m(0<n,m<=10000),分别表示集合A和集合B的元素个数.后两行分别表示集合A和集合B.每个元素为不超出int范围的整数,每个元素之间有一个空格隔开.

Output

针对每组数据输出一行数据,表示合并后的集合,要求从小到大输出,每个元素之间有一个空格隔开.

Sample Input

1 212 31 211 2

Sample Output

1 2 31 2
我的代码:
#include <iostream>
using namespace std;
int main()
{
 int a,b;
 while(cin>>a>>b)
 {
    int *m=(int *)malloc(a*sizeof(int));
 int *n=(int *)malloc(b*sizeof(int));
    for (int i=0;i<a;i++)
 {
  cin>>*(m+i);
 }
 for (i=0;i<b;i++) 
 {
  cin>>*(n+i);
 }
 int j;
 char *x=(char *)malloc(a);
 char *y=(char *)malloc(b);
 int h=0;
 int *t=(int *)malloc((a+b)*sizeof(int));
//输出ab中一样的
 for (i=0;i<a;i++)
 {
  for (j=0;j<b;j++)
  {
   if( *(m+i) == *(n+j))
   {
    //cout<<*(m+i)<<" ";
    *(x+i)='*';
    *(y+j)='*'; 
    *(t+h)=*(m+i);
    h++;
   }
  }
 }
    for (i=0;i<a;i++)
 {
  if(*(x+i)!='*')
  { *(t+h)=*(m+i);
  h++;
   
  }
  // cout<<*(m+i)<<" ";  
 }
 for (j=0;j<b;j++)
 {
  if(*(y+j)!='*')
  {
   *(t+h)=*(n+j);
   h++;
  }
  // cout<<*(n+j)<<" ";  
 }
 int z;
 for(i=0;i<h-1;i++)
 {
  for(j=i+1;j<h;j++)
  {
   if(*(t+i) > *(t+j))
   {
    z=*(t+j);
    *(t+j)=*(t+i);
    *(t+i)=z;
   }
  }
 }
 
 for (i=0;i<h-1;i++)
    cout<<*(t+i)<<" ";
 cout<<*(t+i)<<endl;
 }
 return 0;
}
结果:
003095302009-12-05 16:52:19Accepted10021390 MS468 KBVisual C++cc
心得:
   受不了了,本来这题很早就能写出来,就是因为排序的代码偷懒,从书上直接抄了一个
下来,结果排序就错误了,用简单的例子根本试不出,但是它就说你是RA,弄了好久,因为
使用的数据测试都是正确的,所以就不知道问题在哪,顿时万念俱灰,不想做了,磨了好久
,后来学长告知一组数据,检测出原来排序错了,后来自己写了一段排序,很简单的几行,
却耽误了这么久时间

Problem Description

8600的手机每天消费1元,每消费K元就可以获赠1元,一开始8600有M元,问最多可以用多少天?

Input

输入包括多个测试实例.每个测试实例包括2个整数M, k,(2 <= k <= M <= 1000).M = 0, k = 0代表输入结束.

Output

对于每个测试实例输出一个整数,表示M元可以用的天数。

Sample Input

2 24 30 0

Sample Output

35
我的代码:
#include <iostream>
using namespace std;
int main()
{
 int m,k;
 while(cin>>m>>k)
 {
  if(m==0  && k==0)exit(0);
  else
  {
   int t=0;
   int a,b;
   if(m<k)
   {
    t=t+m;
   }
   else
   {
here:
   a=m/k;
   b=m%k;
     
   t=t+a*k;
              
   if((a+b) >= k)
   {
    m=a+b;
    goto here;
   }
   else
   {
    t=t+a+b;
   }
  
   }
     cout<<t<<endl;
  }
  
 }
 return 0;
}
结果:
003095762009-12-05 17:29:45Accepted100315 MS280 KBVisual C++cc
心得:这题还比较简单,解题要点就在于没有搞清楚其中的算法,对此题可以看出我的循环问题不是很
精通,熟练,所以无奈之下我用了goto。
原创粉丝点击