算术运算
#include
main() {
int x,y;
scanf("%d%d",&x,&y);
printf("%d",x%y);
}
#include
int main() {
int n;
scanf("%d",&n);
int sum = 0;
for(int i=0;i<n;++i){
int m;
scanf("%d",&m);
if(m<0) continue;
sum += m;
}
printf("%d",sum);
return 0;
}
#include
int main(){
int x;
scanf("%d",&x);
printf("%d\n",x*x+2*x+5);
return 0;
}
#include
int main(){
float r;
scanf("%f",&r);
printf("%f",3.14159*r*r);
}
比较运算
#include
int main(){
int a,b,c;
scanf("%d%d%d",&a,&b,&c);
printf("%d",a*a+b*b==c*c);
}
变量
#include
int main(){
int a,b,c;
scanf("%d%d%d",&a,&b,&c);
int temp = a;
a = b;
b = c;
c = temp;
printf("%d %d %d",a,b,c);
}
if
#include
#include
#include
#include
int main() {
int n;
scanf("%d",&n);
printf("%d",abs(n%2));
return 0;
}
#include
int main(){
int a,b;
scanf("%d%d",&a,&b);
printf("%d\n",a+b>10?a*b:a-b);
}
#include
int main(){
int a,b;
scanf("%d%d",&a,&b);
if(a+b<0){
printf("negative");
}else if(a+b>=4){
printf("NO");
}else{
printf("YES");
}
}
#include
#include
#include
#include
int main() {
int n;
scanf("%d",&n);
int count=0;
while(n%2){
++count;
n/=2;
}
printf("%的、n",n);
return 0;
}
#include
#include
#include
#include
int main() {
int n;
scanf("%d",&n);
int res = 0;
for(int i=0;i<4;++i){
res = res*10 + n%10;
n /=10;
}
printf("%d",res);
return 0;
}
int main() {
int n;
scanf("%d",&n);
if(0<=n && n<=3){
printf("infant");
}else if(4<=n && n<=12){
printf("child");
}else if(13<=n && n<=18){
printf("youngster");
}else if(19<=n && n<=25){
printf("youth");
}
return 0;
}
switch
#include
int main(){
char c;
scanf("%c",&c);
switch(c){
case 'a':
printf("apple");
break;
case 'b':
printf("banana");
break;
case 'c':
printf("cat");
break;
default:
printf("no");
}
}
#include
#include
#include
#include
int main() {
int a,b;
scanf("%d%d",&a,&b);
switch(a+b){
case 1:printf("one\n");break;
case 2:printf("two\n");break;
case 3:printf("three\n");break;
case 4:printf("four\n");break;
case 5:printf("five\n");break;
case 6:printf("six\n");break;
case 7:printf("seven\n");break;
case 8:printf("eight\n");break;
case 9:printf("nine\n");break;
default:printf("None\n");
}
return 0;
}
#include
#include
#include
#include
int main() {
int n,m;
scanf("%d%d",&n,&m);
int days[12]={31,28,31,30,31,30,31,31,30,31,30,31};
printf("%d",m==2&&n%4==0&&n%100!=0||n%400==0?29:days[m-1]);
return 0;
}
while
#include
#include
#include
#include
int main() {
int n;
scanf("%d",&n);
int count = 0;
while(n%2 == 0){
n/=2;
++count;
}
printf("%d\n",count);
return 0;
}
while/for
#include
#include
#include
#include
int main() {
int n;
scanf("%d",&n);
for(int i=1;i<=n;++i){
printf("%d\n",i);
}
return 0;
}
#include
#include
#include
#include
int main() {
int n;
scanf("%d",&n);
for(int i=3;i<=n;++i)
if(i%3 == 0)
printf("%d\n",i);
return 0;
}
#include
#include
#include
#include
int main() {
int n;
scanf("%d",&n);
int has = 0;
while(0 != n){
if(n%10 == 7){
has = 1;
break;
}
n /= 10;
}
printf(has?"Yes":"No");
return 0;
}
#include
#include
#include
#include
int main() {
int n;
scanf("%d",&n);
int primer = 1;
for(int i=2;i<n;++i){
if(n%i == 0){
primer = 0;
break;
}
}
printf(primer?"Yes":"No");
return 0;
}
#include
#include
#include
#include
int main() {
int n;
scanf("%d",&n);
int min;
scanf("%d",&min);
while(--n){
int m;
scanf("%d",&m);
min = min>m?m:min;
}
printf("%d",min);
return 0;
}
#include
#include
#include
#include
int main() {
int n;
scanf("%d",&n);
int max,min;
scanf("%d",&max);
min = max;
while(--n){
int m;
scanf("%d",&m);
if(max < m) max = m;
if(min > m) min = m;
}
printf("%d %d",min,max);
return 0;
}
#include
#include
#include
#include
int main() {
int n;
scanf("%d",&n);
printf("%d\n",n);
int res = 0;
while(n--){
int temp;
scanf("%d",&temp);
res += temp;
printf("%d\n",res);
}
return 0;
}
#include
#include
#include
#include
int main() {
int n;
scanf("%d",&n);
int a = 1;
int b = 1;
for(int i=3;i<=n;++i){
int tmp = a+b;
a = b;
b = tmp;
}
printf("%d",b);
return 0;
}
#include
#include
#include
#include
int main() {
int a,b;
scanf("%d%d",&a,&b);
for(int i=b;i>=a;--i){
printf("%d\n",i);
}
return 0;
}
#include
int main() {
int n;
scanf("%d",&n);
int res = 0;
while(n--){
int m;
scanf("%d",&m);
res += m;
}
printf("%d",res);
return 0;
}
#include
#include
#include
#include
int main() {
int n;
scanf("%d",&n);
int a = 4;
int b = 7;
float sum = 4.0/7.0;
for(int i=1;i<n;++i){
int temp = a+b;
a=b;
b=temp;
sum += 1.0*a/b;
}
printf("%d/%d\n %.2f",a,b,sum);
return 0;
}
#include
#include
#include
#include
int main() {
int n;
scanf("%d",&n);
int res = 1;
for(int i=1;i<=n;++i){
res *= i;
}
printf("%d\n",res);
return 0;
}
#include
#include
#include
#include
int main() {
int n;
scanf("%d",&n);
int res=0;
for(int i=0;i<n;++i){
int m;
scanf("%d",&m);
if(m<0) break;
res += m;
}
printf("%d",res);
return 0;
}
#include
#include
#include
#include
int main() {
int n;
scanf("%d",&n);
float ans = 0;
for(int i=1;i<=n;++i){
ans += 1.0/i;
}
printf("%f",ans);
return 0;
}
#include
#include
#include
#include
int main() {
int n;
scanf("%d",&n);
int prime=1;
for(int i=2;i<n;++i){
if(n%i == 0){
prime = 0;
break;
}
}
printf(prime?"yes":"no");
return 0;
}
#include
#include
#include
#include
int main() {
int n;
scanf("%d",&n);
int cnt = 0;
for(int i=1;i<=n;++i){
if(i%2==0 || i%3==0 || i%5==0){
++cnt;
}
}
printf("%d",cnt);
return 0;
}
#include
#include
#include
#include
int main() {
int n;
scanf("%d",&n);
int res = 0;
for(int i=0;i<4;++i){
res = res*10 + n%10;
n /=10;
}
printf("%d",res);
return 0;
}
#include
#include
#include
#include
int main() {
int n;
scanf("%d",&n);
float res = 0;
for(int i=1;i<=n;++i){
res+= 1.0/(i%2?i:-i);
}
printf("%f",res);
return 0;
}
#include
#include
#include
#include
int main() {
int n;
scanf("%d",&n);
double res=0;
int fact = 1;
for(int i=1;i<=n;++i){
fact *= i;
res += 1.0/fact;
}
printf("%.10f\n",res);
return 0;
}
#include
#include
#include
#include
int main() {
int n;
scanf("%d",&n);
int res = 0;
for(int i=1;i<=n;++i){
if(i%10==9) ++res;
if(i/10==9) ++res;
}
printf("%d",res);
return 0;
}
#include
#include
#include
#include
int main() {
int n;
scanf("%d",&n);
n--;
while(1){
int prime = 1;
for(int i=2;i<n;++i){
if(n%i==0){
prime = 0;
n--;
break;
}
}
if(prime){
break;
}
}
printf("%d",n);
return 0;
}
#include
#include
#include
#include
int main() {
int n;
scanf("%d",&n);
int temp = 0;
int m = n;
while(0 != m){
temp = temp*10 + m%10;
m /= 10;
}
printf(n==temp?"yes":"no");
return 0;
}
#include
#include
#include
#include
int main() {
int n;
scanf("%d",&n);
int sum = 0;
for(int i=0;i<n;++i){
int m;
scanf("%d",&m);
if(m<0) continue;
sum += m;
}
printf("%d",sum);
return 0;
}
双重循环
#include
#include
#include
#include
int main() {
int n;
scanf("%d",&n);
for(int i=1;i<=n;++i){
for(int j=1;j<=i;++j){
printf("%d*%d=%d ",j,i,j*i);
}
printf("\n");
}
return 0;
}
#include
#include
#include
#include
int main() {
int n;
scanf("%d",&n);
for(int i=1;i<=n;++i){
for(int j=1;j<=n;++j){
printf("%d*%d=%d",i,j,i*j);
if(j != n) printf("\t");
}
printf("\n");
}
return 0;
}
#include
#include
#include
#include
int main() {
int n;
scanf("%d",&n);
for(int i=0;i<n;++i){
for(int j=0;j<=i;++j){
printf("*");
}
printf("\n");
}
return 0;
}
#include
#include
#include
#include
int main() {
int n;
scanf("%d",&n);
for(int i=0;i<n;++i){
for(int j=0;j<i*2+1;++j){
printf("*");
}
printf("\n");
}
return 0;
}
#include
#include
#include
#include
int main() {
int n;
scanf("%d",&n);
for(int i=1;i<=n;++i){
for(int j=0;j<n-i;++j) printf(" ");
for(int j=0;j<2*i-1;++j) printf("*");
printf("\n");
}
return 0;
}
#include
#include
#include
#include
int main() {
int L,R;
scanf("%d%d",&L,&R);
int none = 1;
for(int i=L;i<=R;++i){
int temp = i;
while(0 != temp){
if(temp%10 == 7){
none = 0;
printf("%d\n",i);
break;
}
temp /= 10;
}
}
if(1 == none){
printf("None");
}
return 0;
}
#include
#include
#include
#include
int main() {
int n;
scanf("%d",&n);
for(int i=2;i<=n;++i){
while(n%i == 0){
printf("%d\n",i);
n /= i;
}
}
return 0;
}
#include
#include
#include
#include
int main() {
int y,m,n;
scanf("%d%d%d",&y,&m,&n);
int days[] = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
if(y%4==0&&y%100!=0||y%400==0){
days[1] = 29;
}
int dist = 0;
for(int i=0;i<m-1;++i){
dist += days[i];
}
int day = (dist+n)%7;
printf("S M T W T F S\n",n);
for(int i=0;i<day;++i){
printf(" ");
}
for(int i=1;i<=days[m-1];++i){
printf("%d",i);
printf((day+i)%7 == 0?"\n":i>9?" ":" ");
}
return 0;
}
数字处理
#include
#include
#include
#include
int main() {
float watermelon,peach,grape,apple;
scanf("%f%f%f%f",&watermelon,&peach,&grape,&apple);
float price_watermelon = watermelon*1.2;
float price_peach = peach*3.5;
float price_grape = grape*4.5;
float price_apple = apple*5.0;
printf("%.1f\n",price_watermelon+0.01);
printf("%.1f\n",price_peach+0.01);
printf("%.1f\n",price_grape+0.01);
printf("%.1f\n",price_apple+0.01);
printf("%.1f\n",price_watermelon+price_peach+price_grape+price_apple+0.01);
return 0;
}
#include
#include
#include
#include
int main() {
int n;
scanf("%d",&n);
int a = sqrt(n);
a*a == n?printf("%d",a):printf("No");
return 0;
}
#include
#include
#include
#include
int main() {
int x,y;
scanf("%d%d",&x,&y);
int sum=x+y;
printf(
(x < 0 && y < 0 && sum >= 0)
||(x > 0 && y > 0 && sum <= 0)
?"Yes":"No");
return 0;
}
#include
#include
#include
#include
int main() {
int n,m;
scanf("%d%d",&n,&m);
long long res = 0;
while(n--){
int ai;
scanf("%d",&ai);
if(ai>=m){
res+=ai;
}
}
printf("%lld",res);
return 0;
}
进制
#include
#include
#include
#include
int main() {
int a,b;
scanf("%d%d",&a,&b);
printf("%d",(a|b)-(a&b));
return 0;
}
#include
#include
#include
#include
int main() {
int n;
scanf("%d",&n);
while(0!=n){
printf("%d",n%2);
n >>= 1;
}
return 0;
}
#include
#include
#include
#include
int toDec(int n,int base){
int res = 0;
int c = 0;
while(n){
int t = n%10;
if(t >= base) return -1;
res += t*pow(base,c);
n /= 10;
++c;
}
return res;
}
int main() {
int a,b,c;
scanf("%d%d%d",&a,&b,&c);
int res = 0;
for(int i=2;i<=16;++i){
int n = toDec(a,i);
int m = toDec(b,i);
int p = toDec(c,i);
if(n*m==p){
res = i;
break;
}
}
printf("%d",res);
return 0;
}
- 2121 转二进制2 短除法(注意整数溢出)或者使用函数itoa
#include
#include
#include
#include
int main() {
int n;
scanf("%d",&n);
int pos = 0;
char res[40];
while(0!=n){
res[pos++] = '0'+n%2;
n /= 2;
}
for(int i=pos-1;i>=0;--i){
printf("%c",res[i]);
}
return 0;
}
- 2130 进制转换 ASCII编码\进制转换\数字转字符串或者使用函数itoa
#include
int main(){
int n,b;
scanf("%d%d",&n,&b);
char table[17] = "0123456789ABCDEF";
char s[32] = {0};
int i = 0;
do{
int t = n%b;
s[i] = table[t];
++i;
n /= b;
}while(n != 0);
int j;
for(j=0;j<i/2;++j){
char t = s[j];
s[j] = s[i-1-j];
s[i-1-j] = t;
}
printf("%s\n",s);
}
多重循环
#include
#include
#include
#include
int main() {
int n;
scanf("%d",&n);
int flag = 1;
for(int o=2;o<=n;++o)
for(int r=2;r<o;++r)
for(int s=r+1;s<o;++s)
for(int t=s+1;t<o;++t)
if(pow(r,3)+pow(s,3)+pow(t,3) == pow(o,3)){
printf("(%d,%d,%d,%d)\n",o,r,s,t);
flag = 0;
}
if(flag) printf("OMG\n");
return 0;
}
数组
#include
#include
#include
#include
int main() {
int n;
scanf("%d",&n);
int arr[n];
int size = n;
while(--n >= 0){
scanf("%d",&arr[n]);
}
printf("%d\n",size);
for(int i=0;i<size;++i){
printf("%d\n",arr[i]);
}
return 0;
}
#include
#include
#include
#include
int main() {
int n;
scanf("%d",&n);
int arr[n];
int sum = 0;
for(int i=0;i<n;++i){
scanf("%d",&arr[i]);
sum += arr[i];
}
if(sum%2==1){
printf("No");
return 0;
}
int pre = 0;
for(int i=0;i<n;++i){
pre += arr[i];
if(pre == sum/2){
printf("Yes");
return 0;
}
if(pre > sum/2){
printf("No");
return 0;
}
}
return 0;
}
#include
#include
#include
#include
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){
if(1 == arr[i]){
printf("Q\n");
continue;
}
int sum = 1;
for(int j=2;j<arr[i];++j){
if(arr[i]%j == 0)
sum += j;
}
if(sum == arr[i]) printf("E\n");
else if(sum > arr[i]) printf("G\n");
else printf("Q\n");
}
return 0;
}
#include
#include
#include
#include
int main() {
int arr[1000];
int len=0;
while(1){
scanf("%d",&arr[len++]);
if(0 == arr[len-1]) break;
}
int c=0;
for(int i=0;i<len-1;++i){
for(int j=i;j<len;++j){
if(arr[i] == 2*arr[j]
|| 2*arr[i] == arr[j])
++c;
}
}
printf("%d",c);
return 0;
}
数组:记录用法
#include
#include
int main(){
int n;
scanf("%d",&n);
char s[100];
scanf("%s",s);
int seat_nums = n;
int record[26] = {0};
int count = 0;
for(int i=0;'\0'!=s[i];++i){
char c = s[i];
int no = c-'A';
if(record[no] == 1){
record[no] = 0;
++seat_nums;
continue;
}else{
if(seat_nums == 0){
++count;
++i;
}else{
--seat_nums;
record[no] = 1;
}
}
}
printf("%d\n",count);
}
#include
#include
#include
#include
int main() {
int n,m,W;
scanf("%d%d%d",&n,&m,&W);
int weights[n+1];
for(int i=1;i<=n;++i){
scanf("%d",&weights[i]);
}
int records[n+1];
memset(records,0,(n+1)*sizeof(int));
int total = 0;
int max = 0;
for(int i=0;i<m;++i){
int index;
scanf("%d",&index);
if(records[index] == 0){
records[index] = 1;
total +=weights[index];
max = max < total?total:max;
if(max > W){
printf("Oh, My God!");
return 0;
}
}else{
records[index] = 0;
total -=weights[index];
}
}
printf("%d",max);
return 0;
}
#include
#include
#include
#include
int main() {
int n,m;
scanf("%d%d",&n,&m);
int arr[n+1];
memset(arr,0,(n+1)*sizeof(int));
while(m--){
int t;
scanf("%d",&t);
arr[t]++;
}
for(int i=1;i<=n;++i){
if(arr[i] == 0) printf("%d\n",i);
}
return 0;
}
#include
#include
#include
#include
int main() {
int n,m;
scanf("%d%d",&n,&m);
int arr[n+1];
memset(arr,0,(n+1)*sizeof(int));
while(m--){
int t;
scanf("%d",&t);
arr[t]++;
}
for(int i=1;i<=n;++i){
if(arr[i] > 1)
printf("%d %d\n",i,arr[i]);
}
return 0;
}
字符串
#include
#include
#include
#include
int main() {
char str[10001];
scanf("%s",str);
for (int i=0;'\0' !=str[i];++i) {
if(str[i]>='0' && str[i]<='9'){
str[i]='*';
}
}
printf("%s",str);
return 0;
}
#include
#include
#include
#include
int main() {
char a[10000],b[10000];
scanf("%s%s",a,b);
printf(strcmp(a,b)<0?"YES":"NO");
return 0;
}
#include
#include
#include
#include
int main() {
int n;
scanf("%d",&n);
char str[10001];
scanf("%s\n",str);
char c;
scanf("%c",&c);
int pos = -1;
for(int i=0;'\0' != str[i];++i){
if(c == str[i]){
pos = i;
break;
}
}
printf("%d",pos);
return 0;
}
#include
#include
#include
#include
int main() {
char a[10001],b[10001];
scanf("%s\n%s",a,b);
char* p = strstr(a,b);
printf("%d",NULL == p?-1:p-a);
return 0;
}
- 1950 数据的存储
gets()
与scanf()
#include
int main(){
char s[2048] = {'\0'};
char t[2048] = {'\0'};
gets(s);
int count = 0;
for(int i=0,j=0;'\0'!=s[i];++i,++j){
if('"' == s[i]){
++count;
if(count%2==1){
t[j] = '`';
++j;
t[j] = '`';
}else{
t[j] = '\'';
++j;
t[j] = '\'';
}
}else{
t[j] = s[i];
}
}
printf("%s\n",t);
}
多维数组/结构体
#include
#include
#include
#include
int main() {
int n;
scanf("%d",&n);
int table[n][3];
for(int i=0;i<n;++i){
scanf("%d%d%d"
,&(table[i][0]),&(table[i][1]),&(table[i][2]));
}
int test[100][3];
int r,g,b;
int m=0;
while(1){
scanf("%d%d%d",&r,&g,&b);
if(r==-1 && g==-1 && b==-1)break;
long long min = LONG_LONG_MAX;
for(int i=0;i<n;++i){
int dist =
pow(r-table[i][0],2)+
pow(g-table[i][1],2)+
pow(b-table[i][2],2);
if(dist < min){
min = dist;
test[m][0] = table[i][0];
test[m][1] = table[i][1];
test[m][2] = table[i][2];
}
}
++m;
}
for(int i=0;i<m;++i){
printf("(%d,%d,%d)\n",test[i][0],test[i][1],test[i][2]);
}
return 0;
}
#include
#include
#include
#include
#define M_PI 3.1415926
int main() {
int n;
scanf("%d",&n);
int data[n][3];
for(int i=0;i<n;++i){
scanf("%d%d%d",
&data[i][0],&data[i][1],&data[i][2]);
}
for(int i=0;i<n;++i){
printf(data[i][1]*5 >= 2*2*M_PI*data[i][0]*(data[i][2]>180?360-data[i][2]:data[i][2])/360?"Yes\n":"No\n");
}
return 0;
}
#include
#include
#include
#include
int main() {
int n;
scanf("%d",&n);
char maxName[8];
char minName[8];
int maxValue = INT_MIN;
int minValue = INT_MAX;
int l,w,h;
char name[8];
while(n--){
scanf("%d%d%d%s\n",&l,&w,&h,&name);
int value = l*w*h;
if(value > maxValue){
maxValue = value;
strcpy(maxName,name);
}
if(value < minValue){
minValue = value;
strcpy(minName,name);
}
}
printf("%s %s",maxName,minName);
return 0;
}
数组+函数
#include
#include
int eatGraph(int n){
for(int i=n;i>1;--i){
int t = n;
bool ok = true;
for(int j=0;j<i;++j){
int s = t-1;
if(s%i!=0) {
ok = false;
break;
}else{
t = s - s/i;
}
}
if(ok && t%i == 0){
return i;
}
}
return -1;
}
int main(){
int n;
scanf("%d",&n);
int nums[n];
for(int i=0;i<n;++i){
scanf("%d",nums+i);
}
for(int i=0;i<n;++i){
int m = eatGraph(nums[i]);
if(-1 != m){
printf("%d\n",m);
}else{
printf("No Solution\n");
}
}
}
#include
#include
#include
#include
int main() {
int n;
scanf("%d",&n);
char d[n][10];
for(int i=0;i<n;++i){
scanf("%s",&d[i]);
}
int ok = 1;
for(int i=0;i<n-1;++i){
for(int j=i+1;j<n;++j)
if(strlen(d[i]) < strlen(d[j])){
if(strstr(d[j],d[i]) == d[j]){
ok = 0;
break;
}
}else{
if(strstr(d[i],d[j]) == d[i]){
ok = 0;
break;
}
}
}
printf(ok?"Yes":"No");
return 0;
}