南阳oj水题集合,语言的灵活运用

a+b

输入
输入两个数,a,b
输出
输出a+b的值
样例输入
2 3
样例输出
5

c/c++

#include<iostream>
using namespace std;
int main()

{   int a,b;
    cin>>a>>b;
    cout<<(a+b);
}

java

import java.util.Scanner;

public class Main{//ACM中,提交的时候记得不要带package,而且类名一定要写Main,带上package会超时
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int a = scanner.nextInt();
        int b = scanner.nextInt();
        System.out.println(a+b);
    }
    

}

虽然这两道题很简单但是主要是用来熟悉语言的应用

-------------------------------------------------------------------------------------------------------------------------------------------------------------------

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

c/c++

#include<iostream>
using namespace std;
int main()

{   int  n ;
    char a,b,c,temp;
    cin>>n;
    while(n--)
    {
       cin>>a>>b>>c;
       if(a>b)
       {
         temp=a;
         a=b;
         b=temp;
       }
       if(a>c)
       {
           temp=a;a=c;c=temp;
       }
       if(b>c)
       {
        temp=b;b=c;c=temp;
       }

       cout<<a<<" "<<b<<" "<<c<<endl;
    }

    }

java

import java.util.Arrays;
import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int n = scanner.nextInt();
        for(int i=0;i<n;i++)
        {    
             char[]ch = new char[3];
             String str = scanner.next();
             ch = str.toCharArray();将字符串转化成字母存放到数组里面
             Arrays.sort(ch);//对ch数组进行sort排序,注意Arrays.sort()的用法
             System.out.println(ch[0]+" "+ch[1]+" "+ch[2]);
            
        }
    }
    

}

Arrays.sort():数组的sort排序

toCharArray():将字符串转换成呢单字符然后存放到数组中

虽然用java在时间还是空间上超过了c

-------------------------------------------------------------------------------------------------------------------------------------------------------------------

Fibonacci数
时间限制:3000 ms  |  内存限制:65535 KB
难度:1
描述
无穷数列1,1235813213455...称为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

java

package day05;

import java.util.Scanner;

public class Main {
     
        public static void main(String[] args) {
             int []a = new int[22];
             a[0]=1;
             a[1]=1;
             a[2]=2;
             for(int  i=3;i<20;i++)
             {
                 a[i] = a[i-1]+a[i-2];
                 
             }
//             System.out.println(a);
//             for(int i=0;i<22;i++)
//                 System.out.println(a[i]);
             Scanner scanner = new Scanner(System.in);
             int n = scanner.nextInt();
             for(int i=0;i<n;i++)
             {
                 int j = scanner.nextInt();
                 System.out.println(a[j-1]);
                 
             }
            
            
            
        }
        
}

c/c++

#include<iostream>
using namespace std;
int main()
{
    int a[100000];
    int n;
    int i;
    a[0]=1;
    a[1]=1;
    a[2]=2;
    for(i=2;i<=20;i++)
    {
        a[i] =a[i-1]+a[i-2];
      //  cout<<a[i]<<endl;
    }

    cin>>n;
    while (n--)
    {
        cin>>i;
        cout<<a[i-1]<<endl;
    }

}

注意有递推关系的数列,一定要有起始的数字定义,然后将整个数组根据规律全都赋值

-------------------------------------------------------------------------------------------------------------------------------------------------------------------

素数求和问题
时间限制: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

c/c++

#include<iostream>
#include<math.h>
using namespace std;

int main()
{
  int t,n;
  cin>>t;
  while(t--)
  {   int sum=0;
      cin>>n;
      while(n--)
      {
          int a,i;
          cin>>a;

          for( i=2;i<=sqrt(a);i++)
          {
             if(a%i==0)
             break;

             if((a%i!=0))
             continue;
          }
          if(i>sqrt(a)&&(a!=1))
          sum+=a;


      }
      cout<<sum<<endl;

  }
}

java

import java.util.Scanner;

public class Main {
     
        public static void main(String[] args) {
            
            Scanner scanner = new Scanner(System.in);
            int t = scanner.nextInt();
            while(t-->0)
            {
                int n = scanner.nextInt();
                int sum=0;
                while(n-->0)
                {   int i;
                    int a = scanner.nextInt();
                    for(i=2;i<=Math.sqrt(a);i++ )
                    {
                        if(a%i==0)
                            break;
                        if(a%i!=0)
                            continue;
                        
                    }
                    if((i>Math.sqrt(a))&&(a!=1))
                        sum +=a;
                }
                System.out.println(sum);
                
                
            }
            
        }
        
}

1不是素数

-------------------------------------------------------------------------------------------------------------------------------------------------------------------

素数距离问题
时间限制: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

c/c++

#include<iostream>
#include<math.h>
using namespace std;
int sushu(int x)
{   int i,flag;
    for( i=2;i<=sqrt(x);i++)
    {
        if(x%i==0)
        {
          flag=0;
          break;
        }

        if(x%i!=0)
        {
            continue;
        }

    }
    if(i>sqrt(x))
    {
        flag=1;
    }
    return flag;
}


int main()
{
    int n;
    cin>>n;
    while(n--)
    {
        int m,left,right,l,r;
        cin>>m;
        if(m==1)
        {
            cout<<2<<" "<<1<<endl;
        }
       else if(sushu(m)!=0)
        {
            cout<<m<<" "<<0<<endl;
        }

        else
        {
            for(l=m;l>1;l--)
            {
                if(sushu(l)!=0)
                {
                    left = m-l;
                    break;
                }
                else
                continue;
            }
            for(r=m; ;r++)
            {
                if(sushu(r)!=0)
                {
                    right = r-m;
                    break;
                }
                else
                continue;
            }
          if(right<left)
        cout<<r<<" "<<right<<endl;
        else
        cout<<l<<" "<<left<<endl;
        }




    }

}

关键注意1的问题

java

import java.util.Scanner;

public class Main {
    public static int sushu(int x)
    {
        int i,flag = 0;
        for(i=2;i<=Math.sqrt(x);i++)
        {
            if(x%i==0)
            {
                flag=0;
                break;
            }
            if(x%i!=0)
            {
                 
                continue;
            }
            
            
        }
        if(i>Math.sqrt(x))
         flag=1;
        return flag;
    }
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int n = scanner.nextInt();
        while(n-->0)
        {   int l,left = 0,r,right=0;
            int m = scanner.nextInt();
            if(m==1)
                System.out.println("2"+" "+"1");
            else if(sushu(m)==1)
            {
                System.out.println(m+" "+0);
                
            }
            else
            {
                for(l=m;l>1;l--)
                {
                    if(sushu(l)==1)
                    {
                        left = m-l;
                        break;
                    }
                    else
                        continue;
                    
                }
                for(r=m; ; r++)
                {
                    if(sushu(r)==1)
                    {
                        right = r-m;
                        break;
                    }
                    else
                        continue;
                }
                
                if(right<left)
                {
                    System.out.println(r+" "+right);
                }
                else
                {
                    System.out.println(l+" "+left);
                    
                }
            }
                
            
        }
        
        
    }

}

-------------------------------------------------------------------------------------------------------------------------------------------------------------------

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<iostream>
#include<string>
#include<cstdio>
using namespace std;
int main()
{
    int flag,core=0;
   char a[100000];
    while(gets(a))
    {   flag=0;
        core++;
        if(a[0]=='A'&&a[1]=='#')
        {
          a[0]='B';
          a[1]='b';
        }
        else if(a[0]=='B'&&a[1]=='b')
        {
           a[0]='A';
           a[1]='#';
        }
        else if (a[0]=='C'&&a[1]=='#')
        {
          a[0]='D';
          a[1]='b';

        }
        else if(a[0]=='D'&&a[1]=='b')
        {
           a[0]='C';
           a[1]='#';
        }
        else if(a[0]=='D'&&a[1]=='#')
        {
            a[0]='E';
            a[1]='b';
        }
        else if(a[0]=='E'&&a[1]=='b')
        {
            a[0]='D';
            a[1]='#';
        }
        else if(a[0]=='F'&&a[1]=='#')
        {
            a[0]='G';
            a[1]='b';
        }
        else if(a[0]=='G'&&a[1]=='b')
        {
            a[0]='F';
            a[1]='#';
        }
        else if(a[0]=='G'&&a[1]=='#')
        {
            a[0]='A';
            a[1]='b';
        }
        else if(a[0]=='A'&&a[1]=='b')
        {
            a[0]='G';
            a[1]='#';
        }
        else {
           flag=1;
        }
        if(flag==0)
        {
            cout<<"Case "<<core<<": "<<a<<endl;

        }
        else{
        cout<<"Case "<<core<<": "<<"UNIQUE"<<endl;}


    }
}

就是一个字符串的替换问题,gets()可以收入空格,用java的话也一样只不过这里用到

ch = str.toCharArray();将字符串转化成字母存放到数组里面,然后比较前两位就可以
还有一种取字符串的方法在这里并不是很实用,就是 ch = str.charAt(0);ch = str.charAt(1);
---------------------------------------------------------------------------------------------------

蛇形填数

时间限制: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<iostream>
#include<string>
#include<cstdio>
#include<cstdio>
#include<cstring>
using namespace std;
int main()
{
    int a[101][101];

    int n;
    cin>>n;
    int x=0,y=n-1;
    memset(a,0,sizeof(a));
    a[0][n-1]=1;
    int val = 1;
    while(val<n*n)
    {
        while(x+1<n&&a[x+1][y]==0) a[++x][y]=++val;

        while(y-1>=0&&a[x][y-1]==0) a[x][--y]=++val;

        while(x-1>=0&&a[x-1][y]==0) a[--x][y]=++val;

        while(y+1<n&&a[x][y+1]==0) a[x][++y]=++val;
    }
    for(x=0;x<n;x++)
     {

      for(y=0;y<n;y++)
      {
        cout<<a[x][y];
      }
       cout<<endl;
     }

}

这个蛇形填数非常容易错,多看看理解理解,为什么val<n*n那里有必要多想想,还有为什么++x或者++y也要注意

package day05;

import java.util.Scanner;

public class Main {
     
        public static void main(String[] args) {
            Scanner scanner = new Scanner(System.in);
            int n = scanner.nextInt();
            int [][]a = new int[101][101];
            int x=0;
            int y=n-1;
            int val=1;
            a[0][n-1]=1;
            while(val<n*n)
            {
                while(x+1<n&&a[x+1][y]==0) a[++x][y]=++val;
                while(y-1>=0&&a[x][y-1]==0) a[x][--y]=++val;
                while(x-1>=0&&a[x-1][y]==0) a[--x][y]=++val;
                while(y+1<n&&a[x][y+1]==0)  a[x][++y]=++val;
                
                
                
            }
            for(x=0;x<n;x++)
            {
                for(y=0;y<n;y++)
                    System.out.print(a[x][y]);
                System.out.println();
            }
            
            
            
        }
}
但是如果要变成
       while((x+1<n)&&a[++x][y]==0) a[++x][y]=++num;
       while((y-1>=0)&&a[x][--y]==0) a[x][--y]=++num;
       while((x-1>=0)&&a[--x][y]==0)a[--x][y]=++num;
       while((y+1<n)&&a[x][++y]==0) a[x][++y]=++num;

结果就会不一样,why········关于++的使用以及+1的使用一定要注意


------------------------------------------------------------------------------------------------------------------------------------------------------- 

韩信点兵
时间限制: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>
#include<cstring>
#include<cmath>
#include<math.h>

#include<stdio.h>
using namespace std;
int main()
{
    double a,b,c;
    double i;
    cin>>a>>b>>c;
    for(i=10;i<=100;i++)
    {
        if(fmod(i,3.0)==a&&fmod(i,5.0)==b&&fmod(i,7.0)==c)
        cout<<i;
        if(i>100)
        cout<<"No answer";


    }

}

math.h

数学函数库,一些数学计算的公式的具体实现是放在math.h里,具体有:
1、 三角函数
double sin(double);正弦
double cos(double);余弦
double tan(double);正切
2 、反三角函数
double asin (double); 结果介于[-PI/2,PI/2]
double acos (double); 结果介于[0,PI]
double atan (double); 反正切(主值),结果介于[-PI/2,PI/2]
double atan2 (double,double); 反正切(整圆值),结果介于[-PI,PI]
3 、双曲三角函数
double sinh (double);
double cosh (double);
double tanh (double);
4 、指数与对数
double frexp(double value,int *exp);这是一个将value值拆分成小数部分f和(以2为底的)指数部分exp,并返回小数部分f,即f*2^exp。其中f取值在0.5~1.0范围或者0。
double ldexp(double x,int exp);这个函数刚好跟上面那个frexp函数功能相反,它的返回值是x*2^exp
double modf(double value,double *iptr);拆分value值,返回它的小数部分,iptr指向整数部分。
double log (double); 以e为底的对数
double log10 (double);以10为底的对数
double pow(double x,double y);计算x的y次幂
float powf(float x,float y); 功能与pow一致,只是输入与输出皆为浮点数
double exp (double);求取自然数e的幂
double sqrt (double);开平方
5 、取整
double ceil (double); 取上整,返回不比x小的最小整数
double floor (double); 取下整,返回不比x大的最大整数,即高斯函数[x]
6 、绝对值
int abs(int i); 求整型的绝对值
double fabs (double);求实型的绝对值
double cabs(struct complex znum);求复数的绝对值
7 、标准化浮点数
double frexp (double f,int *p); 标准化浮点数,f = x * 2^p,已知f求x,p (x介于[0.5,1])
double ldexp (double x,int p); 与frexp相反,已知x,p求f
8 、取整与取余
double modf (double,double*); 将参数的整数部分通过指针回传,返回小数部分
double fmod (double,double); 返回两参数相除的余数
9 、其他
double hypot(double x,double y);已知直角三角形两个直角边长度,求斜边长度
double ldexp(double x,int exponent);计算x*(2的exponent次幂)
double poly(double x,int degree,double coeffs []);计算多项式
int matherr(struct exception *e);数学错误计算处理程序
source: 《C & C++ Code Capsules》
原文地址:https://www.cnblogs.com/mmlovejj/p/4893089.html