有些同学没学过c++,所以不想用c++,只想用c。
写c的第一个程序的也不见得各个语句什么意思都知道吧
在算法竞赛中用到的c++一般也就STL部分,其他可以按照c的写。
就是头文件变了,加了句using namespace std;
其他的c怎么写就怎么写就ok了
定义常量啥的习惯用#define就用#define呗
C++兼容C,C不兼容C++
//C
#include<stdio.h>
int main(){
printf("hello world\n");
return 0;
}
//C++
#include<cstdio>
using namespace std;
int main(){
printf("hello world\n");
return 0;
}
7-1 分段排序 (10分)
在一个长度为10的数组中输入10个整数,把前5个数从小到大排序,后5个数从大到小排序。
输入格式:
输入10个整数。
输出格式:
排序后的数组,数与数间用空格分割。
输入样例:
5 9 2 4 6 7 1 3 0 8
输出样例:
2 4 5 6 9 8 7 3 1 0
#include<cstdio>
#include<algorithm>
using namespace std;
int a[15];
bool cmp(int a,int b){
return a>b;
}
int main(){
for(int i=0;i<10;i++) scanf("%d",&a[i]);
sort(a,a+5);
sort(a+5,a+5+5,cmp);
for(int i=0;i<10;i++) printf("%d ",a[i]);
return 0;
}
7-2 单词排序 (10分)
请编写程序,输入一系列单词,然后按字典顺序排序,最后输出结果。
输入格式
第一行为单词的数量 n (0<n<10000),后面有 n 行,每行一个英文单词(均由小写字母组成,且长度均不超过15个英文字母)
输出格式
共 n 行,每行一个英文单词(按字典顺序排列)
输入样例
3
stick
stock
stack
输出样例
stack
stick
stock
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
const int N=1e4+5;
struct node{
char s[20];
}a[N];
bool cmp(node a,node b){
return strcmp(a.s,b.s)<0;
}
int main(){
int n;
scanf("%d",&n);
for(int i=1;i<=n;i++){
scanf("%s",a[i].s);
}
sort(a+1,a+1+n,cmp);
for(int i=1;i<=n;i++){
printf("%s\n",a[i].s);
}
return 0;
}
7-3 DKS学长和字符串 (15分)
我们可爱的dks学长特别喜欢字符串,就和他喜欢敲代码一样,可是我们的dks学长只喜欢英文字母(大写和小写),其他的字符他碰都不想碰,现在dks学长得到了若干个字符串,他想把这个字符串排序(区分大小写)。
排序的要求是按逆字典序排序(区分大小写),另外别忘了,可爱的dks学长只喜欢英文字母
输入格式:
第一行一个数字n ,代表有n组测试数据
接下来第2到第n+1行,每行会有一个字符串(只会出现大写和小写的英文字母以及数字,字符串长度小于10000位)
输出格式:
对应每个字符串,输出它按要求排序的结果
输入样例:
在这里给出一组输入。例如:
1
111dkstql111
输出样例:
在这里给出相应的输出。例如:
111tsqlkd111
#include<cstdio>
#include<algorithm>
#include<cstring>
using namespace std;
const i