周总结(第一周)(以后要加油哦,不能太懒散)

这周的题目并不是很难,而我将问题考虑的太过复杂,也有一些细节没有考虑到,所以导致提交多次仍是失败,在这里做一个反思

A - 字符排序

输入三个字符后,按各字符的ASCII码从小到大的顺序输出这三个字符。

Input
输入数据有多组,每组占一行,有三个字符组成,之间无空格。

Output
对于每组输入数据,输出一行,字符中间用一个空格分开。

Sample Input

qwe
asd
zxc

Sample Output

Sample Output

e q w
a d s
c x z

这道题是根据ASCII码排列,也就是根据字典排列,是三个数从小到大排列


#include <stdio.h>
#include <stdlib.h>
int main(void)
{
    char ch[4];
    while (scanf("%s", ch) == 1)
    {
        int i, j, k;
        int min = 0;
        for (i = 0; i < 3; i++)
        {
            for (j = 0; j < 3; j++)
            {
                   if (ch[min] > ch[j])
                    min = j;
            }
            printf("%c", ch[min]);
            ch[min] = ' ';
            for (k = 0; k < 3;k++)
            {
                if (ch[k] != ' ')
                {
                    min = k;
                    break;
                }
            }
            if (i != 2)
                printf(" ");
            else
                printf("\n");
        }
    }
}

B.球的体积
根据输入的半径值,计算球的体积。
Input
输入数据有多组,每组占一行,每行包括一个实数,表示球的半径。
Output
输出对应的球的体积,对于每组输入数据,输出一行,计算结果保留三位小数。
Sample Input

1
1.5

Sample Output

4.189
14.137
//在我第一次做这个题居然还想着用数组来解决这个问题,这个题使用数组的话需要担心,越界问题,还需要担心什么时候该退出,不该怎么样来想
#include<stdio.h>
#define PI 3.1415927
int main()
{
    double n ;
    while( scanf("%lf",&n) != EOF ) {
        printf("%.3lf\n",4.0/3*PI*n*n*n);
    }

    return 0;
}

C - 回文串

“回文串”是一个正读和反读都一样的字符串,比如“level”或者“noon”等等就是回文串。请写一个程序判断读入的字符串是否是“回文”。
Input
输入包含多个测试实例,输入数据的第一行是一个正整数n,表示测试实例的个数,后面紧跟着是n个字符串。
Output
如果一个字符串是回文串,则输出”yes”,否则输出”no”.
Sample Input

4
level
abcde
noon
haha

Sample Output

yes
no
yes
no
这道题可以使用二分法来进行判别,对字符串的中间来对两边进行对比,如果对比不相符即退出,不然就继续比较下一位;
#include<stdio.h>
#include<string.h>
int main()
{
    int N,i=0,j=0;
    char a[105];
    scanf("%d",&N);
    getchar();
    while(N--)
    {
        gets(a);
        i=0;
        j=strlen(a)-1;
        while(i<j)
        {
            if(a[i]!=a[j])
                break;
            i++;
            j--;
        }
        if(j<=i)
            printf("yes\n");
        else
            printf("no\n");
    }
    return 0;
}

在这里我一开始向借用的是gets函数,使用getchar();来接受缓冲区,后来发现getchar()的位置出现了错误,又改成这种,就可以通过了

D - 最小公倍数
给定两个正整数,计算这两个数的最小公倍数。
Input
输入包含多组测试数据,每组只有一行,包括两个不大于1000的正整数.
Output
对于每个测试用例,给出这两个数的最小公倍数,每个实例输出一行。
Sample Input

10 14

Sample Output

70
这个题我的思路是利用辗除法来求得最大公约数,最小公倍数等于两个数相乘然后再除最大公约数可得
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
int main()
{
    int a[10],b[10],m,n,c,t;
    int i=0,j=0;
    while(scanf("%d %d",&a[i],&b[i])!=EOF)
    {
        i++;
    }
    for(j=0;j<i;j++)
    {
        m=a[j];
       n=b[j];
     if(a[j]<b[j])
    {
         t=a[j];
         a[j]=b[j];
         b[j]=t;
    }
    while(b[j]!=0)
    {
        c=a[j]%b[j];
        a[j]=b[j];
        b[j]=c;
    }
    printf("%d\n",m*n/a[j]);
    }
    return 0;
}

E - 计算器

 The contest starts now! How excited it is to see balloons floating around. You, one of the best programmers in HDU, can get a very beautiful balloon if only you have solved the very very very... easy problem.
Give you an operator (+,-,*, / --denoting addition, subtraction, multiplication, division respectively) and two positive integers, your task is to output the result.
Is it very easy?
Come on, guy! PLMM will send you a beautiful Balloon right now!
Good Luck!

Input
Input contains multiple test cases. The first line of the input is a single integer T (0 < T<1000) which is the number of test cases. T test cases follow. Each test case contains a char C (+,-,*, /) and two integers A and B(0 < A,B<10000).Of course, we all know that A and B are operands and C is an operator.
Output
For each case, print the operation result. The result should be rounded to 2 decimal places If and only if it is not an integer.
Sample Input

4
+ 1 2
- 1 2
* 1 2
/ 1 2

Sample Output

3
-1
2
0.50
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
int main()
{
    int N;
    scanf("%d",&N);
    int i=0;
   long  int a[1001][2];
    double  b[1001];
    char ch[1001];
    int flag=1;
    for(i=0;i<N;i++)
    {
       getchar();
        scanf("%c",&ch[i]);
        scanf("%ld",&a[i][0]);
        scanf("%ld",&a[i][1]);
        if(ch[i]=='+')
        {
            b[i]=a[i][0]+a[i][1];
        }
        if(ch[i]=='-')
        {    b[i]=a[i][0]-a[i][1];
        }
        if(ch[i]=='*')
        {   b[i]=a[i][0]*a[i][1];
        }
        if(ch[i]=='/')
        {
           if(a[i][0]%a[i][1]!=0)
           {
               b[i]=(a[i][0]*1.0)/a[i][1];
               flag=0;
           }
           else
           {   b[i]=a[i][0]/a[i][1];
           }    
        } 
        if(flag==0&ch[i]=='/')
        {
            printf("%.2lf\n",b[i]);
            flag=1;
        }
        else
        printf("%.0lf\n",b[i]);
    }
    return 0;
}
反思,这个题不需要数组,现在养成了一种惯性思维,凡是题目提示多种数据的时候,习惯用数组,有些题目可以通过重新赋值来解决这个问题。另外要读清楚题意,这个题在写的过程中,因为没有读懂题意,一直使用的是double类型的数据,从而导致了错误

F - 数字字符统计
对于给定的一个字符串,统计其中数字字符出现的次数。
Input
输入数据有多行,第一行是一个整数n,表示测试实例的个数,后面跟着n行,每行包括一个由字母和数字组成的字符串。
Output
对于每个测试实例,输出该串中数值的个数,每个输出占一行。
Sample Input

2
asdfasdf123123asdfasdf
asdf111111111asdfasdfasdf

Sample Output

6
9
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
int main()
{
    int N;
    scanf("%d",&N);
    int i=0,j=0,k=0;
    int count[20];
    memset(count,0,sizeof(count));
    char a[20][100];
    for(i=0;i<N;i++)
    {
        scanf("%s",a[i]);
        k=strlen(a[i]);
        for(j=0;j<k;j++)
        {
             if(a[i][j]>=48&&a[i][j]<=57)
             {
                 count[i]++;
             }
        }
        printf("%d\n",count[i]);
    }
    return 0;
}
这个题的思路也很简单,就是通过遍历,将其中的数字统计出来

G - 数字序列插入
有n(n<=100)个整数,已经按照从小到大顺序排列好,现在另外给一个整数x,请将该数插入到序列中,并使新的序列仍然有序。
Input
输入数据包含多个测试实例,每组数据由两行组成,第一行是n和m,第二行是已经有序的n个数的数列。n和m同时为0标示输入数据的结束,本行不做处理。
Output
对于每个测试实例,输出插入新的元素后的数列。
Sample Input

3 3
1 2 4
0 0

Sample Output

1 2 3 4
这个题有两种思路

第一种,进行数据的插入,通过从后往前的遍历,得到数据应该插入的位置,将数组中的元素往后移动一位

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
int main()
{
   int N,M;
   int i=0;
   int j=0,l=0;
   int k=0;
   int m=0;
   int a[40][101];
  while(scanf("%d%d",&N,&M)&&(M!=0||N!=0))
  {  
      l=0;
      k=N;
   for(i=0;i<N;i++)
   {
       scanf("%d",&a[m][i]);
   }
   for(i=0;i<N;i++)
   {
       if(M<a[m][i])
       {
           for(j=N;j>=i;j--)
           {
              a[m][j+1]=a[m][j];
           }
           a[m][i]=M;
           break;
       }
       if(M>a[m][N-1])
       {
           a[m][N]=M;
       }
       if(M==a[m][i])
       {
             for(j=N;j>=i;j--)
             {
                a[m][j+1]=a[m][j];
             }
             a[m][i]=M;
             break;
       }
       if(i==N)
       {
           a[m][i]=M;
       }
  }
      for(i=0;i<=k;i++)
   {   printf("%d",a[m][i]);
       if(i<k)
           printf(" ");
   }
   printf("\n");
   m++;
  }
  return 0;
}

这种办法是不提倡的,是相当麻烦的
另一种思路,找到它相应的位置,然后直接输出,即可

#include <stdio.h>
#define maxn 100
int a[maxn];
int main(void)
{
    int n, m;
    while (scanf("%d %d", &n, &m) == 2 && !(m == 0 && n == 0))
    {
        int flag = 0;
        for (int i = 0; i < n; i++)
            scanf("%d", &a[i]);
        for (int i = 0; i < n; i++)
        {
            if (!flag && m < a[i])
            {
                printf("%d ", m);
                flag = 1;
            }
            printf("%d", a[i]);
            if (flag && i == n - 1)
                printf("\n");
            else
                printf(" ");
        }
        if (!flag)
            printf("%d\n", m);
    }
}

H - 合法标识符
输入一个字符串,判断其是否是C的合法标识符。
Input
输入数据包含多个测试实例,数据的第一行是一个整数n,表示测试实例的个数,然后是n行输入数据,每行是一个长度不超过50的字符串。
Output
对于每组输入数据,输出一行。如果输入数据是C的合法标识符,则输出”yes”,否则,输出“no”。
Sample Input

3
12ajf
fi8x_a
ff  ai_2

Sample Output

no
yes
no
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
int main()
{
    int N;
    scanf("%d",&N);
    char a[1001];
    int i=0,j=0;
    int k=0,l=0;
    int flag=1;
    getchar();
    while(1)
    {
        l++;

       gets(a);
        for(i=0;i<N;i++)
    {
       flag=0;
        if((a[0]>='a'&&a[0]<='z')||(a[0]>='A'&&a[0]<='Z')||(a[0]=='_'))
        {  for(j=1;a[j];j++)
         {
             if((a[j]>=48&&a[j]<=57)||(a[j]>=65&&a[j]<=90)||(a[j]>=97&&a[j]<=122)||(a[j]==95))
             {
             }
             else
             {
                 flag=1;
                 break;
         }
         }   
        }
        else
        {
            flag=1;
        }
    }
        if(flag==0)
            printf("yes\n");
        else
              printf("no\n");
        if(l==N)
            break;
    }
    return 0;
}

这道题思路很简单:就是根据C语言中标识符的定义,来进行判断

I - 三角形
给定三条边,请你判断一下能不能组成一个三角形。
Input
输入数据第一行包含一个数M,接下有M行,每行一个实例,包含三个正数A,B,C。其中A,B,C <1000;
Output
对于每个测试实例,如果三条边长A,B,C能组成三角形的话,输出YES,否则NO。
Sample Input

2
1 2 3
2 2 2

Sample Output

NO
YES
#include<stdio.h>
int main()
{
 int n;
 double a,b,c;
 scanf("%d",&n);
 while(n--)
 {
  scanf("%lf%lf%lf",&a,&b,&c);
  if(a+b>c&&a+c>b&&b+c>a)
      printf("YES\n");
   else printf("NO\n");
 }
 return 0;
}
反思:这道题属于判断三角形能不能成立,属于很简单的题,两边之和大于第三边,但是在我提交了四次才提交成功,在这做个反思,我在之前使用flag来判断能不能成立,然而,我在每次循环开始没有对flag进行清零,从而造成了错误,这个错误很是不应该

J - 亲和数
古希腊数学家毕达哥拉斯在自然数研究中发现,220的所有真约数(即不是自身的约数)之和为:

1+2+4+5+10+11+20+22+44+55+110=284。

而284的所有真约数为1、2、4、71、 142,加起来恰好为220。人们对这样的数感到很惊奇,并称之为亲和数。一般地讲,如果两个数中任何一个数都是另一个数的真约数之和,则这两个数就是亲和数。

你的任务就编写一个程序,判断给定的两个数是否是亲和数 

Input
输入数据第一行包含一个数M,接下有M行,每行一个实例,包含两个整数A,B; 其中 0 <= A,B <= 600000 ;
Output
对于每个测试实例,如果A和B是亲和数的话输出YES,否则输出NO。
Sample Input

2
220 284
100 200

Sample Output

YES
NO
这个题的思路也很直接,通过求一个数的真约数的和来和另外一个数进行对比,如何符合就说明为亲和数,不符合不是亲和数
include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<math.h>
int sum1(int a){//该函数作用是求出数a的所有真因子之和
    int i,sum=0;
    for (i=2;i<sqrt(a);i++)
    {
        if (a%i==0) sum=sum+(a/i)+i;
    }
    return sum+1;
}
int main()
{
    int N;
    scanf("%d",&N);
    int a,b;
    int flag=0;
    while(N--)
    {
        scanf("%d",&a);
        scanf("%d",&b);
       flag=0;
       if((sum1(a)==b)&&(sum1(b)==a))
           flag=1;
       if(flag==1)
           printf("YES\n");
       else
           printf("NO\n");
   }
    return 0;
}

前方高能预警,做题一定要把题看清,这道题我还以为是求得两个数,进行判断

K - 素数

对于表达式n^2+n+41,当n在(x,y)范围内取整数值时(包括x,y)(-39< =x < y < = 50),判定该表达式的值是否都为素数。
Input
输入数据有多组,每组占一行,由两个整数x,y组成,当x=0,y=0时,表示输入结束,该行不做处理。
Output
对于每个给定范围内的取值,如果表达式的值都为素数,则输出”OK”,否则请输出“Sorry”,每组输出占一行。
Sample Input

0 1
0 0

Sample Output

OK

思路:利用素数的判断方法,来进行判断即可

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<math.h>
int sushu(int n)
{
    int flag=1;
    int k=2;
    for(k=2;k<n;k++)
    {
        if(n%k==0)
        {
            flag=0;
            break;
        }
    }
    return flag;
}
int main()
{
    int x,y;
    int a;
    int i=0;
    int flag=0;
    while(1)
  {  
      flag=0;
     scanf("%d %d",&x,&y);
          if((x==y)&&x==0)
              break;
          for(i=x;i<=y;i++)
          { 
              a=pow(i,2)+i+41;
             flag=sushu(a);
             if(flag==0)
                 break;
        }
             if(flag==1)
             {
                 printf("OK\n");
             }
        else
        {
            printf("Sorry\n");

        }
 }
    return 0;
}

L - 首字母大写
输入一个英文句子,将每个单词的第一个字母改成大写字母。
Input
输入数据包含多个测试实例,每个测试实例是一个长度不超过100的英文句子,占一行。
Output
请输出按照要求改写后的英文句子。
Sample Input

i like acm
i want to get an accepted

Sample Output

I Like Acm
I Want To Get An Accepted
#include<stdio.h>
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
int main()
{
    char a[100];
    while(gets(a))//gets函数的返回值问题
    {
      int i=0,j=0;
    j=strlen(a);
    if(a[0]==0)
    {printf("\n");
        continue;} 
    a[0]=a[0]-'a'+'A';
    for(i=0;i<j;i++)
    {
          if(a[i]==' ')
          a[i+1]=a[i+1]-'a'+'A';           
    }
    printf("%s\n",a);
    }
    return 0;
}
这道题我一开始没有理解gets函数的返回值,不知道如何退出,因为我很少使用gets函数,通过与scanf的函数进行联系,利用man手册来查询,得到了gets函数返回的是一个指针,如果返回的指针为空的话就会退出

猜你喜欢

转载自blog.csdn.net/dream0130__/article/details/81194456