1. 程式人生 > 其它 >吳凡的題庫——快快程式設計1401-1700

吳凡的題庫——快快程式設計1401-1700

  1. 螺旋填表
題目描述
手寫一個如下圖所示螺旋方陣是一件很容易的事,但是計算機打印出來就不容易了,因為印表機的列印頭只能前進不能後退.解決的方法 是認真觀察這 個方陣,從中發現規律,不難發現,螺旋方陣是從裡到外進行填數,每一層的初始位置都固定在左上角,每一層又可分為下、右、上、左四個部分。運 用陣列下標變化規律來控制填數,保證資料按螺旋線順序存入陣列。最後列印這個陣列。
1 16 15 14 13
2 17 24 23 12
3 18 25 22 11
4 19 20 21 10
5 6 7 8 9
輸入輸出格式
輸入格式
一個正整數n,n<=100
輸出格式
n行n列個正整數,注意:行末不能出現空格
輸入輸出樣例
輸入樣例#1:
5
輸出樣例#1:
1 16 15 14 13
2 17 24 23 12
3 18 25 22 11
4 19 20 21 10
5 6 7 8 9
輸入樣例#2:

輸出樣例#2:

輸入樣例#3:

輸出樣例#3:

#include<iostream>
using namespace std;
const int N=109;
int n,a[N][N],b[2]={0,1};
int main(){
	cin>>n;
	int k=n,t=1,p=1;
	for(int i=1;i<=n*n;i++){
		b[p/(k+1)]+=t;
		a[b[0]][b[1]]=i;
		p=p+1;
		if(p>2*k-1){
			p=1;
			k=k-1;
			t=-t;
		}
	}
	for(int i=1;i<=n;i++){
		for(int j=1;j<=n-1;j++)
		    cout<<a[i][j]<<" ";
		cout<<a[i][n];
		cout<<endl;
	}
	return 0;
}
  1. 三角旗2
題目描述
請寫一個程式,輸入是一個正整數n,輸出一個邊長為n的三角旗,高n行,寬n列,由#符合組成,形狀參考樣例:第一行有n-1個空格和1 個#,第二行有n-2個空格和2個#,以此類推。
請注意,每行行首和行末不能有多餘空格。
輸入輸出格式
輸入格式
輸入檔案triangle2.in 輸入一個正整數n,不超過1000
輸出格式
輸出檔案triangle2.out 輸出n行字串
輸入輸出樣例
輸入樣例#1:
5
輸出樣例#1:
    #
   ##
  ###
 ####
#####
輸入樣例#2:

輸出樣例#2:

輸入樣例#3:

輸出樣例#3:

#include<bits/stdc++.h>
using namespace std;
int main(){
	freopen("triangle2.in", "r", stdin);
	freopen("triangle2.out", "w", stdout);
	int n;
	cin>>n;
	for(int i = 1; i <= n;i ++){
		for(int j = 1;j <=n-i; j ++){
			cout<<" ";
		}
		for(int j = 1; j <= i; j ++){
			cout<<"#";
		}
		cout<<endl;
	}
    return 0;
}
  1. 楊輝三角形2
題目描述
輸入一個正整數n,打印出一個n行的楊輝三角形。n<=20。
請注意幾點細節:
1.資料範圍n<=20
2.輸出每一行的行末數字後不可以出現空格。若行末出現空格會被判0分。
輸入輸出格式
輸入格式
輸入檔案yh.in 輸入一個正整數n。
輸出格式
輸出檔案yh.out 輸出共n行,第i行有i個數字由空格隔開。行末無空格。
輸入輸出樣例
輸入樣例#1:
3
輸出樣例#1:
1
1 1
1 2 1
輸入樣例#2:
5
輸出樣例#2:
1
1 1
1 2 1
1 3 3 1
1 4 6 4 1
輸入樣例#3:

輸出樣例#3:

#include<bits/stdc++.h>
using namespace std;
int tri[25][25], n;
int main(){
	freopen("yh.in", "r", stdin);
	freopen("yh.out", "w", stdout);
	cin>>n;
	tri[1][1] = 1;
	for(int i = 2; i <=n; i ++)
	    for(int j = 1; j <= i; j ++)
	        tri[i][j] = tri[i-1][j-1] + tri[i-1][j];
	for(int i = 1; i<=n; i ++){
	    for(int j = 1; j <=i-1; j ++)
	        cout<<tri[i][j]<<" ";
	    cout<<tri[i][i]<<endl;
	}
	return 0;
}
  1. 智商排序
題目描述
智商這個詞的英文時intelligence quotient,簡寫為IQ。已知有n個學生,他們每個人的智商都是一個正整數,請將他們的智商從大到小排序
輸入輸出格式
輸入格式
輸入檔案intelligence.in  輸入第一行為正整數n,n<=200000。第二行為n個正整數,代表每個人的智商,由空格隔開,均不超過250。
輸出格式
輸出檔案intelligence.out 輸出一行共n個正整數,由空格隔開。
輸入輸出樣例
輸入樣例#1:
3
90 120 100
輸出樣例#1:
120 100 90
輸入樣例#2:
無
輸出樣例#2:
無
輸入樣例#3:
無
輸出樣例#3:
無
#include<bits/stdc++.h>
using namespace std;
int main(){
	freopen("intelligence.in", "r", stdin);
	freopen("intelligence.out", "w", stdout);
	int n,a[500001];
	cin>>n;
	for(int i=0;i<n;i++)		cin>>a[i];
	sort(a, a+n);
	reverse(a, a+n);
	for(int i=0;i<n;i++)	cout<<a[i]<<" ";
	return 0;
}
  1. 高考排名
題目描述
你是校長,學校共n名學生參加高考,學號從1到n編號。每個學生有一個總分,你需要將學生成績從高到低排序。如果分數相同,學號小 的排在前面。
輸入輸出格式
輸入格式
輸入檔案rank.in 輸入第一行為正整數n,n<=100000。第二行共n個正整數,依次代表n個人的總分:第1個分數對應學號為1的學生,第2 個分數對應學 號為2的學生,以此類推,最後一個分數對應學號為n的學生。分數均不超過1000。
輸出格式
輸出檔案rank.out 輸出共n行,每行兩個數字,由空格隔開,第一個數為學號,第二個數字為該學生的總分。注意行末不能有空格。
輸入輸出樣例
輸入樣例#1:
4
59 60 60 100
輸出樣例#1:
4 100
2 60
3 60
1 59
輸入樣例#2:
無
輸出樣例#2:
無
輸入樣例#3:
無
輸出樣例#3:
無
#include<bits/stdc++.h>
using namespace std;
const int N=100001;
struct student{
	int id, score;
};
bool cmp(const student&a, const student&b){
	if(a.score>b.score)	return true;
	if(a.score<b.score)	return false;
	if(a.id<b.id)	return true;
	return false;
}
int main(){
	freopen("rank.in", "r", stdin);
	freopen("rank.out", "w", stdout);
	int n, cnt=0;
	student s[N];
	cin>>n;
	for(int i=0;i<n;i++){
		cnt++;
		cin>>s[i].score;
		s[i].id = cnt;
	}
	sort(s, s+n, cmp);
	for(int i=0;i<n;i++){
		cout<<s[i].id<<" "<<s[i].score<<endl;
	}
	return 0;
}
  1. 單詞查詢
題目描述
輸入一行字串a,以換行結束(字串長度<=100)。該字串由若干個單片語成,單詞之間用一個空格隔開,所有單詞區分大小寫。請問某個單詞b,是否在字串a中獨立出現,請定位b的位置。
輸入輸出格式
輸入格式
輸入包括2行,第1行是包含多個英文單詞的字串 a;第2行是待查詢的單詞b(長度 <= 100),b是由若干個英文字母組成中間沒有其他符 號。
輸出格式
如果找到了就輸出該單詞b第一次出現時首字母在a的第幾個位置。如果沒有找到獨立的單詞b就輸出sorry
輸入輸出樣例
輸入樣例#1:
wa ha ha
ha
輸出樣例#1:
4
輸入樣例#2:
A pineapple is not an apple
apple
輸出樣例#2:
23
輸入樣例#3:
abcdefghijk
abc
輸出樣例#3:
sorry
#include<bits/stdc++.h>
using namespace std;
int main(){
	string s, word;
	int id;
	getline(cin, s);
	getline(cin, word);
	s = " " + s + " ";
	id = s.find(" " + word + " ");
	if(id != -1)	cout<<id + 1;
	else	cout<<"sorry";
	return 0;
}
  1. 二進位制轉十進位制
題目描述
將一個二進位制數轉換成十進位制數。
輸入輸出格式
輸入格式
輸入一行01串,長度不超過60。
輸出格式
輸出一個整數。
輸入輸出樣例
輸入樣例#1:
111
輸出樣例#1:
7
輸入樣例#2:
1111
輸出樣例#2:
15
輸入樣例#3:

輸出樣例#3:

#include<iostream>
#include<string>
#include<cmath> 
using namespace std;
int main(){
	long long result=0;
	string s;
	cin>>s;
	for(int i=0; i<s.size(); i++){ 
		result *= 2;
		if(s[i]=='1')result++; 
	}
	cout<<result; 
	return 0; 
}
  1. 站崗放哨2
題目描述
一條直線上,你安排了n個哨兵站崗放哨,編號從1到n。其中i號哨兵的座標位置是x[i]。不會有哨兵站在相同的位置。作為指揮官,你需要知道3個資訊:
1.從左到右,每個哨兵的座標依次是幾?
2.從左到右,每個哨兵依次是幾號哨兵?
3.哨兵編號從1到n,每個哨兵依次站在從左到右的第幾個?
輸入輸出格式
輸入格式
輸入檔案guard.in
輸入第一行包含正整數n。1<=n<=100000。第二行共n個正整數,代表每個哨兵的座標,均不超過1000000000。
輸出格式
輸出檔案guard.out
輸出共3行,每行n個正整數。
輸入輸出樣例
輸入樣例#1:
5
7 8 10 6 9
輸出樣例#1:
6 7 8 9 10
4 1 2 5 3
2 3 5 1 4
輸入樣例#2:
無
輸出樣例#2:
無
輸入樣例#3:
無
輸出樣例#3:
無
#include<bits/stdc++.h>
using namespace std;
struct guard{
	int x, id;
};
bool cmp(const guard&u,const guard&v){
	return u.x<v.x;
}
int main(){
	freopen("guard.in","r",stdin);
	freopen("guard.out","w",stdout);
	int n, rk[100000];
	guard g[100000];
	cin>>n;
	for(int i=1;i<=n;i++) cin>>g[i].x;
	for(int i=1;i<=n;i++) g[i].id=i;
	sort(g+1,g+1+n,cmp);
	for(int i=1;i<=n;i++) rk[g[i].id]=i;
	for(int i=1;i<=n;i++) cout<<g[i].x<<" ";
	cout<<endl;
	for(int i=1;i<=n;i++) cout<<g[i].id<<" ";
	cout<<endl;
	for(int i=1;i<=n;i++) cout<<rk[i]<<" ";
	cout<<endl;
	return 0;
} 
  1. 並列排名2
題目描述
你作為教務老師,手上有一份n名學生的成績,你需要計算每個學生排第幾名,注意會出現並列名次。
輸入輸出格式
輸入格式
輸入檔案rank.in
輸入第一行為正整數n,n<=100000。第二行是n個正整數分數,在0到1000000000之間。
輸出格式
輸出檔案rank.out
輸出共一行,有n個正整數依次代表每人的名次,由空格隔開。
輸入輸出樣例
輸入樣例#1:
4
59 100 99 100
輸出樣例#1:
4 1 3 1
輸入樣例#2:
無
輸出樣例#2:
無
輸入樣例#3:
無
輸出樣例#3:
無
#include<bits/stdc++.h>
using namespace std;
int n;
int x[100008];
int id[100008];
int rk[100008];
bool cmp(const int&a,const int&b){
	return x[a]>x[b];
}
int main(){
	freopen("rank.in", "r", stdin);
	freopen("rank.out", "w", stdout);
	cin>>n;
	for(int i=1;i<=n;i++) cin>>x[i];
	for(int i=1;i<=n;i++) id[i]=i;
	sort(id+1,id+1+n,cmp);
	for(int i=1;i<=n;i++) rk[id[i]]=i;
	
	for(int i=1;i<=n-1;i++)
		if(x[id[i]]==x[id[i+1]])
		    rk[id[i+1]]=rk[id[i]];
	for(int i=1;i<=n;i++) cout<<rk[i]<<" ";
	return 0;
}
  1. 列舉子集
題目描述
有n個數字組成的集合,{1,2,3,...,n}。請列舉所有非空子集。



輸出的先後順序遵循以下規則:

1.每行輸出一個子集,每個子集的元素從小到大輸出,由空格隔開,行末不能有空格。

2.較小數字開頭的子集比較大數字開頭的子集先輸出。

3.開頭數字一樣的話,再依次比較後續數字。後續有數字的子集先輸出,後續沒有數字的子集後輸出。

輸入輸出格式
輸入格式
輸入檔案subsets.in
輸入第一行包含正整數n。1<=n<=15。
輸出格式
輸出檔案subsets.out
輸出共n行,每行若干個正整數。
輸入輸出樣例
輸入樣例#1:
3
輸出樣例#1:
1 2 3
1 2
1 3
1
2 3
2
3
輸入樣例#2:
無
輸出樣例#2:
無
輸入樣例#3:
無
輸出樣例#3:
無
#include<bits/stdc++.h>
using namespace std;
int n,ok[20],p[20];
void print(){
	int m=-1;
	for(int i=0;i<=n;i++)
	    if(ok[i])p[++m]=i;
	if(m==-1)return;
	for(int i=0;i<m;i++)
	    cout<<p[i]<<" ";
	cout<<p[m]<<endl;
}
void dfs(int x){
	if(x==n+1){
		print();
		return;
	}
	ok[x]=1;
	dfs(x+1);
	ok[x]=0;
	dfs(x+1);
}
int main(){
	freopen("subsets.in","r",stdin);
	freopen("subsets.out","w",stdout); 
	cin>>n;
	dfs(1);
	return 0;
}
  1. 列舉組合
題目描述
有n個數字組成的集合,{1,2,3,...,n}。給定一個m,1<=m<=n。對於從n個數裡選m個數的組合情況,請列舉所有可能方案。



輸出的先後順序遵循以下規則:

1.每行輸出一個組合,每個組合的元素從小到大輸出,由空格隔開,行末不能有空格。

2.較小數字開頭的組合大數字開頭的組合先輸出。

3.開頭數字一樣的話,再依次比較後續數字。

輸入輸出格式
輸入格式
輸入檔案combinations.in
輸入第一行包含正整數n和m。1<=m<=n<=15。
輸出格式
輸出檔案combinations.out
輸出共C(n,m)行,每行若干個正整數。
輸入輸出樣例
輸入樣例#1:
3 2
輸出樣例#1:
1 2
1 3
2 3
輸入樣例#2:
無
輸出樣例#2:
無
輸入樣例#3:
無
輸出樣例#3:
無
#include<bits/stdc++.h>
using namespace std;
int n,m,p[20];
void print(){
	for(int i=1;i<m;i++)
	    cout<<p[i]<<" ";
	cout<<p[m]<<endl;
}
void dfs(int x,int c){
	if(c==m){
		print();
		return;
	}
	if(c+n+1-x<m)return;
	if(x==n+1)return;
	p[c+1]=x;
	dfs(x+1,c+1);
	dfs(x+1,c);
}
int main(){
	freopen("combinations.in","r",stdin);
	freopen("combinations.out","w",stdout); 
	cin>>n>>m;
	dfs(1,0);
	return 0;
}