题目1地址:http://ac.jobdu.com/problem.php?cid=1053&pid=0
-
题目1470:调整方阵
-
输入一个N(N<=10)阶方阵,按照如下方式调整方阵:
1.将第一列中最大数所在的行与第一行对调。
2.将第二列中从第二行到第N行最大数所在的行与第二行对调。
依此类推...
N-1.将第N-1列中从第N-1行到第N行最大数所在的行与第N-1行对调。
N.输出这个方阵
题目描述:
-
输入:
-
包含多组测试数据,每组测试数据第一行为一个整数N,表示方阵的阶数.
接下来输入这个N阶方阵.
-
输出:
-
调整后的方阵
-
样例输入:
-
4 3 6 8 7 6 7 5 3 8 6 5 3 9 8 7 2
-
样例输出:
-
9 8 7 2 6 7 5 3 3 6 8 7 8 6 5 3
#include <stdio.h>
#define MAX 10
int N;
int matrix[MAX][MAX];
void Swap(int i, int j){
int tmp;
int k;
for (k = 0; k < N; ++k){
tmp = matrix[i][k];
matrix[i][k] = matrix[j][k];
matrix[j][k] = tmp;
}
}
int RowOfMax(int j){
int i;
int max_i = j;
for (i = j+1; i < N; ++i){
if (matrix[max_i][j] < matrix[i][j]){
max_i = i;
}
}
return max_i;
}
void Adjust(){
int i;
int row;
for (i = 0; i < N - 1; ++i){
row = RowOfMax(i);
Swap(i, row);
}
}
int main(void){
int i, j;
while (scanf("%d", &N) != EOF){
for (i = 0; i < N; ++i){
for (j = 0; j < N; ++j){
scanf("%d", &matrix[i][j]);
}
}
Adjust();
for (i = 0; i < N; ++i){
printf("%d", matrix[i][0]);
for (j = 1; j < N; ++j){
printf(" %d", matrix[i][j]);
}
printf("\n");
}
}
return 0;
}
题目2地址:http://ac.jobdu.com/problem.php?cid=1053&pid=1
本以为是完全背包,结果……坑爹啊!!!
#include <stdio.h>
int main(void){
int n;
int cnt;
while (scanf("%d", &n) != EOF){
cnt = 0;
if (n >= 100){
cnt += n / 100;
n %= 100;
}
if (n >= 50){
cnt += n / 50;
n %= 50;
}
if (n >= 20){
cnt += n / 20;
n %= 20;
}
if (n >= 10){
cnt += n / 10;
n %= 10;
}
if (n >= 5){
cnt += n / 5;
n %= 5;
}
if (n >= 2){
cnt += n / 2;
n %= 2;
}
cnt += n;
printf ("%d\n", cnt);
}
return 0;
}
题目3地址:http://ac.jobdu.com/problem.php?cid=1053&pid=2
#include <stdio.h>
#include <math.h>
int GCD(int a, int b){
int tmp;
if (b == 0)
return a;
while (b != 0){
tmp = a % b;
a = b;
b = tmp;
}
return a;
}
int main(void){
int a, b;
int gcd;
int i;
int cnt;
int tmp;
while (scanf("%d%d", &a, &b) != EOF){
gcd = GCD(a, b);
cnt = 2;
tmp = (int)sqrt((double)gcd);
for (i = 2; i <= tmp; ++i){
if (gcd % i == 0)
cnt += 2;
}
if (tmp * tmp == gcd)
--cnt;
printf("%d\n", cnt);
}
return 0;
}
题目4地址:http://ac.jobdu.com/problem.php?cid=1053&pid=3
#include <stdio.h>
#define MAX 100000
int grade[MAX];
int dp1[MAX];
int dp2[MAX];
void LIS(int n){
int i;
for (i = 0; i < n; ++i)
dp1[i] = 1;
for (i = 1; i < n; ++i){
if (grade[i] > grade[i-1])
dp1[i] = dp1[i-1] + 1;
}
}
void LDS(int n){
int i;
for (i = 0; i < n; ++i)
dp2[i] = 1;
for (i = n-2; i >= 0; --i){
if (grade[i] > grade[i+1])
dp2[i] = dp2[i+1] + 1;
}
}
int Max(int a, int b){
return (a > b) ? a : b;
}
int MaxNumOfCandy(int n){
int i;
int cnt = 0;
for (i = 0; i < n; ++i){
cnt += Max(dp1[i], dp2[i]);
}
return cnt;
}
int main(void){
int n;
int i;
while (scanf("%d", &n) != EOF){
for (i = 0; i < n; ++i)
scanf("%d", &grade[i]);
LIS(n);
LDS(n);
printf("%d\n", MaxNumOfCandy(n));
}
return 0;
}