[TOC]
hdoj_2000
注意getchar的用方法,按照以上的输入之后,处理三个字符以外还会有换行符
#include#include #include #include using namespace std;int main(){ char arr[3]; while(scanf("%c%c%c",&arr[0],&arr[1],&arr[2])!=EOF) { //cout<
hdoj_2002
#include#include #include #include #include #define PI 3.1415927using namespace std;int main(){ //double精度高,有效数字16位,float精度7位 //用double才够表示,不然会溢出 double r; double rst; while(scanf("%lf",&r)!=EOF) { rst=(4/3.0)*PI*pow(r,3);//注意一定要3.0或者4.0,不然3/4=1 printf("%.3lf\n",rst); }}
hdoj_2005
#include#include #include #include #include using namespace std;int main(){ int year,month,day; char ch; int arr1[13]={ 0,31,28,31,30,31,30,31,31,30,31,30,31}; int arr2[13]={ 0,31,29,31,30,31,30,31,31,30,31,30,31}; while(scanf("%d%c%d%c%d",&year,&ch,&month,&ch,&day)!=EOF) { int rst=0; //算完一次要归0 if(year%400==0 ||(year%4==0 && year%100!=0))//闰年 { for(int i=0;i
hdoj_2006
#include#include #include #include #include using namespace std;int main(){ int n; while(scanf("%d",&n)!=EOF) { int arr[10000];//不可以用int arr[n]本地正确,但是oj会编译出错 long long rst=1; for(int i=0;i
hdoj_2007
#include#include #include #include #include using namespace std;int main(){ int L,R; while(scanf("%d %d",&L,&R)!=EOF) { long long rst1=0,rst2=0; if(L>R) swap(L,R) ;//坑点在此,题目没有明说大小 for(int i=L;i<=R;i++) { if(i%2==0) rst1+=pow(i,2); else rst2+=pow(i,3); } printf("%lld %lld\n",rst1,rst2); }}
int min=INT_MAX;//定义最大能表示的数bool cmp(int a,int b){ return abs(a)>abs(b)?true:false;//返回由大到小的数组排序 } sort(arr,arr+n,cmp);//按照绝对值由大到小排序
#include#include #include #include #include #include using namespace std;int arr[2000][2000];int main(){ int m,n; while(scanf("%d %d",&m,&n)!=EOF) { //int arr[2000][2000]; int arr[2000][2000]就退出,这么大居然不用new/malloc。 //函数体内的数组会放在栈中,一般会爆。不过还是推荐使用malloc/calloc把数组放在堆上或者开全局 //全局变量存在于静态区,这样就没有爆栈的问题了。 int rst=0; int position_x=0,position_y=0; for(int i=0;i
#include#include #include #include #include #include using namespace std;int arr[51][6];int main(){ int n,m; while(scanf("%d %d",&n,&m)!=EOF) { int rst=0; double sub_score[6]; double stu_score[51]; memset(sub_score,0.0,sizeof(sub_score));//必须是include 中 memset(stu_score,0.0,sizeof(stu_score)); for(int i=0;i
#include#include #include #include #include #include using namespace std;int main(){ /* gets(str);//gets只能是char数组不能是string类型。gets(string)是错误的 puts(str); "ff ai_2"含有空格的也可以正常读入(for也可以) */ int n; while(scanf("%d",&n)!=EOF) { //getchar();//注意如果没有getchar()的话\n会被计入str //cout< ='A'&& str[i]<='Z') || (str[i]<='z' && str[i]>='a') || str[i]=='_' )) { printf("no\n"); tag=false; break; } if(i!=0 && !( (str[i]>='A'&& str[i]<='Z') || (str[i]<='z' && str[i]>='a') || str[i]=='_' || (str[i]<='9'&&str[i]>='0'))) { tag=false; printf("no\n"); break; } } if(tag) printf("yes\n"); } } return 0; }
#include#include #include #include #include #include using namespace std;/*汉字编码方式很多,有国标码(GB2312_1980),大五码,GBK,简体18030,区位码,电报码,还有它们的变形:unicode 大端小端码,UTF-8,UTF-*,HZ 等等。内码特点 :二进制双字节,每字节用到8bits.输入原理,把连续输入的ASCII字符串,通过中文输入软件转化为双字节 中文内码。在C语言中,可以通过将汉字作为字符串输入。由于一个汉字占2个字节,所以对汉字的操作,只能以2个字节作为操作单位。下面通过具体实例来说明汉字在C语言中的使用:char s[] = "首都北京"; // 将汉字字符赋值给字符数组char s2[20]; // 定义字符数组,存放用户输入的汉字scanf("%s", s2); // 接收用户输入的汉字字符printf("%d\n", sizeof(s)); // 计算字符数组s所占的内存单元,输出9(最后一个字节是结束字符'\0')printf("%s\n", &s[2]); // 输出“都北京”(首字占2个字节)printf("%s\n", s2); // 输出用户输入的汉字 **********关键是:汉字的ascii码是小于0的 */ int main(){ int n; while(scanf("%d",&n)!=EOF) { getchar(); while(n--) { int rst=0; char str[1000]; gets(str); for(int i=0;str[i]!='\0';i++) if(str[i]<0) rst++; rst=rst/2; printf("%d\n",rst); } } return 0; }