#include<iostream>
#include<stdio.h>
#include<math.h>
#include<iomanip>
using namespace std;
void average(){
int a,b,c;
scanf("%d%d%d",&a,&b,&c);
printf("%.3lf\n",double(a+b+c)/3);
}
void temperature()
{
int f;
double c;
cin >> f;
c=5*(double(f)-32)/9;
//printf("%lf\n",c);
cout <<setiosflags(ios::fixed) << setprecision(3)<< c<< endl;
}
void sum()
{
int n;
scanf("%d",&n);
printf("%d\n",n*(1+n)/2);
}
void sincos()
{
//1-4
const double pi=atan(1)*4;
int n;
scanf("%d",&n);
printf("sin(%d)=%lf,cos(%d)=%lf\n",n,sin(pi*n/180)+0.0000001,n,cos(pi*n/180)+0.0000001);
}
void distance()
{
//1-5
double x1,y1,x2,y2;
scanf("%lf%lf%lf%lf",&x1,&y1,&x2,&y2);
printf("%lf",sqrt((x1-x2)*(x1-x2)+(y1-y2)*(y1-y2)));
}
void odd()
{
int n;
scanf("%d",&n);
printf("%s",n%2?"no":"yes");
}
void discount()
{
int n;
cin>> n ;
double ans=n*95;
if(floor(ans+0.5)>300) ans*=0.85;
cout << setiosflags(ios::fixed) << setprecision(2)<< ans << endl ;
}
void abs(){
int a;
cin >> a;
cout << (a> 0 ? a : - a);
}
void triangle()
{
int a,b,c,t;
cin >> a>> b >> c;
if(a>b) {t=a; a=b; b=t;}
if(b>c) {t=b; b=c; c=t;}
if(a>b) {t=a; a=b; b= t;}
if(a+b>c&&c-b<a){
if(a*a+b*b==c*c) cout<< "yes"; else cout<< "no";}
else cout<< "not a triangle";
cout << endl;
}
void year()
{
int n;
cin >> n;
if((n%4==0&&n%100!=0)||(n%400==0)) cout << "yes";else cout <<"no";
}
int main()
{
//average();
//temperature();
//sum();
//sincos();
//distance();
//odd();
//discount();
//abs();
//triangle();
//year();
return 0;
}
7744问题
(1):
#include <stdio.h>
#include <iostream>
#include <math.h>
using namespace std;
int main(){
int a,b,n;
for (a=1;a<=9;a++)
{
for(b=0;b<=9;b++){
n=1100*a+11*b;
intm=floor(sqrt((double)n)+0.5);//sqrt()括号内的必须是double类型;
if(m*m==n)
{
printf("%d\n",n);
}
}
}
}
(2):
#include <stdio.h>
#include <iostream>
#include <math.h>
using namespace std;
int main(){
int a,b,n;
//枚举的思想;
for (int x=1;;x++)
{
int n=x*x;
if (n<1000)
{
continue;
}
if (n>9999)
{
break;
}
int hi=n/100;
int lo=n%100;
if (hi/10==hi%10&&lo/10==lo%10)
{
printf("%d\n",n);
}
}
}
3n+1的问题:
#include <stdio.h>
#include <iostream>
#include <math.h>
using namespace std;
int main(){
int n1,count=0;//注意int类型的范围是-20亿-20亿,如果n的范围是<10亿,那么用int类型很容易溢出,所以用longlong类型;
scanf("%d",&n1);//注意scanf需要使用引用型变量;
long long n2=n1;
while(n2>1){
if (n2%2==1)
{
n2=3*n2+1;
}
else
n2 /=2;
count++;
}
printf("%d\n",count);
return 0;
}
近似计算:
#include <stdio.h>
#include <iostream>
#include <math.h>
using namespace std;
int main(){
double sum=0;
for (int i=0;;i++)
{
double term=1.0/(2*i+1);
if (i%2==1)
{
sum=sum-term;
}
else
sum+=term;
if (term<1e-6)
{
break;
}
}
printf("%.6f\n",sum);
double pi=atan(1.0);//注意double类型的双精度!
printf("%.6f\n",pi);
return0;
}
累加计算:
#include <stdio.h>
#include <iostream>
#include <math.h>
using namespace std;
int main(){
int n;
scanf("%d",&n);
int sum=0;
for (int i=1;i<=n;i++)
{
int sum2=1;
for (int j=1;j<=i;j++)
{
sum2=sum2*j;
}
sum=sum+sum2;
}
printf("%d\n",sum%1000000);
return 0;
}
#include <stdio.h>
#include <iostream>
#include <math.h>
#include <time.h>
using namespace std;
int main(){
int n;
scanf("%d",&n);
int sum=0;
clock_t starttime,endtime;
starttime=clock();
for (int i=1;i<=n;i++)
{
int sum2=1;
for (int j=1;j<=i;j++)
{
sum2=sum2*j;
}
sum=sum+sum2;
}
endtime=clock();
printf("%d\n,%f",sum%1000000,double(endtime-starttime));
return 0;
}
求最大最小平均值:
#include <stdio.h>
#include <iostream>
#include <math.h>
#include <time.h>
using namespace std;
int main(){
const int INF=1000;//先定义好该量
int x,n=0,min=INF,max=-INF,s=0;
while(scanf("%d",&x)==1){
s+=x;
if (x<min)
{
min=x;
}
if (x>max)
{
max=x;
}
n++;
}
printf("%d %d%.3f\n",min,max,(double)s/n);
return 0;
}
蛇形数组:
int n,x,y,tot=0;
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 i=0;i<n;i++){
for(int j=0;j<n;j++){
printf("%3d",a[i][j]);
}
printf("\n");
}
重要算法:
#include<iostream>
#include <stdio.h>
#include<string.h>
#include <ctype.h>
using namespace std;
const char* rev="A 3 HIL JM O 2TUVWXY51SE Z 8 ";
const char* msg[]={"not a palindrome","a regular palindrome","a mirrored string","a mirrored palindrome"};
char r(char ch){
if (isalpha(ch))
{
return rev[ch - 'A'];
}
return rev[ch - '0' +25];
}
int main()
{
char s[30];
while(scanf("%s",s)==1){
int len=strlen(s);
int p=1,m=1;
for (int i=0;i<(len+1)/2;i++)
{
if(s[i]!=s[len-1-i])p=0;
if(r(s[i])!=s[len-1-i])m=0;
}
printf("%s --is %s.\n\n",s,msg[m*2+p]);
}
return 0;
}
得分:
#include<iostream>
#include<stdio.h>
using namespace std;
int main(){
char a[85];
scanf("%s",a);
int sum=0,x=0;//统计o出现的次数;
int i=0;
while(a[i]!='\0'){
if (a[i]=='O')
{
x+=1;
sum+=x;
}
i++;
}
printf("%d",sum);
}
分子量:
#include<stdio.h>
#include<string.h>
#include<ctype.h>//intisdigit(int,char)需要的头文件
#define MAX20
double getWeight(charc){
double w = 0;
switch (c) {
case 'c':
case 'C': w = 12.01;break;
case 'h':
case 'H': w = 1.008;break;
case 'o':
case 'O': w = 16.00;break;
case 'n':
case 'N': w = 14.01;break;
}
return w;
}
int main() {
char str[MAX];
scanf("%s", str);
if(isdigit(str[0])){//isdigit判断字符是不是数字
printf("输入格式错误!\n");
return 0;
}
double weight = 0;
double sum = 0;
for (int i=0; i<strlen(str);i++){
int num = 1;
for(int pre=0; isdigit(str[i]);i++){
num = pre*10+str[i]-'0';
pre = num;
}
sum += num * weight; //weight = str[i-1]'s weight
if(i<strlen(str)) weight =getWeight(str[i]); //weight = str[i]'sweight
}
sum += weight;//加上最后一次循环的原子量
printf("分子量为:%.3lfg/mol\n",sum);
return 0;
}
数数字:#include<stdio.h>
#include <math.h>
#include<string.h>
#include<ctype.h>
int main(){
//数数字
int n;
int count=0;
while((n=getchar())!=EOF){
char c=n;
if(c=='9')count++;
}
printf("%d",count);
return 0;
}
排序问题:
#include<stdio.h>
#include <math.h>
#include<string.h>
#include<ctype.h>
int main(){
int n;
scanf("%d",&n);
int a[201],b[201];
for (int i=0;i<n;i++)
{
scanf("%d",&a[i]);
}
//排序算法!!!!
for (int i=0;i<n;i++)
{
for (int j=0;j<n-1;j++)
{
intmin;
if(a[j+1]<a[j])
{
min=a[j];
a[j]=a[j+1];
a[j+1]=min;
}
}
}
for (int i=0;i<n-1;i++)
{
printf("%d",a[i]);
}
printf("%d\n",a[n-1]);
return 0;
}