数组

xiaoxiao2021-02-28  83

3-1

#include<stdio.h> #include<string.h> int main() { int a[105]; int x,n=0; while(scanf("%d",&x)==1) a[n++]=x; for(int i = n-1; i >= 0; i--) printf("%d\n", a[i]); //printf("%d\n",a[0]); int b[200]; //memcpy(b,a,sizeof(int)*k); 从数组a复制k个元素到数组b memcpy(b,a,sizeof(a)); //把数组a全部复制到数组b printf("%d\n",b[0]); return 0; }

有n盏灯,编号为1~n。第1个人把所有灯打开,第2个人按下所有编号为2的倍数的开关(这些灯将被关掉),第3个人按下所有编号为3的倍数的开关(其中关掉的灯将被打开,开着的灯将被关闭),依此类推。一共有k个人,问最后有哪些灯开着?输入n和k,输出开着的灯的编号。k≤n≤1000。样例输入:7 3样例输出:1 5 6 7

#include<stdio.h> #include<string.h> int main() { int a[1000]; int n,k,first=1; memset(a,0,sizeof(a));//“memset(a,0,sizeof(a))”的作用是把数组a清零 scanf("%d%d",&n,&k); for(int i = 1; i<=k; i++) for(int j=1;j<=n;j++) if(j%i==0)a[j]=!a[j]; for(int i=1;i<=n;i++) if(a[i]){ if(first) first=0; else printf(" "); printf("%d",i); } return 0; }

#include<stdio.h> int main() { int n,k,first=1; scanf("%d%d",&n,&k); int a[n+1]; for(int i=0;i<n+1;i++) a[i]=0; for(int m=1;m<=k;m++) for(int j=1;j<=n;j++) if(j%m==0) a[j]=!a[j]; for(int i=1;i<n+1;i++) if(a[i]) { if(first) first = 0; else printf(" "); printf("%d", i); } printf("\n"); return 0; }

3-3

蛇形填数

#include<stdio.h> #include<string.h> int main() { int n,x,y,tot=0; int a[20][20]; scanf("%d",&n); memset(a,0,sizeof(a)); tot=a[x=0][y=n-1]=1; while(tot<n*n) { while(x+1<n&&!a[x+1][y]) a[++x][y]=++tot; while(y-1>=0&&!a[x][y-1]) a[x][--y]=++tot; while(x-1>=0&&!a[x-1][y]) a[--x][y]=++tot; while(y+1<n&&!a[x][y+1]) a[x][++y]=++tot; } for(int x=0;x<n;x++){ for(y=0;y<n;y++) printf("=",a[x][y]); printf("\n"); } return 0; }

竖式问题

#include<stdio.h> #include<string.h> int main() { int count = 0; char s[20], buf[99]; scanf("%s", s); for(int abc = 111; abc <= 999; abc++) for(int de = 11; de <= 99; de++) { int x = abc*(de%10), y = abc*(de/10), z = abc*de; sprintf(buf, "%d%d%d%d%d", abc, de, x, y, z); int ok = 1; for(int i = 0; i < strlen(buf); i++) if(strchr(s, buf[i]) == NULL) ok = 0; if(ok) { printf("<%d>\n", ++count); printf("]\nXM\n-----\n]\nM\n-----\n]\n\n", abc, de, x, y, z); } } printf("The number of solutions = %d\n", count); return 0; }
转载请注明原文地址: https://www.6miu.com/read-1699989.html

最新回复(0)