這兩天臨時抱佛腳練了一下代碼哩牍,深感自己水平不足囱怕,等考完研再加強練習(xí)棺棵。
01 芯片分配 - 2022_1/3
大概題意是一個芯片 10G,A 套餐組裝需要 2.5 G食寡,B 套餐組裝需要 10G
第一行輸入芯片塊數(shù) m雾狈,第二行輸入套參數(shù) n,第三行輸入各個套餐的類型
從前往后塞套餐抵皱,并編號善榛,輸入最后一組套餐芯片所在塊號辩蛋、編號。
#include <iostream>
#include <cstring>
#include <stdlib.h>
#include <math.h>
using namespace std;
int main(){
int n,m,i,j,k,flag;
char c;
cin >> n;
cin >> m;
float Seriese[32][2];
for(i=0;i<n;i++){
Seriese[i][0] = 10;
Seriese[i][1] = 0;
}
for(i=0;i<m;i++){
cin >> c;
// cout << "c: "<<c<<endl;
if(c == 'A'){
flag = 0;
for(j=0; j<n; j++){
if(Seriese[j][0] >=2.5){
// cout << Seriese[j][0] <<"-2.5= ";
Seriese[j][0] -=2.5;
Seriese[j][1] ++;
flag = 1;
// cout << Seriese[j][0] <<endl;
if(i==m-1){
cout << (j+1) << "\n" << Seriese[j][1];
return 0;
}
break;
}
}
if(flag==0){
cout << "0\n0";
return 0;
}
}
else if(c=='B'){
flag = 0;
for(j=0; j<n; j++){
if(Seriese[j][0] ==10){
Seriese[j][0] =0;
Seriese[j][1] =1;
flag = 1;
if(i==m-1){
cout << (j+1) << "\n1";
return 0;
}
break;
}
}
if(flag ==0){
cout << "0\n0";
return 0;
}
}
}
return(0);
}
=> 5
=> 6
=> A B A B A A A
1
4
02 迷宮最短路徑 - 2022_2/3
給定一個大小為 N×M 的迷宮移盆。迷宮由通道和墻壁組成悼院,每一步可以向鄰接的上下左右四格的通道移動。請求出從起點到終點所需的小步數(shù)咒循。'#', '.' ,'S' ,'G' 分別表示墻壁据途,通道,起點剑鞍,終點昨凡。
- DFS 實現(xiàn)(遞歸)
#include <iostream>
#include <cstdio>
using namespace std;
int p0,q0,p1,q1; //起點和終點的坐標(biāo)
char a[100][100]; //地圖數(shù)組
int book[100][100]; //標(biāo)記數(shù)組
int next[4][2]={{0,1},{1,0},{0,-1},{-1,0}}; //下一步的可能情況
int mini=10000; //最小路徑初值
int n,m;
void dfs(int x,int y,int step){
int nx,ny,k;
if(x==p1&&y==q1){ //到達終點
if(step<mini)
mini=step;
return;
}
for(k=0;k<4;k++){ //四種走向
nx=x+next[k][0];
ny=y+next[k][1];
if(nx<1||nx>n||ny<1||ny>m) //控制越界
continue;
if(book[nx][ny]==0 && (a[nx][ny]=='.'||a[nx][ny]=='G')){ //若此方向能走通
book[nx][ny]=1;
dfs(nx,ny,step+1);
book[nx][ny]=0;
}
}
return;
}
int main(){
cin>>n>>m;
for(int i=1;i<=n;i++){ //地圖初始化
for(int j=1;j<=m;j++){
cin>>a[i][j];
if(a[i][j]=='S'){
p0=i;
q0=j;
}
if(a[i][j]=='G'){
p1=i;
q1=j;
}
}
}
book[p0][q0]=1; //標(biāo)記起點
dfs(p0,q0,0);
cout<<"shortest path: "<<mini<<endl;
return 0;
}
- BFS 實現(xiàn)(非遞歸)
#include <iostream>
#include <cstdio>
using namespace std;
char a[100][100]; //地圖數(shù)組
int book[100][100]; //標(biāo)記數(shù)組
int sx,sy,gx,gy; //起點和終點的坐標(biāo)
int next[4][2]={{1,0},{0,1},{-1,0},{0,-1}}; //下一步的可能情況
int n,m;
struct node{
int x,y,step;
};
int main(){
int head,tail,i,j,k,tx,ty;
head=tail=0;
cin>>n>>m;
for(int i=1;i<=n;i++){ //地圖初始化
for(int j=1;j<=m;j++){
cin>>a[i][j];
if(a[i][j]=='S'){
sx=i;
sy=j;
}
if(a[i][j]=='G'){
gx=i;
gy=j;
}
}
}
node q[10000]; //100*100的地圖隊列
q[tail].x=sx; //將起點壓入隊列
q[tail].y=sy;
q[tail].step=0;
book[sx][sy]=1;
tail++;
int flag=0;
while(head<tail){
for(k=0;k<4;k++){ //四種走向
tx=q[head].x+next[k][0];
ty=q[head].y+next[k][1];
if(tx<1||tx>n||ty<1||ty>m) //控制越界
continue;
if(book[tx][ty]==0&&a[tx][ty]!='#'){ //若此方向能走通
q[tail].x=tx;
q[tail].y=ty;
q[tail].step=q[head].step+1;
book[tx][ty]=1;
tail++;
}
if(tx==gx&&ty==gy){ //到達終點
flag=1;
break;
}
}
if(flag)
break;
head++;
}
cout<<"shortest path: "<<q[tail-1].step<<endl;
return 0;
}
03 小朋友分組 - 2021_1/3
題目:
幼兒園老師安排小朋友做游戲,現(xiàn)在需要給N個小朋友進行分組蚁署,老師讓每個同學(xué)寫一個名字便脊,代表這位小朋友想和誰分到一組。請問老師在滿足所有小朋友意愿的情況下光戈,最多可以將班級分成多少組哪痰?
輸入:
第一行輸入N,0<N<=100000
接下來是N行代表每個小朋友希望和誰分到一組久妆,如"John Jack"晌杰,代表John希望和Jack分到一組,兩個名字之間以空格分割筷弦,名字本身不存在空格肋演。
輸出:
分組的最多數(shù)量。
#include <iostream>
#include <cstring>
using namespace std;
int main(){
string student[100000][2];
int N,i,j;
int TwoPeople=0,other=0;
cin>>N;
for(i=0;i<N;i++){
cin>>student[i][0]>>student[i][1];
}
for(i=0;i<N;i++){
for(j=i+1;j<N;j++){
if((student[i][1]==student[j][0]) && (student[i][0]==student[j][1])){
TwoPeople++;
student[i][0]=student[i][1]=" ";
student[j][0]=student[j][1]=" ";
}
}
}
while(student[0][0]!=" "){
for(i=0;i<N;i++){
for(j=0;j<N;j++){
if((student[i][1]==student[j][0]) && (student[i][1]!="?")){
student[i][1]=student[j][1];
student[j][0]=student[j][1]=" ";
}
}
}
for(i=0;i<N;i++){
if((student[i][0]==student[i][1]) && (student[i][0]!=" "))
other++;
}
}
// for(int i=0;i<N;i++){
// cout<<student[i][0]<<" "<<student[i][1]<<endl;
// }
cout<<(TwoPeople+other)<<endl;
return(0);
}
=>6
=>Jack Tom
=>Alice John
=>Jessica Leonie
=>Tom Alice
=>John Jack
=>Leonie Jessica
2
04 路線規(guī)劃 - 2021_3/3
題目:
設(shè)計一條樂園游玩路線烂琴,使得:
游玩總時長不超過t爹殊,但盡可能接近t
不走回頭路,僅向右或向下兩個方向奸绷,直到出口
樂園是一個row行col列的方格區(qū)域梗夸,每個區(qū)域上都標(biāo)注了游玩的時長,從[0,0]出發(fā)号醉,選擇符合上述要求的最佳路線反症,直到出口[row-1, col-1]。
輸入:
首行輸入以單個空格分割的三個正整數(shù)row畔派、col铅碍、t,row為地圖行數(shù)(0<row<=13)线椰,col為列數(shù)(0<col<=13)该酗,t為規(guī)定不能超過的游玩時長。
接下來row行,每一行有col個以空格分割的數(shù)字呜魄,代表該區(qū)域的游玩時長time(0<time<=100)
輸出:
輸出最佳游玩時長悔叽,若不存在,輸出-1爵嗅。
#include <stdio.h>
#include <iostream>
using namespace std;
int total=0,result=0,row,col,t;
void DFS(int Graph[13][13],int i,int j,int total) {
if (Graph[i][j] == 0) //邊界返回
return;
total += Graph[i][j];
// cout <<"Graph["<<i<<"]["<<j<<"]: "<<Graph[i][j]<<" total+=Graph = "<<total<<endl;
if ((i==row-1) && (j==col-1)){ //終點返回
if(((t-total)<(t-result)) && (total<=t))
result = total;
total = 0;
return;
}
DFS(Graph,i+1,j,total);
DFS(Graph,i,j+1,total);
}
int main(){
int Graph[13][13]={0};
int i,j,k;
cin>>row>>col>>t;
for(i=0;i<row;i++){
for(j=0;j<col;j++)
cin>>Graph[i][j];
}
DFS(Graph,0,0,total);
if(result!=0)
cout<<result;
else
cout<<"-1";
return 0;
}
=>5 5 30
=>3 5 4 2 3
=>4 5 3 4 3
=>4 3 5 3 2
=>2 5 3 3 5
=>5 3 4 4 1
30
05 HJ_1
描述
計算字符串最后一個單詞的長度娇澎,單詞以空格隔開,字符串長度小于5000睹晒。(注:字符串末尾不以空格為結(jié)尾)
輸入描述:
輸入一行趟庄,代表要計算的字符串,非空伪很,長度小于5000戚啥。
輸出描述:
輸出一個整數(shù),表示輸入字符串最后一個單詞的長度锉试。
#include <stdio.h>
#include <string.h>
int main()
{
char str[100];
while((scanf("%s",&str))!=EOF);
printf("%d",(strlen(str)));
return 0;
}
06 HJ_2
描述
寫出一個程序猫十,接受一個由字母、數(shù)字和空格組成的字符串呆盖,和一個字符拖云,然后輸出輸入字符串中該字符的出現(xiàn)次數(shù)。(不區(qū)分大小寫字母)
數(shù)據(jù)范圍: 1 \le n \le 1000 \1≤n≤1000
輸入描述:
第一行輸入一個由字母和數(shù)字以及空格組成的字符串应又,第二行輸入一個字符宙项。
輸出描述:
輸出輸入字符串中含有該字符的個數(shù)。(不區(qū)分大小寫字母)
#include <cstring>
#include <iostream>
using namespace std;
int main(){
char c,c2;
char s[1000]="";
int n=0,i=0;
while((c=getchar())!='\n'){
if(c>=97 && c<=122)
c-=32;
s[i++] = c;
}
cin>>c2;
for(i=0;i<strlen(s);i++){
if((c2==s[i]) || ((c2-32)==s[i]))
n++;
}
cout<<n;
return 0;
}
07 HJ_5
描述
寫出一個程序株扛,接受一個十六進制的數(shù)尤筐,輸出該數(shù)值的十進制表示。
數(shù)據(jù)范圍:保證結(jié)果在 1 \le n \le 2^{31}-1 \1≤n≤2
31
?1
輸入描述:
輸入一個十六進制的數(shù)值字符串洞就。
輸出描述:
輸出該數(shù)值的十進制字符串叔磷。不同組的測試用例用\n隔開。
#include <iostream>
#include <cstring>
#include <math.h>
using namespace std;
int main(){
char Oxnumber[10];
int temp[8]={0};
int Intnumber = 0;
cin>>Oxnumber;
int i,j=0,n=0;
for(i=(strlen(Oxnumber)-1);;i--){
if(Oxnumber[i]=='x')
break;
if(Oxnumber[i]>=65 && Oxnumber[i]<=97)
temp[j++] = int(Oxnumber[i]-55);
else
temp[j++] = int(Oxnumber[i]-48);
n++;
}
for(i=0;i<n;i++){
// cout<<"temp["<<i<<"]: "<<temp[i]<<endl;
Intnumber += temp[i]*pow(16,i);
}
cout<<Intnumber;
return 0;
}
08 HJ_6
描述
功能:輸入一個正整數(shù)奖磁,按照從小到大的順序輸出它的所有質(zhì)因子(重復(fù)的也要列舉)(如180的質(zhì)因子為2 2 3 3 5 )
數(shù)據(jù)范圍: 1 \le n \le 2 \times 10^{9} + 14 \1≤n≤2×10 9+14
輸入描述:
輸入一個整數(shù)
輸出描述:
按照從小到大的順序輸出它的所有質(zhì)數(shù)的因子,以空格隔開繁疤。
#include <iostream>
#include <cstring>
using namespace std;
bool IsThis(int x){
int i,j,k;
for(i=2;i<=x/2;i++){
if(x%i==0)
return false;
}
return true;
}
int main(){
int N,i,n=0,j=0,temp;
cin>>N;
int number[100] = {0};
temp = N;
for(i=2;i<=N/2;){
if(temp%i==0){
if(IsThis(i)){
number[j++] = i;
temp /= i;
// cout<<"i: "<<i<<" yes!!!"<<endl;
n++;
}
else{
// cout<<"i: "<<i<<" nno!!!"<<endl;
continue;
}
}
else
i++;
}
if(n==0)
cout<<N;
else{
for(i=0;i<n;i++)
cout<<number[i]<<" ";
}
return 0;
}
09 HJ_8
描述
數(shù)據(jù)表記錄包含表索引index和數(shù)值value(int范圍的正整數(shù))咖为,請對表索引相同的記錄進行合并,即將相同索引的數(shù)值進行求和運算稠腊,輸出按照index值升序進行輸出躁染。
提示:
0 <= index <= 11111111
1 <= value <= 100000
輸入描述:
先輸入鍵值對的個數(shù)n(1 <= n <= 500)
接下來n行每行輸入成對的index和value值,以空格隔開
輸出描述:
輸出合并后的鍵值對(多行)
/*
這道題當(dāng)時腦子壞了架忌,寫復(fù)雜了
*/
#include <iostream>
#include <cstring>
using namespace std;
int B[500][2]={0};
void Solution(int A[500][2],int n){
int i,j=0,k,n2=0;
int temp_index,temp_value,min;
for(i=0;i<n;i++){
if(A[i][0]!=-1){
B[j][0] = A[i][0];
B[j][1] = A[i][1];
j++;
n2++;
}
}
for(i=0;i<n2;i++){
int min_index = B[i][0];
int temp = i;
for(j=i+1;j<n2;j++){
if(B[j][0] < min_index){
min_index = B[j][0];
temp = j;
}
}
temp_index = B[i][0];
temp_value = B[i][1];
B[i][0] = B[temp][0];
B[i][1] = B[temp][1];
B[temp][0] = temp_index;
B[temp][1] = temp_value;
}
for(i=0;i<n2;i++)
cout<<B[i][0]<<" "<<B[i][1]<<endl;
}
int main(){
int A[500][2]={0};
int n,i,j,k;
cin>>n;
for(i=0;i<n;i++)
cin>>A[i][0]>>A[i][1];
for(i=0;i<n-1;i++){
if(A[i][0]!=-1){
for(j=i+1;j<n;j++){
if(A[j][0] == A[i][0]){
A[i][1] += A[j][1];
A[j][0] = A[j][1] = -1;
}
}
}
}
Solution(A,n);
return 0;
}
0A HJ_14
描述
給定 n 個字符串吞彤,請對 n 個字符串按照字典序排列。
數(shù)據(jù)范圍: 1 \le n \le 1000 \1≤n≤1000 ,字符串長度滿足 1 \le len \le 100 \1≤len≤100
輸入描述:
輸入第一行為一個正整數(shù)n(1≤n≤1000),下面n行為n個字符串(字符串長度≤100),字符串中只含有大小寫字母饰恕。
輸出描述:
數(shù)據(jù)輸出n行挠羔,輸出結(jié)果為按照字典序排列的字符串。
#include <iostream>
#include <cstring>
using namespace std;
int main(){
char words[100][100];
char temp[100];
int i,j,k,n,min;
cin >> n;
for(i=0;i<n;i++)
cin >> words[i];
for(i=0;i<n-1;i++){
min = i;
for(j=i+1;j<n;j++){
if(strcmp(words[min],words[j]) > 0)
min = j;
}
cout << "words[" <<i<<"] <-> " <<"words[" <<min << "]"<<endl;
strcpy(temp,words[i]);
strcpy(words[i],words[min]);
strcpy(words[min],temp);
}
for(i=0;i<n;i++)
cout << words[i] << endl;
return(0);
}
0B HJ_22
描述
某商店規(guī)定:三個空汽水瓶可以換一瓶汽水埋嵌,允許向老板借空汽水瓶(但是必須要歸還)破加。
小張手上有n個空汽水瓶,她想知道自己最多可以喝到多少瓶汽水雹嗦。
數(shù)據(jù)范圍:輸入的正整數(shù)滿足 1 \le n \le 100 \1≤n≤100
注意:本題存在多組輸入范舀。輸入的 0 表示輸入結(jié)束,并不用輸出結(jié)果了罪。
輸入描述:
輸入文件最多包含 10 組測試數(shù)據(jù)锭环,每個數(shù)據(jù)占一行,僅包含一個正整數(shù) n( 1<=n<=100 )泊藕,表示小張手上的空汽水瓶數(shù)辅辩。n=0 表示輸入結(jié)束,你的程序不應(yīng)當(dāng)處理這一行吱七。
輸出描述:
對于每組測試數(shù)據(jù)汽久,輸出一行,表示最多可以喝的汽水瓶數(shù)踊餐。如果一瓶也喝不到景醇,輸出0。
#include <iostream>
using namespace std;
void solution(int x){
int result=0;
int s = x;
int r = 0;
int temp = r+s;
while(temp >= 3){
temp = r+s;
s = temp/3;
r = temp%3;
result += s;
}
if(temp == 2)
cout << (result+1) << endl;
else
cout << result <<endl;
}
int main(){
int bottle[10];
int i=0,j,k;
while(cin>>bottle[i]){
if(bottle[i] == 0)
break;
i++;
}
int n = i;
for(i=0;i<n;i++){
if(bottle[i] == 1)
cout << "0" << endl;
else
solution(bottle[i]);
}
// for(i=0;i<n;i++)
// cout << bottle[i] << endl;
return(0);
}
0C HJ_33
描述
原理:ip地址的每段可以看成是一個0-255的整數(shù)吝岭,把每段拆分成一個二進制形式組合起來三痰,然后把這個二進制數(shù)轉(zhuǎn)變成
一個長整數(shù)。
舉例:一個ip地址為10.0.3.193
每段數(shù)字 相對應(yīng)的二進制數(shù)
10 00001010
0 00000000
3 00000011
193 11000001
組合起來即為:00001010 00000000 00000011 11000001,轉(zhuǎn)換為10進制數(shù)就是:167773121窜管,即該IP地址轉(zhuǎn)換后的數(shù)字就是它了散劫。
數(shù)據(jù)范圍:保證輸入的是合法的 IP 序列
輸入描述:
1 輸入IP地址
2 輸入10進制型的IP地址
輸出描述:
1 輸出轉(zhuǎn)換成10進制的IP地址
2 輸出轉(zhuǎn)換后的IP地址
#include <iostream>
#include <cstring>
#include <bitset>
#include <stdlib.h>
using namespace std;
int main(){
char c;
char ip[4][3];
int IP[4]={0};
int i=0,j=0,k,count,int_ip=0,int_ip2,temp2;
while((c=getchar())!='\n'){
if(c!='.')
ip[i][j++] = c;
else{
i++;
j = 0;
}
}
cin >> int_ip2;
for(i=0;i<4;i++){
bitset<32> temp = atoi(ip[i]); //字符串轉(zhuǎn)十進制轉(zhuǎn)二進制
temp = temp << (32-8*(i+1)); //二進制移位
// cout << temp << " " << temp.to_ulong()<<endl;
int_ip += temp.to_ulong();
// cout << bint << endl;
}
cout << int_ip << endl; //二進制轉(zhuǎn)十進制
bitset<32> bin_ip = int_ip2;
// cout << bin_ip <<endl;
for(i=0;i<4;i++){
temp2 = ((bin_ip<<(i*8))>>24).to_ulong(); //移位并轉(zhuǎn)十進制
itoa(temp2,ip[i],10); //十進制轉(zhuǎn)字符串
// cout << ((bin_ip<<(i*8))>>24) << "->" <<ip[i]<<endl;
}
for(i=0;i<3;i++)
cout << ip[i] << ".";
cout << ip[3];
return(0);
}
0D HJ_34
描述
Lily上課時使用字母數(shù)字圖片教小朋友們學(xué)習(xí)英語單詞,每次都需要把這些圖片按照大心环(ASCII碼值從小到大)排列收好获搏。請大家給Lily幫忙,通過代碼解決失乾。
Lily使用的圖片使用字符"A"到"Z"常熙、"a"到"z"、"0"到"9"表示碱茁。
數(shù)據(jù)范圍:每組輸入的字符串長度滿足 1 \le n \le 1000 \1≤n≤1000
輸入描述:
一行裸卫,一個字符串,字符串中的每個字符表示一張Lily使用的圖片纽竣。
輸出描述:
Lily的所有圖片按照從小到大的順序輸出
#include <iostream>
#include <cstring>
using namespace std;
int main(){
char words[1000];
char c,temp;
int i=0,j,k,min;
while((c=getchar())!='\n')
words[i++] = c;
int n=i;
for(i=0; i<n-1; i++){
min = i;
for(j=i+1; j<n; j++){
if(words[j] < words[min])
min = j;
}
temp = words[i];
words[i] = words[min];
words[min] = temp;
}
for(i=0; i<n; i++)
cout<<words[i];
return(0);
}
0E HJ_35
描述
蛇形矩陣是由1開始的自然數(shù)依次排列成的一個矩陣上三角形墓贿。
例如茧泪,當(dāng)輸入5時,應(yīng)該輸出的三角形為:
1 3 6 10 15
2 5 9 14
4 8 13
7 12
11
輸入描述:
輸入正整數(shù)N(N不大于100)
輸出描述:
輸出一個N行的蛇形矩陣聋袋。
#include <stdio.h>
#include <iostream>
using namespace std;
int total(int x){
int i=1,result =0;
while(i!=(x+1))
result += (i++);
return result;
}
int main(){
int i,j,k,N;
cin >> N;
int Graph[100][100] = {0};
for(j=0; j<N; j++){
Graph[0][j] = total(j+1);
cout << Graph[0][j] << " ";
}
cout << endl;
for(i=1; i<N; i++){
for(j=0; j<N-i; j++){
// cout << " j= "<< j<<" ";
// cout << "Graph["<<i<<"]["<<j<<"]= "<<Graph[i][j]<<" Graph["<<(i-1)<<"]["<<j<<"]= "<<Graph[i-1][j]<<" 后者再加(i+j)= ";
Graph[i][j] = Graph[i-1][j]+j+i;
// cout <<Graph[i][j] << endl;
cout << Graph[i][j] << " ";
}
cout << endl;
}
return 0;
}
0F HJ_37
描述
有一種兔子队伟,從出生后第3個月起每個月都生一只兔子,小兔子長到第三個月后每個月又生一只兔子舱馅。
例子:假設(shè)一只兔子第3個月出生缰泡,那么它第5個月開始會每個月生一只兔子。
假如兔子都不死代嗤,問第n個月的兔子總數(shù)為多少棘钞?
數(shù)據(jù)范圍:輸入滿足 1 \le n \le 31 \1≤n≤31
輸入描述:
輸入一個int型整數(shù)表示第n個月
輸出描述:
輸出對應(yīng)的兔子總數(shù)
#include <iostream>
#include <cstring>
using namespace std;
int main(){
int n,i,j,index=1;
cin >> n;
int rabbit[10000000];
rabbit[0] = 1;
for(i=1; i<=n; i++){
j = 0;
while(rabbit[j]){
if((i-rabbit[j]) >=2){
// cout << "i: " << i << " rabbit[" << j <<"]: " << rabbit[j] <<endl;
// cout << "第 " << i <<"月生了一只兔子。"<<endl;
rabbit[index++] = i;
}
j++;
}
}
cout << index;
return(0);
}
10 HJ_53
以上三角形的數(shù)陣干毅,第一行只有一個數(shù)1宜猜,以下每行的每個數(shù),是恰好是它上面的數(shù)硝逢、左上角數(shù)和右上角的數(shù)姨拥,3個數(shù)之和(如果不存在某個數(shù),認為該數(shù)就是0)渠鸽。
求第n行第一個偶數(shù)出現(xiàn)的位置叫乌。如果沒有偶數(shù),則輸出-1徽缚。例如輸入3,則輸出2憨奸,輸入4則輸出3,輸入2則輸出-1凿试。
數(shù)據(jù)范圍: 1 \le n \le 10^9 \1≤n≤10 9
輸入描述:
輸入一個int整數(shù)
輸出描述:
輸出返回的int值
#include <iostream>
#include <cstring>
#include <bitset>
#include <stdlib.h>
using namespace std;
int main(){
int i,j,k,n;
int triangle[100][200];
triangle[0][0] = triangle[1][0] = triangle[1][1] = triangle[1][2] = 1;
cin >> n;
if(n==1 || n==2){
cout << "-1";
return 0;
}
for(i=2;i<n;i++){
triangle[i][0] = 1;
triangle[i][1] = triangle[i-1][1]+1;
for(j=2;j<2*i-1;j++){
//也可以利用對稱性
triangle[i][j] = triangle[i-1][j]+triangle[i-1][j-1]+triangle[i-1][j-2];
}
triangle[i][2*i-1] = triangle[i-1][2*i-3]+1;
triangle[i][2*i] = 1;
}
for(j=1;j<2*(n-1);j++){
if(triangle[n-1][j]%2==0){
cout << (j+1);
break;
}
}
if(j==2*(n-1))
cout <<"-1";
return(0);
}
11 HJ_94
描述
請實現(xiàn)一個計票統(tǒng)計系統(tǒng)排宰。你會收到很多投票,其中有合法的也有不合法的那婉,請統(tǒng)計每個候選人得票的數(shù)量以及不合法的票數(shù)板甘。
(注:不合法的投票指的是投票的名字不存在n個候選人的名字中!O昃妗)
數(shù)據(jù)范圍:每組輸入中候選人數(shù)量滿足 1 \le n \le 100 \1≤n≤100 盐类,總票數(shù)量滿足 1 \le n \le 100 \1≤n≤100
輸入描述:
第一行輸入候選人的人數(shù)n,第二行輸入n個候選人的名字(均為大寫字母的字符串)呛谜,第三行輸入投票人的人數(shù)在跳,第四行輸入投票。
輸出描述:
按照輸入的順序呻率,每行輸出候選人的名字和得票數(shù)量(以" : "隔開,注:英文冒號左右兩邊都有一個空格I胍)礼仗,最后一行輸出不合法的票數(shù),格式為"Invalid : "+不合法的票數(shù)。
#include <iostream>
#include <cstring>
#include <bitset>
#include <stdlib.h>
using namespace std;
int main(){
int n1,n2,i,j,flag;
int invalid = 0;
char temp[100];
cin >> n1;
char names[100][100];
int notes[100]={0};
for(i=0;i<n1;i++)
cin >> names[i];
cin >> n2;
for(i=0;i<n2;i++){
cin >> temp;
flag = 0;
for(j=0;j<n1;j++){
if(strcmp(names[j],temp)==0){
notes[j] ++;
flag = 1;
}
}
if(flag==0)
invalid++;
}
for(i=0;i<n1;i++){
cout << names[i] <<" : "<< notes[i] <<endl;
}
cout << "Invalid : "<<invalid;
return(0);
}
12 NOJ_1
給定一個整數(shù)元践,判斷它能否被3韭脊,5,7整除单旁,并輸出以下信息:
1沪羔、能同時被3,5象浑,7整除(直接輸出3 5 7蔫饰,每個數(shù)中間一個空格);
2愉豺、只能被其中兩個數(shù)整除(輸出兩個數(shù)篓吁,小的在前,大的在后蚪拦。例如:3 5或者 3 7或者5 7,中間用空格分隔)杖剪;
3、只能被其中一個數(shù)整除(輸出這個除數(shù))驰贷;
4盛嘿、不能被任何數(shù)整除,輸出小寫字符‘n’,不包括單引號括袒。
#include<string.h>
#include<stdio.h>
int main(){
int x;
scanf("%d",&x);
if(x%105==0)
printf("3 5 7\n");
else if(x%15==0)
printf("3 5\n");
else if(x%21==0)
printf("5 7\n");
else if(x%35==0)
printf("5 7\n");
else
printf("n\n");
return 0;
}
13 NOJ_2
在我們現(xiàn)在使用的日歷中, 閏年被定義為能被4整除且不能被100整除的年份次兆,或者是能被400整除的年份。例如:1700, 1800, 1900 和 2100 不是閏年箱熬,而 1600, 2000 和 2400 是閏年类垦。
給定一系列可能重復(fù)的年份,判斷這些年份是否是閏年城须。
#include<string.h>
#include<stdio.h>
int main(){
int T;
while(T<1||T>1000000){
scanf("%d",&T);
}
int year[T]={0};
char* flag[T]={""};
for(int i=0;i<T;i++){
AG: scanf("%d",&year[i]);
if(year[i]<1 || year[i]>9999)
goto AG;
if(year[i]%400==0)
flag[i]="Yes";
else if(year[i]%4==0 && year[i]%100!=0)
flag[i]="Yes";
else
flag[i]="No";
}
for(int i=0;i<T;i++){
printf("%s\n",flag[i]);
}
return 0;
}
14 NOJ_3
每一本正式出版的圖書都有一個ISBN號碼與之對應(yīng)蚤认,ISBN碼包括9位數(shù)字、1位識別碼和3位分隔符糕伐,其規(guī)定格式如“x-xxx-xxxxx-x”砰琢,其中符號“-”就是分隔符(鍵盤上的減號),最后一位是識別碼良瞧,例如0-670-82162-4就是一個標(biāo)準(zhǔn)的ISBN碼陪汽。ISBN碼的首位數(shù)字表示書籍的出版語言,例如0代表英語褥蚯;第一個分隔符“-”之后的三位數(shù)字代表出版社挚冤,例如670代表維京出版社;第二個分隔符后的五位數(shù)字代表該書在該出版社的編號赞庶;最后一位為識別碼训挡。
Input:
輸入只有一行澳骤,是一個字符序列,表示一本書的ISBN號碼(保證輸入符合ISBN號碼的格式要求)澜薄。
Output:
輸出共一行为肮,假如輸入的ISBN號碼的識別碼正確,那么輸出“Right”肤京,否則颊艳,按照規(guī)定的格式,輸出正確的ISBN號碼(包括分隔符“-”)忘分。
#include <stdio.h>
#include <string.h>
int main(){
char s[13];
int a[9],i=0,j=0;
scanf("%s",&s);
while(s[i])
a[j++]=s[i++]-'0';
int x=a[0]*1+a[2]*2+a[3]*3+a[4]*4+a[6]*5+a[7]*6+a[8]*7+a[9]*8+a[10]*9;
x%=11;
if(a[12]==x)
printf("Right\n");
else{
for(i=0;i<12;i++)
printf("%c",char(a[i]+'0'));
printf("%d",x);
}
return(0);
}
15 NOJ_4
Input:
第一行分別為矩陣的行數(shù)m和列數(shù)n(m < 100棋枕,n < 100),兩者之間以一個空格分開饭庞。
接下來輸入的m行數(shù)據(jù)中戒悠,每行包含n個整數(shù),整數(shù)之間以一個空格分開舟山。
Output:
輸出對應(yīng)矩陣的邊緣元素和
#include <stdio.h>
#include <string.h>
int main(){
int m,n,i,j;
scanf("%d %d",&m,&n);
int a[m][n]={0};
for(i=0;i<m;i++){
for(j=0;j<n;j++)
scanf("%d",&a[i][j]);
}
int x=0;
for(j=0;j<n;j++){
x+=a[0][j];
x+=a[m-1][j];
}
for(i=1;i<m-1;i++){
x+=a[i][0];
x+=a[i][n-1];
}
printf("%d ",x);
// for(i=0;i<m;i++){
// for(j=0;j<n;j++)
// printf("%d ",a[i][j]);
// printf("\n",a[i][j]);
// }
return(0);
}
16 NOJ_5(※)
BanG Dream!(バンドリ绸狐!)是一個以電視動畫、手機音樂游戲累盗、漫畫寒矿、輕小說、廣播若债、聲優(yōu)參與的現(xiàn)實演唱會和舞臺活動等形式展開的次世代少女樂隊計劃符相。BanG Dream! 少女樂團派對!(BanG Dream! Girls Band Party)是由 Craft Egg 開發(fā)的一款A(yù)ndroid / iOS / Nintendo Switch 音樂節(jié)奏類的社交手機卡牌網(wǎng)絡(luò)游戲,游戲官方簡稱為 ガルパ(Garupa)蠢琳,國內(nèi)玩家簡稱 “邦多利” 或 “邦邦”啊终。
在邦邦的卡牌系統(tǒng)中,所有出現(xiàn)的卡牌都會被分配一種屬性傲须,屬性有 Powerful蓝牲、Happy、Cool泰讽、Pure四種(分別簡稱為紅例衍、橙、藍已卸、綠屬性)佛玄,輸入中使用R/O/B/G
來表示。每張卡牌還具有三種能力值:演出力(Performance)累澡,技巧力(Technique)梦抢,形象力(Visual),三種能力值總和稱為卡牌的綜合力愧哟。
邦邦中還可以選擇一種雜志和一種區(qū)域道具來提升卡牌的綜合力:雜志能夠提升所有卡牌三種能力值其一奥吩,而特定區(qū)域的道具設(shè)置可以提升某種屬性所有卡牌的綜合力具伍。
雜志和區(qū)域道具的效果列表如下:
在游戲中,你可以任意選擇具有五張不同的卡牌組成隊伍進行游戲圈驼,全隊所有卡牌綜合力之和即為隊伍的綜合力,綜合力更高的隊伍能夠在游戲中得到更高的分數(shù)望几。
現(xiàn)在珂朵莉已經(jīng)組好了一個隊伍绩脆,并把卡牌的信息(屬性、三種能力值)告訴了你橄抹,并且還告訴你了她選擇使用的雜志以及區(qū)域道具靴迫,請你計算一下她組好的隊伍綜合力是多少。
Input:
第一行輸入兩個用空格分開的大寫字母楼誓,代表珂朵莉選擇的雜志和區(qū)域道具種類玉锌。
下面 5 行,每行先輸入一個大寫字母疟羹,表示卡牌的屬性主守,然后輸入三個正整數(shù) p t v,分別表示卡牌的 演出力(Performance)榄融,技巧力(Technique)和 形象力(Visual)参淫。(1≤p,t,v≤105)
Output:
一個正整數(shù),珂朵莉組成的隊伍的綜合力愧杯。
#include <stdio.h>
#include <string.h>
char zz[3]={'P','T','V'};
char qy[4]={'R','O','B','G'};
char color[5];
int input[5][3];
float output[5];
void F(int Input[5][3],int i,int j){
if(color[i]==qy[j])
output[i]=1.12*(Input[i][0]+Input[i][1]+Input[i][2]);
else
output[i]=Input[i][0]+Input[i][1]+Input[i][2];
for(int k=0;k<3;k++){
switch(zz[k]){
case 'P':
output[i]+=Input[i][0]*0.16;
break;
case 'T':
output[i]+=Input[i][1]*0.16;
break;
case 'V':
output[i]+=Input[i][2]*0.16;
break;
}
}
}
int main(){
int i,j;
char ZZ,QY;
scanf("%c %c",&ZZ,&QY);
for(i=0;i<3;i++){
if(ZZ!=zz[i])
zz[i]='?';
}
for(i=0;i<4;i++){
if(QY!=qy[i])
qy[i]='?';
}
// for(i=0;i<3;i++)
// printf("%c ",zz[i]);
// printf("\n");
// for(i=0;i<4;i++)
// printf("%c ",qy[i]);
// printf("\n");
for(i=0;i<5;i++)
scanf(" %c %d %d %d",&color[i],&input[i][0],&input[i][1],&input[i][2]);
// for(i=0;i<5;i++){
// printf("%c %d %d %d\n",color[i],input[i][0],input[i][1],input[i][2]);
for(i=0;i<5;i++){
switch(color[i]){
case 'R':
F(input,i,0);
break;
case 'O':
F(input,i,1);
break;
case 'B':
F(input,i,2);
break;
case 'G':
F(input,i,3);
break;
}
}
float result=0;
for(i=0;i<5;i++){
result+=output[i];
// printf("%lf\n",output[i]);
}
printf("%d",(int)result);
return(0);
}
17 NOJ_6
在一個果園里涎才,多多已經(jīng)將所有的果子打了下來,而且按果子的不同種類分成了不同的堆力九。多多決定把所有的果子合成一堆耍铜。
每一次合并,多多可以把兩堆果子合并到一起跌前,消耗的體力等于兩堆果子的重量之和棕兼。可以看出舒萎,所有的果子經(jīng)過n-1次合并之后程储,就只剩下一堆了。多多在合并果子時總共消耗的體力等于每次合并所耗體力之和臂寝。
因為還要花大力氣把這些果子搬回家章鲤,所以多多在合并果子時要盡可能地節(jié)省體力。假定每個果子重量都為1咆贬,并且已知果子的種類數(shù)和每種果子的數(shù)目败徊,你的任務(wù)是設(shè)計出合并的次序方案,使多多耗費的體力最少掏缎,并輸出這個最小的體力耗費值皱蹦。
例如有3種果子煤杀,數(shù)目依次為1,2沪哺,9沈自。可以先將1辜妓、2堆合并枯途,新堆數(shù)目為3,耗費體力為3籍滴。接著酪夷,將新堆與原先的第三堆合并,又得到新的堆孽惰,數(shù)目為12晚岭,耗費體力為12。所以多多總共耗費體力=3+12=15勋功√贡ǎ可以證明15為最小的體力耗費值。
Input:
輸入包括兩行狂鞋,第一行是一個整數(shù)n(1<=n<=10000)燎竖,表示果子的種類數(shù)。第二行包含n個整數(shù)要销,用空格分隔构回,第i個整數(shù)ai(1<=ai<=20000)是第i種果子的數(shù)目。
Output:
輸出包括一行疏咐,這一行只包含一個整數(shù)纤掸,也就是最小的體力耗費值。輸入數(shù)據(jù)保證這個值小于2^31浑塞。
#include <stdio.h>
#include <string.h>
void bubleSort(int data[], int n) {
int i,j,temp;
for(j=0;j<n-1;j++) {
for(i=0;i<n-j-1;i++) {
if(data[i]>data[i+1]) {
temp = data[i];
data[i] = data[i+1];
data[i+1] = temp;
}
}
}
}
int main(){
int n,i,j;
scanf("%d",&n);
int a[n];
for(i=0;i<n;i++)
scanf("%d",&a[i]);
bubleSort(a,n);
// for(i=0;i<n;i++)
// printf("%d ",a[i]);
// printf("\n");
int result=0;
for(i=1;i<n;i++){
int x=0;
for(j=0;j<=i;j++)
x+=a[j];
result+=x;
}
printf("%d",result);
return(0);
}
18 NOJ_7 華強的瓜田(※)
大概題意是輸入方形瓜田的邊長 n借跪,再輸入k個灑水器,輸入灑水半徑 r酌壕,問華強能灑水的瓜的個數(shù)掏愁。
#include<stdio.h>
#include<string.h>
#include<math.h>
void water(int fruit[100][100],int x, int y, int r, int n) {
for (int i = 0; i <= r; i++) {
for (int j = 0; j <= r; j++) {
if((x + i) <= n && (y + j) <= n){
if(sqrt(i*i+j*j)<=r){
fruit[x + i -1][y + j - 1] = 1;
fruit[x + i -1][y - j - 1] = 1;
fruit[x - i -1][y + j - 1] = 1;
fruit[x - i -1][y - j - 1] = 1;
// printf("%d^2+%d^2 = %lf^2\n",i,j,sqrt(i^2+j^2));
}
}
}
}
}
int main(){
int n, k, r;
int x, y;
scanf("%d %d %d", &n, &k, &r);
int fruit[100][100] = { {0} };
for (int i = 0; i < k; i++) {
scanf("%d %d", &x, &y);
water(fruit, x, y, r, n);
}
int total = 0;
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
if(fruit[i][j] == 1)
total++;
}
}
printf("%d\n",total);
// for (int i = 0; i < n; i++) {
// for (int j = 0; j < n; j++)
// printf("%d ", fruit[i][j]);
// printf("\n");
// }
return 0;
}