小技巧:本文之前由csdn自动生成了一个目录,不必下拉一个一个去找,可通过目录标题直接定位。
声明:
题目部分皆为南阳OJ题目。
代码部分包含AC代码(可能不止一个)和最优代码,大部分都是本人写的,并且大部分为c代码和少部分c++代码and极少java代码,但基本都是c语言知识点,没有太多差别,可能代码有的写的比较丑,毕竟知识有限。
语言入门部分题基本都较为简单,是学习编程入门的很好练习,也是ACM的第一步,入门的最佳方法,望认真对待。
本文由csdn-jtahstu原创,转载请注明出处,欢迎志同道合的朋友一起交流学习。本人QQ:1373758426和csdn博客地址,链接:http://blog.youkuaiyun.com/jtahstu
该分类南阳OJ地址:我是地址,链接:http://acm.nyist.edu.cn/JudgeOnline/problemset.php?typeid=1
Now begin
1、
A+B Problem
-
描述
-
此题为练手用题,请大家计算一下a+b的值
-
输入
- 输入两个数,a,b 输出
- 输出a+b的值 样例输入
-
2 3
样例输出
-
5
提示
-
例如:
C语言版:
#include<stdio.h> int main() { int a,b; scanf("%d%d",&a,&b); printf("%d\n",a+b); }
C++版:
#include<iostream> using namespace std; int main() { int a,b; cin>>a>>b; cout<<a+b<<endl; }
Java版:
import java.io.*; import java.util.*; public class Main { public static void main(String args[]) throws Exception { Scanner cin=new Scanner(System.in); int a=cin.nextInt(),b=cin.nextInt(); System.out.println(a+b); } }
Java jdk 1.4 版
import java.io.*; import java.util.*; public class Main { public static void main (String args[]) throws Exception { BufferedReader stdin = new BufferedReader( new InputStreamReader(System.in)); String line = stdin.readLine(); StringTokenizer st = new StringTokenizer(line); int a = Integer.parseInt(st.nextToken()); int b = Integer.parseInt(st.nextToken()); System.out.println(a+b); } }
请注意不要输出过多提示性语句(如:“please input two numbers”),不然会WrongAnswer的
#include<stdio.h>
int main()
{
int a,b;
scanf("%d%d",&a,&b);
printf("%d\n",a+b);
}
#include<iostream>
main(){std::cout<<(1<<31)-1;}
4、
ASCII码排序
-
描述
- 输入三个字符(可以重复)后,按各字符的ASCII码从小到大的顺序输出这三个字符。
#include<stdio.h>
#define MAX 3
char a[MAX];
int main() {
int n;
scanf("%d\n", &n);
while (n--) {
char x, y, z;
scanf("%s", a);
x = a[0];
if (a[1] < x)
x = a[1];
if (a[2] < x)
x = a[2];
z = a[2];
if (a[1] > z)
z = a[1];
if (a[0] > z)
z = a[0];
y = a[0] + a[1] + a[2] - x - z;
printf("%c %c %c\n", x, y, z);
}
return 0;
}
#include "stdio.h"//最优程序
main()
{
char a,b,c,d;
int i;
scanf("%d",&i);
getchar();
while(i--)
{
scanf("%c%c%c",&a,&b,&c);
getchar();
if (a>b) {d=a;a=b;b=d;}
if (a>c) {d=a;a=c;c=d;}
if (b>c) {d=b;b=c;c=d;}
printf("%c %c %c\n",a,b,c);
}
}
11、
奇偶数分离
-
描述
- 有一个整型偶数n(2<= n <=10000),你要做的是:先把1到n中的所有奇数从小到大输出,再把所有的偶数从小到大输出。
# include"stdio.h"
int main() {
int i, b, c, d, j, k;
scanf("%d", &i);
for (d = 0; d < i; d++) {
scanf("%d", &b);
for (k = 1; k <= b; k++) {
if ((k % 2) == 1)
printf("%d ", k);
}
printf("\n");
for (j = 1; j <= b; j++) {
if ((j % 2) == 0)
printf("%d ", j);
}
printf("\n");
}
return 0;
}
#include<stdio.h>//最优程序
int main() {
int n;
scanf("%d", &n);
int a;
while (n--) {
scanf("%d", &a);
for (int i = 1; i <= a; i += 2)
printf("%d ", i);
puts("");
for (int i = 2; i <= a; i += 2)
printf("%d ", i);
puts("");
}
}
13、
Fibonacci数
-
描述
-
无穷数列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)
#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;
}
#include<stdio.h>//最优程序
main()
{
int m,n,i,s1,s2;
scanf("%d",&m);
while(m--)
{ scanf("%d",&n);
for(i=3,s1=s2=1;i<=n;i++)
{
s1=s1+s2;s2=s1-s2;
}
printf("%d\n",s1);
}
}
22、
素数求和问题
-
描述
-
现在给你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
来源
- [hzyqazasdf]原创 上传者
- hzyqazasdf
-
第一行给出整数M(0<M<10)代表多少组测试数据
#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;
}
return 0;
}
#include<stdio.h>//最优程序
#include <math.h>
int main()
{
int m,n,i,j,a[1000],flag=0;
long s;
scanf("%d",&m);
while(m--)
{
s=0;
scanf("%d",&n);
for(i=0;i<n;i++)
scanf("%d",&a[i]);
for(i=0;i<n;i++)
{
if(a[i]==1) continue;
flag=0;
for(j=2;j<=sqrt(a[i]);j++)
{
if(a[i]%j==0)
{flag=1;break;}
}
if(flag==0) s+=a[i];
}
printf("%d\n",s);
}
return 0;
}
24、
素数距离问题
-
描述
-
现在给出你一些数,要求你写出一个程序,输出这些整数相邻最近的素数,并输出其相距长度。如果左右有等距离长度素数,则输出左侧的值及相应距离。
如果输入的整数本身就是素数,则输出该素数本身,距离输出0-
输入
-
第一行给出测试数据组数N(0<N<=10000)
接下来的N行每行有一个整数M(0<M<1000000), 输出
-
每行输出两个整数 A B.
其中A表示离相应测试数据最近的素数,B表示其间的距离。 样例输入
-
3 6 8 10
样例输出
-
5 1 7 1 11 1
来源
- 经典题目 上传者
- hzyqazasdf
-
第一行给出测试数据组数N(0<N<=10000)
#include<stdio.h>
#include<math.h>
bool judge(int m);
int main()
{
int N,n,i;
scanf("%d",&N);
while(N--)
{
scanf("%d",&n);
if(judge(n))
{
printf("%d 0\n",n);
continue;
}
for(i=1; n-i!=-1; i++)
{
if(judge(n-i))
{
printf("%d %d\n",n-i,i);
break;
}
if(judge(n+i))
{
printf("%d %d\n",n+i,i);
break;
}
}
}
return 0;
}
bool judge(int m)
{
if(m==0||m==1)
return false;
int i;
for(i=2; i<=sqrt(m); i++)
{
if(m%i==0)break;
}
if(i>sqrt(m))
return true;
return false;
}
#include<iostream>//最优程序
#include<cmath>
using namespace std;
bool isprime(int n)
{
for(int k=2;k<=sqrt((double)n);k++)
if((n%k)==0)
return false;
return true;
}
int main()
{
int n;
cin>>n;
while(n--)
{
int num,i,j;
cin>>num;
if(num==1)
{
cout<<"2 1"<<endl;
continue;
}
for(i=num;!isprime(i);i--);
for(j=num;!isprime(j);j++);
if((num-i)<(j-num))
cout<<i<<' '<<(num-i)<<endl;
else if((num-i)>(j-num))
cout<<j<<' '<<(j-num)<<endl;
else if((num-i)==(j-num))
cout<<i<<' '<<(num-i)<<endl;
}
}
25、
A Famous Music Composer
-
描述
-
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
来源
- hdu 上传者
- 李如兵
#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;
}
#include<iostream>//最优程序
#include<string>
using namespace std;
string trans(string a){
string b="";
if(a[1]=='#'){
b+=char((a[0]-'A'+1)%7+'A');
b+='b';
}else{
b+=char((a[0]-'A'+6)%7+'A');
b+='#';
}
return b;
}
int main(){
string a,b;
for(int t=1; cin>>a>>b; t++){
cout<<"Case "<<t<<": ";
if(a.length()==1)
cout<<"UNIQUE"<<endl;
else
cout<<trans(a)<<" "<<b<<endl;
}
return 0;
}
31、
5个数求最值
-
描述
-
设计一个从5个整数中取最小数和最大数的程序
-
输入
- 输入只有一组测试数据,为五个不大于1万的正整数 输出
- 输出两个数,第一个为这五个数中的最小值,第二个为这五个数中的最大值,两个数字以空格格开。 样例输入
-
1 2 3 4 5
样例输出
-
1 5
来源
- C语言课本第四章第一题 上传者
- 张云聪
#include<stdio.h>
int main() {
int a[5], temp, i, j;
for (i = 0; i < 5; i++)
scanf("%d", &a[i]);
for (j = 0; j < 5; j++)
for (i = 0; i < 4; i++)
if (a[i] > a[i + 1]) {
temp = a[i];
a[i] = a[i + 1];
a[i + 1] = temp;
}
printf("%d %d", a[0], a[4]);
return 0;
}
#include<iostream>//最优程序
#include<iterator>
#include<algorithm>
using namespace std;
int main()
{
int a[5];
copy(istream_iterator<int>(cin),istream_iterator<int>(),a);
cout<<*min_element(a,a+5)<<" "<<*max_element(a,a+5)<<endl;
}
33、
蛇形填数
-
描述
-
在n*n方陈里填入1,2,...,n*n,要求填成蛇形。例如n=4时方陈为:
10 11 12 1
9 16 13 2
8 15 14 3
7 6 5 4
#include"stdio.h"
#include<stdlib.h>
int main()
{
int a[109][109]= {0},i,j,k,n,m,top,x,y;
top=1;
scanf("%d",&n);
a[x=0][y=n-1]=1;
while(top<n*n)
{
while(x+1<n&&!a[x+1][y]) a[++x][y]=++top;
while(y-1>=0&&!a[x][y-1]) a[x][--y]=++top;
while(x-1>=0&&!a[x-1][y]) a[--x][y]=++top;
while(y+1<n&&!a[x][y+1]) a[x][++y]=++top;
}
for(i=0; i<n; i++)
{
for(j=0; j<n; j++)
printf("%d ",a[i][j]);
printf("\n");
}
//system("33.exe\n");
}
#include<stdio.h>//最优程序
int main()
{
int a,b,c,d,n,sum=1;
int yi[101][101];
scanf("%d",&n);
for(a=0;a<=(n-1)/2;a++)
{
for(b=a;b<=n-a-1;b++)
yi[b][n-a-1]=sum++;
for(b=n-2-a;b>=a;b--)
yi[n-a-1][b]=sum++;
for(b=n-a-2;b>=a;b--)
yi[b][a]=sum++;
for(b=a+1;b<n-a-1;b++)
yi[a][b]=sum++;
}
for(c=0;c<n;c++)
{
for(d=0;d<n;d++)
printf("%d ",yi[c][d]);
printf("\n");
}
}
34、
韩信点兵
-
描述
- 相传韩信才智过人,从不直接清点自己军队的人数,只要让士兵先后以三人一排、五人一排、七人一排地变换队形,而他每次只掠一眼队伍的排尾就知道总人数了。输入3个非负整数a,b,c ,表示每种队形排尾的人数(a<3,b<5,c<7),输出总人数的最小值(或报告无解)。已知总人数不小于10,不超过100 。
#include<iostream>
using namespace std;
int main()
{
int a,b,c;
cin>>a>>b>>c;
int n=(a*70+b*21+c*15)%105;
if(n>100||n<10) cout<<"No answer"<<endl;
else cout<<n<<endl;
}
39、
水仙花数
-
描述
-
请判断一个数是不是水仙花数。
其中水仙花数定义各个位数立方和等于它本身的三位数。
#include "stdio.h"
int main(int argc, char const *argv[])
{
int n=0;
//freopen("input.txt","r",stdin);
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;
}
#include<iostream>//最优程序
using namespace std;
int main()
{
int a;
while(1)
{
cin>>a;
if(a==0) break;
cout<<((a==153||a==370||a==371||a==407)?"Yes":"No")<<endl;
}
}
40、
公约数和公倍数
-
描述
- 小明被一个问题给难住了,现在需要你帮帮忙。问题是:给出两个正整数,求出它们的最大公约数和最小公倍数。
#include<stdio.h>
int main() {
int a, b, c, n, k;
scanf("%d", &n); //输入一个整数n(0<n<=10000),表示有n组测试数据
while (n--) {
scanf("%d %d", &a, &b); //输入两个整数
k = a * b;
while (b != 0) {
c = a % b;
a = b;
b = c;
}
printf("%d %d\n", a, k / a);
}
return 0;
}
import java.io.*;
import java.util.*;
public class Main {
public static int gcd(int a, int b) {
return b == 0 ? a : gcd(b, a % b);
}
public static Scanner count = new Scanner(System.in);
public static void main(String[] arges) {
int n = count.nextInt();
while ((n--) > 0) {
int a = count.nextInt(), b = count.nextInt();
System.out.println(gcd(a, b) + " " + a * b / gcd(a, b));
}
count.close();
}
}
#include<stdio.h>
int main()
{
unsigned int u,v,r,s,i,d;
scanf("%u",&s);
for(i=1;i<=s;i++)
{
scanf("%u%u",&u,&v);
d=u*v;
while(v!=0)
{
r=u%v;
u=v;
v=r;
}
printf("%u %u\n",u,d/u);
}
return 0;
}
41、
三个数从小到大排序
import java.util.Scanner;
public class Main
{
public static void main(String[] args)
{
int a,b,c,n;
Scanner num=new Scanner(System.in);
System.out.print("");
a=num.nextInt();
System.out.print("");
b=num.nextInt();
System.out.print("");
c=num.nextInt();
if(a>b) {
n=a;a=b;b=n;
}
if(b>c) {
n=b;b=c;c=n;
}
if(a>c) {
n=a;a=c;c=n;
}
System.out.println(a+" "+b+" "+c);
}
}
#include <stdio.h>//最优程序
int main()
{
int a,b,c,an[3],i,t,j,max,flag;
scanf ("%d %d %d",&an[0],&an[1],&an[2]);
for (i=0;i<3;i++)
{
t=max=an[i];
flag=i;
for (j=i;j<3;j++)
if (an[j]>t)
{
max=an[j];
flag=j;
};
t=an[i];
an[i]=max;
an[flag]=t;
}
for (i=2;i>=0;i--)
printf ("%d ",an[i]);
return 0;
}
56、
阶乘因式分解(一)
-
描述
-
给定两个数m,n,其中m是一个素数。
将n(0<=n<=10000)的阶乘分解质因数,求其中有多少个m。
#include <stdio.h>
int main(void) {
int N, n, m, count;
scanf("%d", &N);
while (N--) {
count = 0;
scanf("%d%d", &n, &m);
while (n) {
n = n / m;
count = count + n;
}
printf("%d\n", count);
}
return 0;
}
#include<iostream>//最优程序
using namespace std;
int get(int n,int num)
{
if(n==0) return 0;
else return get(n/num,num)+n/num;
}
int main()
{
int n;
cin>>n;
while(n--)
{
int a,b;
cin>>a>>b;
cout<<get(a,b)<<endl;
}
}
57、
6174问题
-
描述
-
假设你有一个各位数字互不相同的四位数,把所有的数字从大到小排序后得到a,从小到大后得到b,然后用a-b替换原来这个数,并且继续操作。例如,从1234出发,依次可以得到4321-1234=3087、8730-378=8352、8532-2358=6174,又回到了它自己!现在要你写一个程序来判断一个四位数经过多少次这样的操作能出现循环,并且求出操作的次数
比如输入1234执行顺序是1234->3087->8352->6174->6174,输出是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;
}
#include<iostream>//最优程序
#include<algorithm>
#include<stdio.h>
using namespace std;
int main()
{
//freopen("1.txt","r",stdin);
int k;
cin>>k;
while(k--)
{
int n,a[4],n1,n2;
scanf("%d",&n);
int s=1;
while(n!=6174)
{
a[0]=n%10;
a[3]=n/1000;
a[1]=n/10%10;
a[2]=n/100%10;
sort(a,a+4);
n1=1000*a[3]+100*a[2]+10*a[1]+a[0];
n2=1000*a[0]+100*a[1]+10*a[2]+a[3];
n=n1-n2;
s++;
}
printf("%d\n",s);
}
}
60、
谁获得了最高奖学金
-
描述
-
某校的惯例是在每学期的期末考试之后发放奖学金。发放的奖学金共有五种,获取的条件各自不同:
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),每组测试数据输入的第一行是一个整数X(1 <= X <= 100),表示学生的总数。接下来的X行每行是一位学生的数据,从左向右依次是姓名,期末平均成绩,班级评议成绩,是否是学生干部,是否是西部省份学生,以及发表的论文数。姓名是由大小写英文字母组成的长度不超过20的字符串(不含空格);期末平均成绩和班级评议成绩都是0到100之间的整数(包括0和100);是否是学生干部和是否是西部省份学生分别用一个字符表示,Y表示是,N表示不是;发表的论文数是0到10的整数(包括0和10)。每两个相邻数据项之间用一个空格分隔。
输出
-
每组测试数据输出包括三行,第一行是获得最多奖金的学生的姓名,第二行是这名学生获得的奖金总数。如果有两位或两位以上的学生获得的奖金最多,输出他们之中在输入文件中出现最早的学生的姓名。第三行是这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
样例输出
- <
-
第一行输入数据N,表示测试数据组数(0<N<100),每组测试数据输入的第一行是一个整数X(1 <= X <= 100),表示学生的总数。接下来的X行每行是一位学生的数据,从左向右依次是姓名,期末平均成绩,班级评议成绩,是否是学生干部,是否是西部省份学生,以及发表的论文数。姓名是由大小写英文字母组成的长度不超过20的字符串(不含空格);期末平均成绩和班级评议成绩都是0到100之间的整数(包括0和100);是否是学生干部和是否是西部省份学生分别用一个字符表示,Y表示是,N表示不是;发表的论文数是0到10的整数(包括0和10)。每两个相邻数据项之间用一个空格分隔。