南阳OJ语言入门锦集

来源:互联网 发布:手机淘宝如何换货 编辑:程序博客网 时间:2024/05/16 01:04

       这里的题大多是我自己做的。我会用不同的知识点来解决一道题。并且有详细的注释,对新手有帮助。也希望你们在ACM的道路上越走越远。

1:/*ASCII码排序

时间限制:3000 ms  |  内存限制:65535 KB

难度:2

描述 输入三个字符(可以重复)后,按各字符的ASCII码从小到大的顺序输出这三个字符。
输入第一行输入一个数N,表示有N组测试数据。后面的N行输入多组数据,每组输入数据都是占一行,有三个字符组成,之间无空格。输出对于每组输入数据,输出一行,字符中间用一个空格分开。样例输入2
qwe
asd
样例输出e q w
a d s*/

<span style="font-size:18px;"></pre><pre class="cpp" name="code"># include<stdio.h>int main(){ int n; scanf("%d",&n); getchar(); while(n--) {  char a[4];  int j,i,t;  for(j=0;j<3;j++)  scanf("%c",&a[j]);  getchar();  for(i=0;i<2;i++)  for(j=0;j<2-i;j++)//冒泡排序   if(a[j]>a[j+1])  {  t=a[j];  a[j]=a[j+1];  a[j+1]=t;   }  for(j=0;j<3;j++)  printf("%c ",a[j]); }  return 0;}#include <stdio.h> int main() {     char a,b,c,tmp;  int i; scanf("%d",&i); getchar();while(i--){      a=getchar();           b=getchar();       c=getchar();   getchar();         if(a>b)       //这里可以用逗号表达式,更加简洁  {               tmp=a;        a=b;           b=tmp;         }          if(a>c)      {              tmp=a;        a=c;              c=tmp;         }         if(b>c)     {             tmp=b;        b=c;             c=tmp;         }          printf("%c %c %c\n",a,b,c);     }    return 0; }</span>

2:/*奇偶数分离

时间限制:3000 ms  |  内存限制:65535 KB

难度:1

描述 有一个整型偶数n(2<= n <=10000),你要做的是:先把1到n中的所有奇数从小到大输出,再把所有的偶数从小到大输出。
输入第一行有一个整数i(2<=i<30)表示有 i 组测试数据;
 每组有一个整型偶数n。输出第一行输出所有的奇数
 第二行输出所有的偶数
样例输入2
10
14
样例输出1 3 5 7 9
2 4 6 8 10

1 3 5 7 9 11 13
2 4 6 8 10 12 14 */

<span style="font-size:18px;"># include<stdio.h> int main(){ int i,j,k,t; scanf("%d",&i); while(i--) {  scanf("%d",&j);  for(k=1;k<=j;k=k+2)    printf("%d ",k);    printf("\n");//格式也很重要   for(t=2;t<=j;t=t+2)     printf("%d ",t);     }</span>


<span style="font-size:18px;"></span>

3:/*Fibonacci数

时间限制:3000 ms  |  内存限制:65535 KB

难度:1

描述 无穷数列1,1,2,3,5,8,13,21,34,55...称为Fibonacci数列,它可以递归地定义为
F(n)=1 ...........(n=1或n=2)
 F(n)=F(n-1)+F(n-2).....(n>2)
现要你来求第n个斐波纳奇数。(第1个、第二个都为1)
输入第一行是一个整数m(m<5)表示共有m组测试数据
 每次测试数据只有一行,且只有一个整形数n(n<20)
输出对每组输入n,输出第n个Fibonacci数样例输入3
1
3
5
样例输出1
2
5*/

<span style="font-size:18px;"># include<stdio.h>int main(){    int p[22]; int n,m,i; scanf("%d",&n); while(n--) {  p[1]=1;p[2]=1;  scanf("%d",&m);  for(i=3;i<=m;i++)  p[i]=p[i-1]+p[i-2];  printf("%d\n",p[m]); } return 0;}#include <stdio.h>  int F(int n) {      if (n == 1 || n == 2) {          return 1;      } else {          return F(n - 1) + F(n - 2);      }  }    int main() {      int i, n;      scanf("%d", &i);      while (i--) {          scanf("%d", &n);          printf("%d\n", F(n));      }      return 0;  }  </span>
第二种用的是调用函数,来解决这个问题。灵活运用函数,可以解决很多问题。

4:/*素数求和问题

限制:3000 ms  |  内存限制:65535 KB
难度:2
描述 现在给你N个数(0<N<1000),现在要求你写出一个程序,找出这N个数中的所有素数,并求和。
输入第一行给出整数M(0<M<10)代表多少组测试数据
每组测试数据第一行给你N,代表该组测试数据的数量。
接下来的N个数为要测试的数据,每个数小于1000输出每组测试数据结果占一行,
输出给出的测试数据的所有素数和样例输入3
5
1 2 3 4 5
8
11 12 13 14 15 16 17 18
10
21 22 23 24 25 26 27 28 29 30

样例输出10
41
52*/

<span style="font-size:18px;">#include"stdio.h"  int fun(int n)  {int i;   if(n==1)return 0;   else {for(i=2;i<n;i++)         if(n%i==0)break;         if(i>=n)return n;         else return 0;   }   }  int main()   {      int m,n,k,i,s;     int a[1000];     scanf("%d",&m);     while(m--)      {         s=0;         scanf("%d",&n);         for(i=0;i<n;i++)         scanf("%d",&a[i]);         for(k=0;k<n;k++)         s+=fun(a[k]);         printf("%d\n",s);     }  return 0;}  #include<stdio.h>  #include<math.h>  int main() {      int i, j, k, a, n, m, sum = 0;      scanf("%d", &a);      for (i = 0; i < a; i++) {          scanf("%d", &n);          for (j = 0; j < n; j++) {              scanf("%d", &m);              for (k = 2; k <= sqrt(m); k++) {                 if (m % k == 0)                      break;             }             if (k > sqrt(m) && m != 1)                  sum = sum + m;         }          printf("%d\n", sum);  sum=0;//输入一组数据后 ,sum清零一下。      }      return 0;  }  

5:/*素数距离问题

时间限制:3000 ms  |  内存限制:65535 KB 

难度:2

描述 现在给出你一些数,要求你写出一个程序,输出这些整数相邻最近的素数,

并输出其相距长度。如果左右有等距离长度素数,则输出左侧的值及相应距离。

 如果输入的整数本身就是素数,则输出该素数本身,距离输出0

输入第一行给出测试数据组数N(0<N<=10000)

接下来的N行每行有一个整数M(0<M<1000000),输出每行输出两个整数 A B.

其中A表示离相应测试数据最近的素数,B表示其间的距离。样例输入3

6

8

10

样例输出5 1

7 1

11 1*/

# include<stdio.h># include<math.h>int p(int m);int main(){int i,n,N;scanf("%d",&N);while(N--){scanf("%d",&n);if(p(n)){printf("%d 0\n",n);continue;    }for(i=0;n-i!=-1;i++){if(p(n-i)){printf("%d %d\n",n-i,i);break;    }if(p(n+i)){printf("%d %d\n",n+i,i);break;}}}return 0;}int p(int m){if(m==0||m==1)return 0;int i;for(i=2;i<=sqrt(m);i++)if(m%i==0)break;if(i>sqrt(m))return 1;return 0;}

6:/*A Famous Music Composer

时间限制:1000 ms  |  内存限制:65535 KB 

难度:1

描述 

Mr. B is a famous music composer. One of his most famous work was his set of preludes.

These 24 pieces span the 24 musical keys (there are musically distinct 12 scale notes, 

and each may use major or minor tonality). The 12 distinct scale notes are: 

 A     A#=Bb  B        C       C#=Db D       D#=Eb  E       F        F#=Gb  G      

 G#=Ab 

Five of the notes have two alternate names, as is indicated above with equals sign. 

Thus, there are 17 possible names of scale notes, but only 12 musically distinct notes.

When using one of these as the keynote for a musical key, 

we can further distinguish between major and minor tonalities. 

This gives 34 possible keys, of which 24 are musically distinct. 

 

In naming his preludes, Mr. B used all the keys except the following 10, 

which were named instead by their alternate names: 

Ab minor  A# major A# minor  C# major  Db minor 

D# major  D# minor Gb major  Gb minor  G# major  

Write a program that, given the name of a key, give an alternate name if it has one, 

or report the key name is unique. 

输入Each test case is described by one line having the format "note tonality", where "note" is one of the 17 names for the scale notes given above, and "tonality" is either "major" or "minor" (quotes for clarify). 输出For each case output the required answer, following the format of the sample.样例输入Ab minor

D# major

G minor

样例输出Case 1: G# minor

Case 2: Eb major

Case 3: UNIQUE*/

#include <stdio.h>  #include <string.h>  #include <algorithm>  using namespace std;  char str[100];  int cas = 1;    int main()  {      while(gets(str))//输入字符串一直为真      {          printf("Case %d: ",cas++);          int i,j,len;          len = strlen(str);//字符串的长度          if(str[1] == ' ')              printf("UNIQUE\n");          else          {              if(str[1] == '#')              {                  if(str[0] == 'G')                      printf("Ab");                  else                      printf("%cb",str[0]+1);              }              else if(str[1] == 'b')              {                  if(str[0] == 'A')                  printf("G#");                  else                  printf("%c#",str[0]-1);              }              for(i = 2;i<len;i++)              printf("%c",str[i]);              printf("\n");          }      }        return 0;  }  

7:/*5个数求最值

时间限制:1000 ms  |  内存限制:65535 KB 

难度:1

描述 设计一个从5个整数中取最小数和最大数的程序

输入输入只有一组测试数据,为五个不大于1万的正整数输出输出两个数,

第一个为这五个数中的最小值,第二个为这五个数中的最大值,两个数字以空格格开。

样例输入1 2 3 4 5

样例输出1 5*/

# include<stdio.h>//打擂台方法 int main(){ int i,j,min,max,a[5];  for(i=0;i<5;i++)  scanf("%d",&a[i]);  max=a[0];min=a[0];  for(j=1;j<5;j++)  {   if(a[j]>max)  {max=a[j];  }  if(a[j]<min)  {min=a[j];  }  }   printf("%d %d",min,max);  return 0;}

8:/*韩信点兵

时间限制:3000 ms  |  内存限制:65535 KB 

难度:1

描述 相传韩信才智过人,从不直接清点自己军队的人数,只要让士兵先后以三人一排、五人一排、

七人一排地变换队形,而他每次只掠一眼队伍的排尾就知道总人数了。

输入3个非负整数a,b,c ,表示每种队形排尾的人数(a<3,b<5,c<7),输出总人数的最小值(

或报告无解)。已知总人数不小于10,不超过100 

输入输入3个非负整数a,b,c ,表示每种队形排尾的人数(a<3,b<5,c<7)。

例如,输入:2 4 5输出输出总人数的最小值(或报告无解,即输出No answer)。

实例,输出:89样例输入2 1 6

样例输出41*/

#include<stdio.h>int main(){int a,b,c,n,m;scanf("%d %d %d",&a,&b,&c);for(n=10;n<=100;n++){if(a==n%3&&b==n%5&&c==n%7){printf("%d\n",n);break;//分号就是结束。     }    }if(a!=(n%3)&&b!=(n%5)&&c!=(n%7))//要知道优先顺序。 printf("No answer\n");return 0;}//这里也可以用标识符来做这道题。/*#include"stdio.h"  int main()  {      int a,b,c,i,n,flag;      scanf("%d%d%d",&a,&b,&c);     for(n=10;n<=100;n++)      {          if(n%3==a && n%5==b && n%7==c)          {             flag=0;             printf("%d\n",n);break;         }       else                flag=1;     }     if(flag) //意思是如果flag不等于0.侧输出。printf("No answer\n");  return 0;}  */

9:/*水仙花数

时间限制:1000 ms  |  内存限制:65535 KB 

难度:0

描述 请判断一个数是不是水仙花数。

 其中水仙花数定义各个位数立方和等于它本身的三位数。

输入有多组测试数据,每组测试数据以包含一个整数n(100<=n<1000)

输入0表示程序输入结束。输出如果n是水仙花数就输出Yes

否则输出No样例输入153

154

0

样例输出Yes

No*/

#include<stdio.h>int main(){int n,i,j,k;scanf("%d",&n);i=n/100%10;j=n/10%10;k=n%10;if(n==i*i*i+j*j*j+k*k*k)printf("yes\n");elseprintf("no\n");return 0;}这个方法很奇特。#include "stdio.h"  int main()  {      int n=0;        while(scanf("%d",&n)==1)      {          if(n!=0)         {             if (n==153||n==370||n==371||n==407) printf("Yes\n");              else printf("No\n");         } }      return 0;  }   

10:/*公约数和公倍数

时间限制:1000 ms  |  内存限制:65535 KB 

难度:1

描述 小明被一个问题给难住了,现在需要你帮帮忙。问题是:给出两个正整数,

求出它们的最大公约数和最小公倍数。

输入第一行输入一个整数n0<n<=10000),表示有n组测试数据;

随后的n行输入两个整数i,j0<i,j<=32767)

输出输出每组测试数据的最大公约数和最小公倍数样例输入3

6 6

12 11

33 22

样例输出6 6

1 132

11 66*/

# include<stdio.h>int main(){int n;scanf("%d",&n);while(n--){int x,y,a,b,t;scanf("%d %d",&a,&b);x=a;y=b;while(y!=0){t=x%y;//这个可以判断,a,b的大小x=y;y=t;  }  printf("%d %d\n",x,a*b/x);}return 0;}2:#include <stdio.h>int gcd(int a, int b){if (a % b == 0)return b;return gcd(b, a % b);    碾转相除法}int main(){int t;scanf("%d", &t);while (t--){int a, b;scanf("%d%d", &a, &b);printf("%d %d\n", gcd(a, b), a * b / gcd(a, b));}return 0;} 

11:/*三个数从小到大排序

时间限制:3000 ms  |  内存限制:65535 KB 

难度:0

描述 

现在要写一个程序,实现给三个数排序的功能

输入输入三个正整数输出给输入的三个正整数排序样例输入20 7 33

样例输出7 20 33*/

# include<stdio.h>int main(){void sort(int a[],int n);int i,a[3];for(i=0;i<3;i++)scanf("%d",&a[i]);sort(a,3);for(i=0;i<3;i++)printf("%d ",a[i]);return 0;}void sort(int a[],int n){int m,j,k,t;for(m=0;m<n-1;m++){k=m;for(j=m+1;j<n;j++)if(a[j]<a[k])k=j;t=a[k];a[k]=a[m];a[m]=t;}}(2)# include<stdio.h># include<algorithm>using namespace std;int main(){int a[3];scanf("%d %d %d",&a[0],&a[1],&a[2]);sort(a,a+3);printf("%d %d %d\n",a[0],a[1],a[2]);return 0;}(3)# include<stdio.h>int main(){int i,a[3],m,j,k,t;for(i=0;i<3;i++)scanf("%d",&a[i]);for(m=0;m<2;m++){k=m;for(j=m+1;j<3;j++)if(a[j]<a[k])k=j;t=a[k];a[k]=a[m];a[m]=t;}for(i=0;i<3;i++)printf("%d ",a[i]);return 0;}

12

阶乘因式分解(一)

时间限制:3000 ms  |  内存限制:65535 KB 

难度:2

描述 

给定两个数m,n,其中m是一个素数。

n0<=n<=10000)的阶乘分解质因数,求其中有多少个m

输入第一行是一个整数s0<s<=100),表示测试数据的组数

随后的s每行有两个整数nm。输出输出m的个数。样例输入2

100 5

16 2

样例输出24

15

#include<stdio.h>int main(){int i,j,k,m;scanf("%d",&m);while(m--){i=0;scanf("%d%d",&j,&k);while(j){j=j/k;i=i+j;//把输出语句,放在这里答案就不对了。}printf("%d\n",i); //位置很关键。}return 0;}

13:6174问题

时间限制:1000 ms  |  内存限制:65535 KB 

难度:2

描述 

假设你有一个各位数字互不相同的四位数,把所有的数字从大到小排序后得到a,从小到大后得到b,

然后用a-b替换原来这个数,并且继续操作。例如,从1234出发,

依次可以得到4321-1234=30878730-378=83528532-2358=6174,又回到了它自己!

现在要你写一个程序来判断一个四位数经过多少次这样的操作能出现循环,并且求出操作的次数

比如输入1234执行顺序是1234->3087->8352->6174->6174,输出是4

输入第一行输入n,代表有n组测试数据。

接下来n行每行都写一个各位数字互不相同的四位数输出经过多少次上面描述的操作才能出现循环

样例输入1

1234

样例输出4

#include<stdio.h>  int main()  {      int n,m,a[4],i,j,count,max,min,t;     scanf("%d",&n);     while(n--)     {          count=1;         scanf("%d",&m);         while(m!=6174)          {              for(i=0; i<4; i++)              {                  a[i]=m%10;                  m=m/10;  //求得这个四位数的个位,十位,百位,千位。            }              for(i=0; i<4; i++)                for(j=i+1; j<4; j++)  //让这四个数比较大小。                   if(a[i]<a[j])                      {                          t=a[i];                          a[i]=a[j];                          a[j]=t;                      }             max=a[0]*1000+a[1]*100+a[2]*10+a[3];             min=a[3]*1000+a[2]*100+a[1]*10+a[0];             m=max-min;             count++;          }          printf("%d\n",count);      }      return 0;  }  

14:

/*谁获得了最高奖学金

时间限制:1000 ms  |  内存限制:65535 KB 

难度:2

描述     某校的惯例是在每学期的期末考试之后发放奖学金。

发放的奖学金共有五种,获取的条件各自不同:

1) 院士奖学金,每人8000元,期末平均成绩高于80分(>80),

 并且在本学期内发表1篇或1篇以上论文的学生均可获得;

2) 五四奖学金,每人4000元,期末平均成绩高于85分(>85),

并且班级评议成绩高于80分(>80)的学生均可获得;

3) 成绩优秀奖,每人2000元,期末平均成绩高于90分(>90)的学生均可获得;

4) 西部奖学金,每人1000元,期末平均成绩高于85分(>85)的西部省份学生均可获得;

5) 班级贡献奖,每人850元,班级评议成绩高于80分(>80)的学生干部均可获得;

只要符合条件就可以得奖,每项奖学金的获奖人数没有限制,

每名学生也可以同时获得多项奖学金。例如姚林的期末平均成绩是87分,班级评议成绩82分,

同时他还是一位学生干部,那么他可以同时获得五四奖学金和班级贡献奖,奖金总数是4850元。

现在给出若干学生的相关数据,请计算哪些同学获得的奖金总数最高

(假设总有同学能满足获得奖学金的条件)。

输入第一行输入数据N,表示测试数据组数(0<N<100),每组测试数据输入的第一行是

一个整数X1 <= X <= 100),表示学生的总数。接下来的X行每行是一位学生的数据,

从左向右依次是姓名,期末平均成绩,班级评议成绩,是否是学生干部,是否是西部省份学生,

以及发表的论文数。姓名是由大小写英文字母组成的长度不超过20的字符串(不含空格);

期末平均成绩和班级评议成绩都是0100之间的整数(包括0100);

是否是学生干部和是否是西部省份学生分别用一个字符表示,Y表示是,N表示不是;

发表的论文数是010的整数(包括010)。每两个相邻数据项之间用一个空格分隔。

输出  每组测试数据输出包括三行,第一行是获得最多奖金的学生的姓名,

第二行是这名学生获得的奖金总数。如果有两位或两位以上的学生获得的奖金最多,

输出他们之中在输入文件中出现最早的学生的姓名。第三行是这X个学生获得的奖学金的总数。

样例输入1

4

YaoLin 87 82 Y N 0

ChenRuiyi 88 78 N Y 1

LiXin 92 88 N N 0

ZhangQin 83 87 Y N 1

样例输出ChenRuiyi

9000

28700*/

#include<stdio.h>#include<string.h>struct {    char name[22];    int qimo;    int banyi;    char bangan;    char xibu;    int lunwen;    int jiangjin;}data[110];int main(){    int n,x;    int i,tsum,t,k;    scanf("%d",&n);    while(n--)    {         scanf("%d",&x);        for(i=0;i<x;i++)            data[i].jiangjin=0;        for(i=0;i<x;i++)        scanf("%s %d%d %c %c %d",data[i].name,&data[i].qimo,&data[i].banyi,&data[i].bangan,&data[i].xibu,&data[i].lunwen);        for(i=0;i<x;i++)        {            if(data[i].qimo>80&&data[i].lunwen>=1)                data[i].jiangjin+=8000;            if(data[i].qimo>85&&data[i].banyi>80)                data[i].jiangjin+=4000;            if(data[i].qimo>90)                data[i].jiangjin+=2000;            if(data[i].qimo>85&&data[i].xibu=='Y')                data[i].jiangjin+=1000;            if(data[i].banyi>80&&data[i].bangan=='Y')                data[i].jiangjin+=850;        }        t=-1;        tsum=0;        for(i=0;i<x;i++)        {            tsum+=data[i].jiangjin;            if(t<data[i].jiangjin)            {                t=data[i].jiangjin;                k=i;            }        }        printf("%s\n%d\n%d\n",data[k].name,data[k].jiangjin,tsum);    }    return 0;}

15:

/*笨小熊

时间限制:2000 ms  |  内存限制:65535 KB 

难度:2

描述 

笨小熊的词汇量很小,所以每次做英语选择题的时候都很头疼。但是他找到了一种方法,经试验证明,

用这种方法去选择选项的时候选对的几率非常大! 

这种方法的具体描述如下:假设maxn是单词中出现次数最多的字母的出现次数,

minn是单词中出现次数最少的字母的出现次数,如果maxn-minn是一个质数,

那么笨小熊就认为这是个Lucky Word,这样的单词很可能就是正确的答案。 

输入第一行数据N(0<N<100)表示测试数据组数。

每组测试数据输入只有一行,是一个单词,其中只可能出现小写字母,并且长度小于100。 输出每组测试数据输出共两行,第一行是一个字符串,假设输入的的单词是Lucky Word,那么输出“Lucky Word”,否则输出“No Answer”; 

第二行是一个整数,如果输入单词是Lucky Word,输出maxn-minn的值,否则输出0样例输入2

error

olympic

样例输出Lucky Word

2

No Answer

0*/

#include <stdio.h>  #include <string.h>  #include <math.h>  #include <stdlib.h>    int cmp(const void *a, const void *b){      return *(int *)b - *(int *)a;  }    int f(int n){ //判断素数      if(n < 2) return 0;      for(int i = 2; i <= sqrt(n); ++i)          if(n % i == 0) return 0;      return 1;  }    int main(){      int t, ok, a[101];      char s[101];      scanf("%d", &t);      while(t--){      scanf("%s",s);        memset(a, 0, sizeof(a));          for(int i = 0; i != strlen(s); ++i){              if(s[i] == '.') continue;              ++a[i];              for(int j = i + 1; j != strlen(s); ++j)                 if(s[j] == s[i]){                      ++a[i];                      s[j] = '.';                 }          }          //排序,降序          qsort(a, 101, sizeof(int), cmp);          int i = 0;          while(i++ != 101)              if(!a[i]) break;          --i;          int x = a[0] - a[i];          if(f(x)) printf("Lucky Word\n%d\n", x);          else printf("No Answer\n0\n");      }      return 0;  }  

 

16:

/*鸡兔同笼

时间限制:3000 ms  |  内存限制:65535 KB 

难度:1

描述 已知鸡和兔的总数量为n,总腿数为m。输入nm,依次输出鸡和兔的数目,如果无解,则输出“No answer(不要引号)

输入第一行输入一个数据a,代表接下来共有几组数据,在接下来的(a<10)

 a行里,每行都有一个nm.(0<m,n<100)输出输出鸡兔的个数,或者No answer样例输入2

14 32

10 16

样例输出12 2

No answer*/

#include <stdlib.h>  #include<stdio.h> int main()  {      int a,c,n,b,i,m;      scanf("%d",&a);     if(a>0 && a<10)     while(a--)      {          scanf("%d %d",&n,&m);   //n:chicken  m:rubbit          if( n>0 && n<100 && m>0 && m<100)          {              b=m/2-n;             a=2*n-m/2;         }         if(m%2==1 || a<0 || b<0)             printf("No answer\n");         else             printf("%d %d\n",a,b);     }  return 0;} 

17:/*另一种阶乘问题

时间限制:3000 ms  |  内存限制:65535 KB 

难度:1

描述 

大家都知道阶乘这个概念,举个简单的例子:5=1*2*3*4*5.现在我们引入一种新的阶乘概念,

将原来的每个数相乘变为i不大于n的所有奇数相乘例如:5!!=1*3*5.现在明白现在这种阶乘的意思了吧!

现在你的任务是求出1!!+2!!......+n!!的正确值(n<=20)

输入第一行输入一个a(a<=20),代表共有a组测试数据

 接下来a行各行输入一个n.

输出各行输出结果一个整数R表示1!!+2!!......+n!!的正确值样例输入2

3

5

样例输出5

23*/

#include"stdio.h"  int main()  {      int a,n,i,j,t;      long s;     scanf("%d",&a);      if(a<=20)       while(a--)      {          s=0;t=1;          scanf("%d",&n);          if(n>20) break;          for(i=1;i<=n;i++)            {              t=1;               //求出每个i的阶乘              for(j=1;j<=i;j+=2) {             t*=j;   }                         s+=t;         }             printf("%ld\n",s);      }  return 0;}  

18:/*Financial Management

时间限制:3000 ms  |  内存限制:65535 KB 

难度:1

描述 Larry graduated this year and finally has a job. He's making a lot of money,

but somehow never seems to have enough. 

Larry has decided that he needs to grab hold of his financial portfolio 

and solve his financing problems. The first step is to figure out 

what's been going on with his money. Larry has his bank account statements 

and wants to see how much money he has. Help Larry by writing a program to take his 

closing balance from each of the past twelve months 

and calculate his average account balance.

输入The input will be twelve lines. Each line will contain the closing balance of his 

bank account for a particular month.

Each number will be positive and displayed to the penny. No dollar sign will be included. 

输出The output will be a single number, the average (mean) of the closing balances for

the twelve months. It will be rounded to the nearest penny, 

preceded immediately by a dollar sign, and followed by the end-of-line. 

There will be no other spaces or characters in the output. 

样例输入100.00

489.12

12454.12

1234.10

823.05

109.20

5.27

1542.25

839.18

83.99

1295.01

1.75

样例输出1581.42*/

#include "stdio.h"  int main()  {      double sum=0,a;      int n=12;       while(n--)      {          scanf("%lf",&a);          sum+=a;      }      printf("%.2lf\n",sum/12);      return 0;  }  

19:/*小学生算术

时间限制:3000 ms  |  内存限制:65535 KB 

难度:1

描述 很多小学生在学习加法时,发现“进位”特别容易出错。

你的任务是计算两个三位数在相加时需要多少次进位。

你编制的程序应当可以连续处理多组数据,直到读到两个0(这是输入结束标记)。

输入输入两个正整数m,n.(m,n,都是三位数)输出输出m,n,相加时需要进位多少次。

样例输入123 456

555 555

123 594

0 0

样例输出0

1*/

#include <stdio.h>  int main()  {      int m,n,i,j;      int a[3],b[3];     while(scanf("%d%d",&m,&n),m!=0||n!=0)      {          a[0]=m/100;          a[1]=(m-100*a[0])/10;          a[2]=m%10;          b[0]=n/100;          b[1]=(n-100*b[0])/10;          b[2]=n%10;          j=0;          for(i=2; i>=0; i--)          {             if(a[i]+b[i]>=10)              {                  j++;                  a[i-1]+=1;//要考虑进位问题             }         }          printf("%d\n",j);      }   return 0;  } 

20:/*日期计算

时间限制:3000 ms  |  内存限制:65535 KB 

难度:1

描述 如题,输入一个日期,格式如:2010 10 24 ,判断这一天是这一年中的第几天。

输入第一行输入一个数N0<N<=100,表示有N组测试数据。

后面的N行输入多组输入数据,每行的输入数据都是一个按题目要求格式输入的日期。

输出每组输入数据的输出占一行,输出判断出的天数n样例输入3

2000 4 5

2001 5 4

2010 10 24

样例输出96

124

297*/

#include<stdio.h>//最优程序  int main()  {      int a,b=0,c,y,m,d,fib;      scanf("%d",&a);      while(a--)      {          scanf("%d %d %d",&y,&m,&d);          if(y%400==0||y%100!=0&&y%4==0)              fib=29;          else fib=28;          for(c=1;c<=m;c++)          switch(c-1) //很重要 4月份有三个月过完。         {             case 1:             case 3:             case 5:             case 7:             case 8:             case 10:b+=31;break;             case 2:b+=fib;break;             case 4:             case 6:             case 9:             case 11:b+=30;break;          }          b+=d;          printf("%d\n",b);          b=0;      }      return 0;  }          

21:/*开灯问题

时间限制:3000 ms  |  内存限制:65535 KB 

难度:1

描述 

n盏灯,编号为1~n,第1个人把所有灯打开,第2个人按下所有编号为的倍数的开关

(这些灯将被关掉),第个人按下所有编号为3的倍数的开关

(其中关掉的灯将被打开,开着的灯将被关闭),依此类推。一共有k个人,问最后有哪些灯开着?

输入:nk,输出开着的灯编号。kn1000

 

输入输入一组数据:nk输出输出开着的灯编号样例输入7 3

样例输出1 5 6 7*/

# include<stdio.h>int main(){int i,j,k,open,n;scanf("%d%d",&n,&k);for(i=1;i<=n;i++){open=1;for(j=2;j<=k;j++){if(i%j==0)open=(open+1)%2;}if(open)printf("%d ",i);}return 0;}

22:/*cigarettes

时间限制:3000 ms  |  内存限制:65535 KB 

难度:2

描述 

Tom has many cigarettes. We hypothesized that he has n cigarettes and smokes them 

one by one keeping all the butts. Out of k > 1 butts he can roll a new cigarette. 

 Nowdo you know how many cigarettes can Tom has? 

输入First input is a single line,it's n and stands for there are n testdata.

then there are n lines ,each line contains two integer numbers giving 

the values of n and k.输出For each line of input, 

output one integer number on a separate line giving the maximum number of cigarettes 

that Peter can have.样例输入3

4 3

10 3

100 5

样例输出5

14

124*/

#include "stdio.h"    int main()  {        int m;     scanf("%d",&m);      while(m--)      {          int n,k,sum;          scanf("%d%d",&n,&k);          sum=n;          while(n/k)        //当n/k=0时,停止循环。                    { sum+=n/k;  n=n/k+n%k; }          printf("%d\n",sum);      }      return 0;  }                






0 0
原创粉丝点击