您的位置:首页 > 其它

【NYOJ 分类——语言入门】——汇总(一)

2017-03-29 20:21 211 查看
题目1

题目信息

运行结果

本题排行

讨论区


A+B Problem

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

描述此题为练手用题,请大家计算一下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<iostream>
using namespace std;
int main()
{
int a,b;
cin>>a>>b;
cout<<a+b<<endl;
}


题目4

题目信息

运行结果

本题排行

讨论区


ASCII码排序

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

描述输入三个字符(可以重复)后,按各字符的ASCII码从小到大的顺序输出这三个字符。

输入第一行输入一个数N,表示有N组测试数据。后面的N行输入多组数据,每组输入数据都是占一行,有三个字符组成,之间无空格。
输出对于每组输入数据,输出一行,字符中间用一个空格分开。
样例输入
2
qwe
asd


样例输出
e q w
a d s


来源网络
上传者naonao

#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

题目信息

运行结果

本题排行

讨论区


奇偶数分离

时间限制: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


来源[苗栋栋]原创
上传者苗栋栋

#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数

时间限制: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


来源经典题目
上传者张云聪

#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

题目信息

运行结果

本题排行

讨论区


素数求和问题

时间限制: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


来源[hzyqazasdf]原创
上传者hzyqazasdf

#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

题目信息

运行结果

本题排行

讨论区


素数距离问题

时间限制: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


来源经典题目
上传者hzyqazasdf

#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

时间限制: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#=DbD 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# majorA# minor C# major Db minor
D# major D# minorGb 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<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个数求最值

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

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

输入输入只有一组测试数据,为五个不大于1万的正整数
输出输出两个数,第一个为这五个数中的最小值,第二个为这五个数中的最大值,两个数字以空格格开。
样例输入
1 2 3 4 5


样例输出
1 5


来源C语言课本第四章第一题
上传者张云聪

#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

题目信息

运行结果

本题排行

讨论区


蛇形填数

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

描述在n*n方陈里填入1,2,...,n*n,要求填成蛇形。例如n=4时方陈为:

10 11 12 1

9 16 13 2

8 15 14 3

7 6 5 4

输入直接输入方陈的维数,即n的值。(n<=100)
输出输出结果是蛇形方陈。
样例输入
3


样例输出
7 8 1
6 9 2
5 4 3


来源算法经典
上传者首席执行官

#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

题目信息

运行结果

本题排行

讨论区


韩信点兵

时间限制: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<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;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: