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

这篇博客汇总了NYOJ平台上的语言入门系列,首篇聚焦于A+B Problem。详细介绍了题目的基本信息,展示了运行结果,并提供了本题的排行榜和讨论区链接,是ACM语言入门者的参考资料。
摘要由CSDN通过智能技术生成

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;
}        

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);	
		
	
	}

}
        

奇偶数分离

时间限制: 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("");
	}
}        

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);}}        

素数求和问题

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

素数距离问题

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

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;
}
        

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;
}        

蛇形填数

时间限制: 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");
	}
}        

韩信点兵

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


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值