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码从小到大的顺序输出这三个字符。
#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中的所有奇数从小到大输出,再把所有的偶数从小到大输出。
#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)
#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
-
第一行给出整数M(0<M<10)代表多少组测试数据
#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
-
第一行给出测试数据组数N(0<N<=10000)
#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#=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<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
#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 。
#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; }