多维数组/结构体
- 1887 颜色精简
#include <stdio.h>
#include <math.h>
#include <stdlib.h>
int cmp(const void* a,const void* b){
// printf("%f %f %f",*(float*)a,*(float*)b,*(float*)a-*(float*)b);
if((*(float*)a - *(float*)b)>0){
return 1;
}else{
return -1;
}
}
int main(){
int n;
scanf("%d",&n);
int arr[n][3];
for (int i=0;i<n;i++){
for (int j=0;j<3;j++){
scanf("%d",&arr[i][j]);
}
}
/*
for (int i=0;i<n;i++){
printf("(%d,%d,%d)\n",arr[i][0],arr[i][1],arr[i][2]);
}
*/
int arr1[200][3];
int count = 0;
for (int i=0;i<200;i++){
for (int j=0;j<3;j++){
scanf("%d",&arr1[i][j]);
}
if(arr1[i][0] == -1 && arr1[i][1] == -1 && arr1[i][2] == -1){
count = i;
break;
}
}
/*
for (int i=0;i<count;i++){
printf("(%d,%d,%d)\n",arr1[i][0],arr1[i][1],arr1[i][2]);
}
*/
float d[n];
float c[n];
for (int i=0;i<count;i++){
for (int k=0;k<n;k++){
d[k] = sqrt(pow((arr1[i][0]-arr[k][0]),2)+pow((arr1[i][1]-arr[k][1]),2)+pow((arr1[i][2]-arr[k][2]),2));
c[k] = d[k];
}
qsort(c,n,sizeof(float),cmp);
/*
for (int h=0;h<n;h++){
printf("%f\t %f\n",c[h],d[h]);
}
printf("\n");
*/
for (int j =0;j<n;j++){
if (c[0] == d[j]){
printf("(%d,%d,%d)\n",arr[j][0],arr[j][1],arr[j][2]);
break;
}
}
}
}
- 1916 购物
#include <stdio.h>
void Print(int,int,int,float);
int main(){
int n;//n个数据
scanf("%d",&n);
int arr[n][3];
for (int i=0;i<n;i++){
for (int j=0;j<3;j++){
scanf("%d",&arr[i][j]);// arr[][0]:半径 arr[][1]:油 arr[][2]:家和超市所形成的圆心角
}
}
//扇形弧长:n*pai*r/180
float m;
for (int i=0;i<n;i++){
Print(arr[i][2],arr[i][0],arr[i][1],m);
}
}
void Print(int a,int b,int c,float d){// a:arr[i][2]圆心角 b:arr[i][0]半径 c:arr[i][1]油
if (a <= 180){
d = a*b*3.1415926/180;
if (5*c >= 2*d){
printf("YES\n");
}else{
printf("NO\n");
}
}else if(a > 180){
d = (360-a)*b*3.1415926/180l;
if (5*c > 2*d){
printf("YES\n");
}else{
printf("NO\n");
}
}
}
- 1957 幼儿园霸王
#include <stdio.h>
#include <stdlib.h>
typedef struct Pupil{
int a;//长
int b;//宽
int c;//高
char name[9];
}PUP;
int cmp(const void* a,const void* b){
return *(int*)b-*(int*)a;
}
int main(){
int n;
scanf("%d",&n);
PUP pup[n];
for (int i=0;i<n;i++){
scanf("%d%d%d%s",&(pup[i].a),&(pup[i].b),&(pup[i].c),&(pup[i].name));
}
/*
for (int i=0;i<n;i++){
printf("(%d,%d,%d,%s)\n",(pup[i].a),(pup[i].b),(pup[i].c),(pup[i].name));
}
*/
int p[n];
int q[n];
for (int i=0;i<n;i++){
p[i] = (pup[i].a)*(pup[i].b)*(pup[i].c);
q[i] = p[i];
}
qsort(q,n,sizeof(int),cmp);
int m = n;
int v = n;
for (int i=0;i<n;i++){
for (int j=0;j<n;j++){
if(q[0] == p[i] && q[n-1] == p[j]){
m = i;
v = j;
}
}
if (m != n && v != n){
printf("%s %s\n",pup[m].name,pup[v].name);
break;
}
}
}
数组+函数
- 1917 吃葡萄
#include <stdio.h>
void NumofN(int n){
int count1 = 0;
for (int i=n-1;i>=1;i--){
int count = 0;
int m = n;
if(m == 3){
count1++;
printf("2\n");
break;
}else{
while (m>i){
if(m%i == 1){
m = m-m/i-1;
count++;
}else break;
}
if(m%i == 0 && count == i){\
count1++;
printf("%d\n",i);
break;
}
}
}
if(count1 == 0){
printf("NO Solution\n");
}
}
int main(){
int n;
scanf("%d",&n);
int arr[n];
for (int i=0;i<n;i++){
scanf("%d",&arr[i]);
}
for (int i=0;i<n;i++){
NumofN(arr[i]);
}
}
- 1888 可以编码
#include <stdio.h>
#include <string.h>
int main(){
int n;
scanf("%d",&n);
char s[n][11];
for (int i=0;i<n;i++){
scanf("%s",s[i]);
}
/*
for (int i=0;i<n;i++){
printf("%s\n",s[i]);
}
*/
int count = 1;
for (int i=0;i<n-1;i++){
for (int j=i+1;j<n;j++){
if(strlen(s[i]) < strlen(s[j])){
if(strstr(s[j],s[i]) == s[j]){
count = 0;
break;
}
}else{
if(strstr(s[i],s[j]) == s[i]){
count = 0;
break;
}
}
}
}
if (count == 1){
printf("YES\n");
}else{
printf("NO\n");
}
}